1 | /// <reference types="node" />
|
2 | /// <reference lib="dom" />
|
3 | declare module "error/base" {
|
4 | /**
|
5 | * @packageDocumentation
|
6 | * @internal
|
7 | */
|
8 | import { ErrorMetadata } from "types";
|
9 | export class BaseError extends Error {
|
10 | meta: ErrorMetadata;
|
11 | constructor(message: string, meta?: ErrorMetadata);
|
12 | }
|
13 | }
|
14 | declare module "error/action" {
|
15 | /**
|
16 | * @packageDocumentation
|
17 | * @internal
|
18 | */
|
19 | import { BaseError } from "error/base";
|
20 | export class ActionError extends BaseError {
|
21 | constructor(error: string | Error);
|
22 | }
|
23 | }
|
24 | declare module "logger/index" {
|
25 | import type { Logger } from "types";
|
26 | export let logger: Logger;
|
27 | export function setLogger(loggerObj: Logger): void;
|
28 | export function getLogger(): Logger;
|
29 | export function createDefaultLogger(): Logger;
|
30 | }
|
31 | declare module "error/fatal" {
|
32 | import type { BaseError } from "error/base";
|
33 | /**
|
34 | * @param err - Error object
|
35 | */
|
36 | export function fatalError(error: BaseError): void;
|
37 | }
|
38 | declare module "error/invalid-validator" {
|
39 | /**
|
40 | * @packageDocumentation
|
41 | * @internal
|
42 | */
|
43 | import { BaseError } from "error/base";
|
44 | import { Validator } from "types";
|
45 | export class InvalidValidatorError extends BaseError {
|
46 | constructor(validator: Validator);
|
47 | }
|
48 | }
|
49 | declare module "error/missing-argument" {
|
50 | /**
|
51 | * @packageDocumentation
|
52 | * @internal
|
53 | */
|
54 | import { BaseError } from "error/base";
|
55 | import { Command } from "command/index";
|
56 | import { Argument } from "types";
|
57 | export class MissingArgumentError extends BaseError {
|
58 | constructor(argument: Argument, command: Command);
|
59 | }
|
60 | }
|
61 | declare module "error/missing-flag" {
|
62 | /**
|
63 | * @packageDocumentation
|
64 | * @internal
|
65 | */
|
66 | import { BaseError } from "error/base";
|
67 | import { Command } from "command/index";
|
68 | import { Option } from "types";
|
69 | export class MissingFlagError extends BaseError {
|
70 | constructor(flag: Option, command: Command);
|
71 | }
|
72 | }
|
73 | declare module "utils/colorize" {
|
74 | export function colorize(text: string): string;
|
75 | }
|
76 | declare module "error/multi-validation" {
|
77 | /**
|
78 | * @packageDocumentation
|
79 | * @internal
|
80 | */
|
81 | import { BaseError } from "error/base";
|
82 | import type { Command } from "command/index";
|
83 | export class ValidationSummaryError extends BaseError {
|
84 | constructor(cmd: Command, errors: BaseError[]);
|
85 | }
|
86 | }
|
87 | declare module "error/no-action" {
|
88 | /**
|
89 | * @packageDocumentation
|
90 | * @internal
|
91 | */
|
92 | import { BaseError } from "error/base";
|
93 | import { Command } from "command/index";
|
94 | export class NoActionError extends BaseError {
|
95 | constructor(cmd?: Command);
|
96 | }
|
97 | }
|
98 | declare module "error/option-synopsis-syntax" {
|
99 | /**
|
100 | * @packageDocumentation
|
101 | * @internal
|
102 | */
|
103 | import { BaseError } from "error/base";
|
104 | export class OptionSynopsisSyntaxError extends BaseError {
|
105 | constructor(synopsis: string);
|
106 | }
|
107 | }
|
108 | declare module "option/utils" {
|
109 | import type { OptionSynopsis, ParserTypes } from "types";
|
110 | export function getCleanNameFromNotation(str: string, camelCased?: boolean): string;
|
111 | export function getDashedOpt(name: string): string;
|
112 | export function isNumeric(n: string): boolean;
|
113 | export function isOptionStr(str?: string): str is string;
|
114 | export function isConcatenatedOpt(str: string): string[] | false;
|
115 | export function isNegativeOpt(opt: string): boolean;
|
116 | export function isOptArray(flag: ParserTypes | ParserTypes[]): flag is ParserTypes[];
|
117 | export function formatOptName(name: string): string;
|
118 | /**
|
119 | * Parse a option synopsis
|
120 | *
|
121 | * @example
|
122 | * parseSynopsis("-f, --file <path>")
|
123 | * // Returns...
|
124 | * {
|
125 | * longName: 'file',
|
126 | * longNotation: '--file',
|
127 | * shortNotation: '-f',
|
128 | * shortName: 'f'
|
129 | * valueType: 0, // 0 = optional, 1 = required, 2 = no value
|
130 | * variadic: false
|
131 | * name: 'file'
|
132 | * notation: '--file' // either the long or short notation
|
133 | * }
|
134 | *
|
135 | * @param synopsis
|
136 | * @ignore
|
137 | */
|
138 | export function parseOptionSynopsis(synopsis: string): OptionSynopsis;
|
139 | }
|
140 | declare module "validator/utils" {
|
141 | /**
|
142 | * @packageDocumentation
|
143 | * @internal
|
144 | */
|
145 | import type { Validator } from "types";
|
146 | import { Argument, Option } from "types";
|
147 | export function isCaporalValidator(validator: Validator | undefined): validator is number;
|
148 | export function isNumericValidator(validator: Validator | undefined): boolean;
|
149 | export function isStringValidator(validator: Validator | undefined): boolean;
|
150 | export function isBoolValidator(validator: Validator | undefined): boolean;
|
151 | export function isArrayValidator(validator: Validator | undefined): boolean;
|
152 | export function checkValidator(validator: Validator | undefined): void;
|
153 | export function getTypeHint(obj: Argument | Option): string | undefined;
|
154 | }
|
155 | declare module "help/types" {
|
156 | /**
|
157 | * @packageDocumentation
|
158 | * @module caporal/types
|
159 | */
|
160 | import { Command } from "command/index";
|
161 | import { Program } from "program/index";
|
162 | import chalk from "chalk";
|
163 | import { colorize } from "utils/colorize";
|
164 | import { buildTable } from "help/utils";
|
165 | import type { GlobalOptions } from "types";
|
166 | export interface CustomizedHelpOpts {
|
167 | /**
|
168 | * Name of the section to be added in help.
|
169 | */
|
170 | sectionName: string;
|
171 | /**
|
172 | * Enable or disable the automatic coloration of text.
|
173 | */
|
174 | colorize: boolean;
|
175 | }
|
176 | export interface CustomizedHelp {
|
177 | /**
|
178 | * Various display options.
|
179 | */
|
180 | options: CustomizedHelpOpts;
|
181 | /**
|
182 | * Help text. Padding of the text is automatically handled for you.
|
183 | */
|
184 | text: string;
|
185 | }
|
186 | export type CustomizedHelpMap = Map<Command | Program, CustomizedHelp[]>;
|
187 | export interface Template {
|
188 | (ctx: TemplateContext): Promise<string> | string;
|
189 | }
|
190 | export interface TemplateFunction {
|
191 | (name: string, ctx: TemplateContext): Promise<string> | string;
|
192 | }
|
193 | export interface TemplateContext {
|
194 | prog: Program;
|
195 | cmd?: Command;
|
196 | customHelp: CustomizedHelpMap;
|
197 | globalOptions: GlobalOptions;
|
198 | chalk: typeof chalk;
|
199 | colorize: typeof colorize;
|
200 | tpl: TemplateFunction;
|
201 | table: typeof buildTable;
|
202 | indent: (str: string) => string;
|
203 | eol: string;
|
204 | eol2: string;
|
205 | eol3: string;
|
206 | spaces: string;
|
207 | }
|
208 | }
|
209 | declare module "help/utils" {
|
210 | import type { TemplateContext } from "help/types";
|
211 | import type { Option, Argument } from "types";
|
212 | import type { Command } from "command/index";
|
213 | export function buildTable(data: string[][], options?: {}): string;
|
214 | export function getDefaultValueHint(obj: Argument | Option): string | undefined;
|
215 | export function getOptionsTable(options: Option[], ctx: TemplateContext, title?: string): string;
|
216 | export function getArgumentsTable(args: Argument[], ctx: TemplateContext, title?: string): string;
|
217 | export function getCommandsTable(commands: Command[], ctx: TemplateContext, title?: string): string;
|
218 | }
|
219 | declare module "help/templates/command" {
|
220 | /**
|
221 | * @packageDocumentation
|
222 | * @internal
|
223 | */
|
224 | import type { Template } from "help/types";
|
225 | export const command: Template;
|
226 | }
|
227 | declare module "help/templates/header" {
|
228 | /**
|
229 | * @packageDocumentation
|
230 | * @internal
|
231 | */
|
232 | import type { Template } from "help/types";
|
233 | export const header: Template;
|
234 | }
|
235 | declare module "help/templates/program" {
|
236 | /**
|
237 | * @packageDocumentation
|
238 | * @internal
|
239 | */
|
240 | import type { Template } from "help/types";
|
241 | export const program: Template;
|
242 | }
|
243 | declare module "help/templates/usage" {
|
244 | /**
|
245 | * @packageDocumentation
|
246 | * @internal
|
247 | */
|
248 | import type { Template } from "help/types";
|
249 | export const usage: Template;
|
250 | }
|
251 | declare module "help/templates/custom" {
|
252 | /**
|
253 | * @packageDocumentation
|
254 | * @internal
|
255 | */
|
256 | import type { Template } from "help/types";
|
257 | export const custom: Template;
|
258 | }
|
259 | declare module "help/templates/index" {
|
260 | /**
|
261 | * @packageDocumentation
|
262 | * @internal
|
263 | */
|
264 | export * from "help/templates/command";
|
265 | export * from "help/templates/header";
|
266 | export * from "help/templates/program";
|
267 | export * from "help/templates/usage";
|
268 | export * from "help/templates/custom";
|
269 | }
|
270 | declare module "help/index" {
|
271 | /**
|
272 | * @packageDocumentation
|
273 | * @module caporal/help
|
274 | */
|
275 | import { Command } from "command/index";
|
276 | import { Program } from "program/index";
|
277 | import { CustomizedHelpOpts, TemplateContext, Template } from "help/types";
|
278 | /**
|
279 | * Customize the help
|
280 | *
|
281 | * @param obj
|
282 | * @param text
|
283 | * @param options
|
284 | * @internal
|
285 | */
|
286 | export function customizeHelp(obj: Command | Program, text: string, options: Partial<CustomizedHelpOpts>): void;
|
287 | /**
|
288 | * Register a new help template
|
289 | *
|
290 | * @param name Template name
|
291 | * @param template Template function
|
292 | *
|
293 | */
|
294 | export function registerTemplate(name: string, template: Template): Map<string, Template>;
|
295 | /**
|
296 | * Helper to be used to call templates from within templates
|
297 | *
|
298 | * @param name Template name
|
299 | * @param ctx Execution context
|
300 | * @internal
|
301 | */
|
302 | export function tpl(name: string, ctx: TemplateContext): Promise<string>;
|
303 | /**
|
304 | * @internal
|
305 | * @param program
|
306 | * @param command
|
307 | */
|
308 | export function getContext(program: Program, command?: Command): TemplateContext;
|
309 | /**
|
310 | * Return the help text
|
311 | *
|
312 | * @param program Program instance
|
313 | * @param command Command instance, if any
|
314 | * @internal
|
315 | */
|
316 | export function getHelp(program: Program, command?: Command): Promise<string>;
|
317 | }
|
318 | declare module "parser/index" {
|
319 | import type { ParserOptions, ParserResult } from "types";
|
320 | /**
|
321 | * Parse a line
|
322 | *
|
323 | * @param line Line to be parsed
|
324 | * @param options Parser options
|
325 | * @internal
|
326 | */
|
327 | export function parseLine(line: string, options?: Partial<ParserOptions>): ParserResult;
|
328 | /**
|
329 | * Parse command line arguments
|
330 | *
|
331 | * @param options Parser options
|
332 | * @param argv command line arguments array (a.k.a. "argv")
|
333 | */
|
334 | export function parseArgv(options?: Partial<ParserOptions>, argv?: string[]): ParserResult;
|
335 | }
|
336 | declare module "validator/regexp" {
|
337 | /**
|
338 | * @packageDocumentation
|
339 | * @internal
|
340 | */
|
341 | import type { ParserTypes, Argument, Option } from "types";
|
342 | /**
|
343 | * Validate using a RegExp
|
344 | *
|
345 | * @param validator
|
346 | * @param value
|
347 | * @ignore
|
348 | */
|
349 | export function validateWithRegExp(validator: RegExp, value: ParserTypes | ParserTypes[], context: Argument | Option): ParserTypes | ParserTypes[];
|
350 | }
|
351 | declare module "validator/array" {
|
352 | /**
|
353 | * @packageDocumentation
|
354 | * @internal
|
355 | */
|
356 | import type { ParserTypes, Argument, Option } from "types";
|
357 | /**
|
358 | * Validate using an array of valid values.
|
359 | *
|
360 | * @param validator
|
361 | * @param value
|
362 | * @ignore
|
363 | */
|
364 | export function validateWithArray(validator: ParserTypes[], value: ParserTypes | ParserTypes[], context: Argument | Option): ParserTypes | ParserTypes[];
|
365 | }
|
366 | declare module "validator/function" {
|
367 | /**
|
368 | * @packageDocumentation
|
369 | * @internal
|
370 | */
|
371 | import type { ParserTypes, FunctionValidator, Argument, Option } from "types";
|
372 | export function validateWithFunction(validator: FunctionValidator, value: ParserTypes | ParserTypes[], context: Argument | Option): Promise<ParserTypes | ParserTypes[]>;
|
373 | }
|
374 | declare module "validator/caporal" {
|
375 | /**
|
376 | * @packageDocumentation
|
377 | * @internal
|
378 | */
|
379 | import type { ParserTypes, Argument, Option } from "types";
|
380 | import { CaporalValidator } from "types";
|
381 | export { CaporalValidator };
|
382 | export function validateWithCaporal(validator: CaporalValidator, value: ParserTypes | ParserTypes[], context: Argument | Option, skipArrayValidation?: boolean): ParserTypes | ParserTypes[];
|
383 | /**
|
384 | * The string validator actually just cast the value to string
|
385 | *
|
386 | * @param value
|
387 | * @ignore
|
388 | */
|
389 | export function validateBoolFlag(value: ParserTypes, context: Argument | Option): boolean;
|
390 | export function validateNumericFlag(validator: number, value: ParserTypes, context: Argument | Option): number;
|
391 | export function validateArrayFlag(validator: number, value: ParserTypes | ParserTypes[], context: Argument | Option): ParserTypes | ParserTypes[];
|
392 | /**
|
393 | * The string validator actually just cast the value to string
|
394 | *
|
395 | * @param value
|
396 | * @ignore
|
397 | */
|
398 | export function validateStringFlag(value: ParserTypes | ParserTypes[]): string;
|
399 | }
|
400 | declare module "validator/validate" {
|
401 | /**
|
402 | * @packageDocumentation
|
403 | * @internal
|
404 | */
|
405 | import type { Validator, Promisable, ParsedOption, ParsedArgument, Argument, Option } from "types";
|
406 | export function validate(value: ParsedOption | ParsedArgument, validator: Validator, context: Argument | Option): Promisable<ParsedOption | ParsedArgument>;
|
407 | }
|
408 | declare module "argument/find" {
|
409 | /**
|
410 | * @packageDocumentation
|
411 | * @internal
|
412 | */
|
413 | import type { Argument } from "types";
|
414 | import type { Command } from "command/index";
|
415 | export function findArgument(cmd: Command, name: string): Argument | undefined;
|
416 | }
|
417 | declare module "argument/validate" {
|
418 | import { BaseError } from "error/index";
|
419 | import type { ArgumentsRange, ParsedArguments, ParsedArgumentsObject } from "types";
|
420 | import type { Command } from "command/index";
|
421 | /**
|
422 | * Get the number of required argument for a given command
|
423 | *
|
424 | * @param cmd
|
425 | */
|
426 | export function getRequiredArgsCount(cmd: Command): number;
|
427 | export function getArgsObjectFromArray(cmd: Command, args: ParsedArguments): ParsedArgumentsObject;
|
428 | /**
|
429 | * Check if the given command has at leat one variadic argument
|
430 | *
|
431 | * @param cmd
|
432 | */
|
433 | export function hasVariadicArgument(cmd: Command): boolean;
|
434 | export function getArgsRange(cmd: Command): ArgumentsRange;
|
435 | export function checkRequiredArgs(cmd: Command, args: ParsedArgumentsObject, parsedArgv: ParsedArguments): BaseError[];
|
436 | export function removeCommandFromArgs(cmd: Command, args: ParsedArguments): ParsedArguments;
|
437 | interface ArgsValidationResult {
|
438 | args: ParsedArgumentsObject;
|
439 | errors: BaseError[];
|
440 | }
|
441 | /**
|
442 | *
|
443 | * @param cmd
|
444 | * @param parsedArgv
|
445 | *
|
446 | * @todo Bugs:
|
447 | *
|
448 | *
|
449 | * ts-node examples/pizza/pizza.ts cancel my-order jhazd hazd
|
450 | *
|
451 | * -> result ok, should be too many arguments
|
452 | *
|
453 | */
|
454 | export function validateArgs(cmd: Command, parsedArgv: ParsedArguments): Promise<ArgsValidationResult>;
|
455 | }
|
456 | declare module "command/import" {
|
457 | /**
|
458 | * @packageDocumentation
|
459 | * @internal
|
460 | */
|
461 | import { CommandCreator } from "types";
|
462 | export function importCommand(file: string): Promise<CommandCreator>;
|
463 | }
|
464 | declare module "command/find" {
|
465 | /**
|
466 | * @packageDocumentation
|
467 | * @internal
|
468 | */
|
469 | import type { Program } from "program/index";
|
470 | import type { Command } from "command/index";
|
471 | export function findCommand(program: Program, argv: string[]): Promise<Command | undefined>;
|
472 | }
|
473 | declare module "autocomplete/types" {
|
474 | /**
|
475 | * @packageDocumentation
|
476 | * @module caporal/types
|
477 | */
|
478 | import type { Argument, Option, Promisable, ParserResult } from "types";
|
479 | import type tabtab from "tabtab";
|
480 | import type { Command } from "command/index";
|
481 | import type { Program } from "program/index";
|
482 | export interface CompletionItem {
|
483 | name: string;
|
484 | description: string;
|
485 | }
|
486 | export interface Completer {
|
487 | (ctx: CompletionContext): Promisable<(string | CompletionItem)[]>;
|
488 | }
|
489 | export interface CompletionContext {
|
490 | program: Program;
|
491 | currentCmd?: Command;
|
492 | compEnv: tabtab.TabtabEnv;
|
493 | parserResult: ParserResult;
|
494 | lastPartIsOpt: boolean;
|
495 | lastPartIsKnownOpt: boolean;
|
496 | currentOpt?: Option;
|
497 | }
|
498 | export type Completions = Map<Argument | Option, Completer>;
|
499 | }
|
500 | declare module "autocomplete/index" {
|
501 | import { Program } from "program/index";
|
502 | import { Argument, Option } from "types";
|
503 | import { Completer, CompletionItem } from "autocomplete/types";
|
504 | /**
|
505 | * Register a completion handler
|
506 | *
|
507 | * @param {Argument|Option} arg_or_opt argument or option to complete
|
508 | * @param {Function} completer
|
509 | */
|
510 | export function registerCompletion(argOrOpt: Argument | Option, completer: Completer): void;
|
511 | export function installCompletion(program: Program): Promise<void>;
|
512 | export function uninstallCompletion(program: Program): Promise<void>;
|
513 | /**
|
514 | * Called by tabtab
|
515 | */
|
516 | export function complete(program: Program, { env, argv }?: {
|
517 | env: NodeJS.ProcessEnv;
|
518 | argv: string[];
|
519 | }): Promise<CompletionItem[] | false>;
|
520 | }
|
521 | declare module "option/index" {
|
522 | /**
|
523 | * @packageDocumentation
|
524 | * @module caporal/option
|
525 | */
|
526 | import { Option, CreateOptionProgramOpts, CreateOptionCommandOpts, Action, GlobalOptions, ParserProcessedResult } from "types";
|
527 | import type { Command } from "command/index";
|
528 | import type { Program } from "program/index";
|
529 | /**
|
530 | * Create an Option object
|
531 | *
|
532 | * @internal
|
533 | * @param synopsis
|
534 | * @param description
|
535 | * @param options
|
536 | */
|
537 | export function createOption(synopsis: string, description: string, options?: CreateOptionProgramOpts | CreateOptionCommandOpts): Option;
|
538 | export { showHelp };
|
539 | /**
|
540 | * Display help. Return false to prevent further processing.
|
541 | *
|
542 | * @internal
|
543 | */
|
544 | const showHelp: Action;
|
545 | /**
|
546 | * Get the list of registered global flags
|
547 | *
|
548 | * @internal
|
549 | */
|
550 | export function getGlobalOptions(): GlobalOptions;
|
551 | export function resetGlobalOptions(): GlobalOptions;
|
552 | /**
|
553 | * Disable a global option
|
554 | *
|
555 | * @param name Can be the option short/long name or notation
|
556 | */
|
557 | export function disableGlobalOption(name: string): boolean;
|
558 | /**
|
559 | * Add a global option to the program.
|
560 | * A global option is available at the program level,
|
561 | * and associated with one given {@link Action}.
|
562 | *
|
563 | * @param a {@link Option} instance, for example created using {@link createOption()}
|
564 | */
|
565 | export function addGlobalOption(opt: Option, action?: Action): GlobalOptions;
|
566 | /**
|
567 | * Process global options, if any
|
568 | * @internal
|
569 | */
|
570 | export function processGlobalOptions(parsed: ParserProcessedResult, program: Program, command?: Command): Promise<boolean>;
|
571 | /**
|
572 | * Find a global Option action from the option name (short or long)
|
573 | *
|
574 | * @param name Short or long name
|
575 | * @internal
|
576 | */
|
577 | export function findGlobalOptAction(name: string): Action | undefined;
|
578 | /**
|
579 | * Find a global Option by it's name (short or long)
|
580 | *
|
581 | * @param name Short or long name
|
582 | * @internal
|
583 | */
|
584 | export function findGlobalOption(name: string): Option | undefined;
|
585 | export function isOptionObject(obj: unknown): obj is Option;
|
586 | }
|
587 | declare module "utils/levenshtein" {
|
588 | /**
|
589 | * @packageDocumentation
|
590 | * @internal
|
591 | */
|
592 | export function levenshtein(a: string, b: string): number;
|
593 | }
|
594 | declare module "utils/suggest" {
|
595 | /**
|
596 | * Get autocomplete suggestions
|
597 | *
|
598 | * @param {String} input - User input
|
599 | * @param {String[]} possibilities - Possibilities to retrieve suggestions from
|
600 | */
|
601 | export function getSuggestions(input: string, possibilities: string[]): string[];
|
602 | /**
|
603 | * Make diff bolder in a string
|
604 | *
|
605 | * @param from original string
|
606 | * @param to target string
|
607 | */
|
608 | export function boldDiffString(from: string, to: string): string;
|
609 | }
|
610 | declare module "error/unknown-option" {
|
611 | /**
|
612 | * @packageDocumentation
|
613 | * @internal
|
614 | */
|
615 | import { BaseError } from "error/base";
|
616 | import type { Command } from "command/index";
|
617 | /**
|
618 | * @todo Rewrite
|
619 | */
|
620 | export class UnknownOptionError extends BaseError {
|
621 | constructor(flag: string, command: Command);
|
622 | }
|
623 | }
|
624 | declare module "error/unknown-command" {
|
625 | /**
|
626 | * @packageDocumentation
|
627 | * @internal
|
628 | */
|
629 | import { BaseError } from "error/base";
|
630 | import type { Program } from "program/index";
|
631 | /**
|
632 | * @todo Rewrite
|
633 | */
|
634 | export class UnknownOrUnspecifiedCommandError extends BaseError {
|
635 | constructor(program: Program, command?: string);
|
636 | }
|
637 | }
|
638 | declare module "error/validation" {
|
639 | import { BaseError } from "error/base";
|
640 | import { Validator, ParserTypes, Argument, Option } from "types";
|
641 | interface ValidationErrorParameters {
|
642 | value: ParserTypes | ParserTypes[];
|
643 | error?: Error | string;
|
644 | validator: Validator;
|
645 | context: Argument | Option;
|
646 | }
|
647 | export class ValidationError extends BaseError {
|
648 | constructor({ value, error, validator, context }: ValidationErrorParameters);
|
649 | }
|
650 | }
|
651 | declare module "error/too-many-arguments" {
|
652 | /**
|
653 | * @packageDocumentation
|
654 | * @internal
|
655 | */
|
656 | import { BaseError } from "error/base";
|
657 | import { ArgumentsRange } from "types";
|
658 | import { Command } from "command/index";
|
659 | export class TooManyArgumentsError extends BaseError {
|
660 | constructor(cmd: Command, range: ArgumentsRange, argsCount: number);
|
661 | }
|
662 | }
|
663 | declare module "error/index" {
|
664 | /**
|
665 | * @packageDocumentation
|
666 | * @internal
|
667 | */
|
668 | export * from "error/action";
|
669 | export * from "error/base";
|
670 | export * from "error/fatal";
|
671 | export * from "error/invalid-validator";
|
672 | export * from "error/missing-argument";
|
673 | export * from "error/missing-flag";
|
674 | export * from "error/multi-validation";
|
675 | export * from "error/no-action";
|
676 | export * from "error/option-synopsis-syntax";
|
677 | export * from "error/unknown-option";
|
678 | export * from "error/unknown-command";
|
679 | export * from "error/validation";
|
680 | export * from "error/too-many-arguments";
|
681 | }
|
682 | declare module "types" {
|
683 | /**
|
684 | * List of Caporal type aliases.
|
685 | *
|
686 | * @packageDocumentation
|
687 | * @module caporal/types
|
688 | */
|
689 | import { Logger as WinstonLogger } from "winston";
|
690 | import { Program } from "program/index";
|
691 | import { Command } from "command/index";
|
692 | import { BaseError } from "error/index";
|
693 | /**
|
694 | * The Caporal logger interface. It extends the [Winston](https://github.com/winstonjs/winston) Logger interface
|
695 | * and adds the following properties & methods.
|
696 | * @noInheritDoc
|
697 | */
|
698 | export interface Logger extends WinstonLogger {
|
699 | /**
|
700 | * Allow to force disabling colors.
|
701 | */
|
702 | disableColors(): void;
|
703 | /**
|
704 | * Tells Caporal if colors are enabled or not.
|
705 | */
|
706 | colorsEnabled: boolean;
|
707 | }
|
708 | export type GlobalOptions = Map<Option, Action | undefined>;
|
709 | /**
|
710 | * Caporal-provided validator flags.
|
711 | */
|
712 | export enum CaporalValidator {
|
713 | /**
|
714 | * Number validator. Check that the value looks like a numeric one
|
715 | * and cast the provided value to a javascript `Number`.
|
716 | */
|
717 | NUMBER = 1,
|
718 | /**
|
719 | * Boolean validator. Check that the value looks like a boolean.
|
720 | * It accepts values like `true`, `false`, `yes`, `no`, `0`, and `1`
|
721 | * and will auto-cast those values to `true` or `false`.
|
722 | */
|
723 | BOOLEAN = 2,
|
724 | /**
|
725 | * String validator. Mainly used to make sure the value is a string,
|
726 | * and prevent Caporal auto-casting of numerics values and boolean
|
727 | * strings like `true` or `false`.
|
728 | */
|
729 | STRING = 4,
|
730 | /**
|
731 | * Array validator. Convert any provided value to an array. If a string is provided,
|
732 | * this validator will try to split it by commas.
|
733 | */
|
734 | ARRAY = 8
|
735 | }
|
736 | type FunctionValidatorArgument = ParsedArgument | ParsedOption;
|
737 | export interface FunctionValidator<T = FunctionValidatorArgument> {
|
738 | (value: T): Promisable<T>;
|
739 | }
|
740 | export type Validator = RegExp | FunctionValidator | CaporalValidator | ParserTypes[];
|
741 | /**
|
742 | * @internal
|
743 | */
|
744 | export interface ValidatorWrapper {
|
745 | validate(value: ParsedArgument | ParsedOption): ParserTypes | ParserTypes[] | Promise<ParserTypes>;
|
746 | getChoices(): ParserTypes[];
|
747 | }
|
748 | export interface OptionSynopsis {
|
749 | name: string;
|
750 | notation: string;
|
751 | shortName?: string;
|
752 | shortNotation?: string;
|
753 | longName?: string;
|
754 | longNotation?: string;
|
755 | allNames: string[];
|
756 | allNotations: string[];
|
757 | synopsis: string;
|
758 | valueRequired: boolean;
|
759 | valueType?: OptionValueType;
|
760 | variadic: boolean;
|
761 | }
|
762 | /**
|
763 | * Option possible value.
|
764 | *
|
765 | */
|
766 | export enum OptionValueType {
|
767 | /**
|
768 | * Value is optional.
|
769 | */
|
770 | Optional = 0,
|
771 | /**
|
772 | * Value is required.
|
773 | */
|
774 | Required = 1,
|
775 | /**
|
776 | * Option does not have any possible value
|
777 | */
|
778 | None = 2
|
779 | }
|
780 | /**
|
781 | * Option properties
|
782 | */
|
783 | export interface CreateOptionCommandOpts {
|
784 | /**
|
785 | * Optional validator
|
786 | */
|
787 | validator?: Validator;
|
788 | /**
|
789 | * Default value for the Option
|
790 | */
|
791 | default?: ParsedOption;
|
792 | /**
|
793 | * Set the Option as itself required
|
794 | */
|
795 | required?: boolean;
|
796 | /**
|
797 | * Hide the option from help
|
798 | */
|
799 | hidden?: boolean;
|
800 | }
|
801 | /**
|
802 | * Option properties
|
803 | */
|
804 | export interface CreateOptionProgramOpts extends CreateOptionCommandOpts {
|
805 | /**
|
806 | * Set to `true` for a global option.
|
807 | */
|
808 | global?: boolean;
|
809 | /**
|
810 | * Action to call when a global-option is passed.
|
811 | * Only available for global options, e.g. when `global` is set to `true`.
|
812 | */
|
813 | action?: Action;
|
814 | }
|
815 | export interface CreateArgumentOpts {
|
816 | /**
|
817 | * Argument validator.
|
818 | */
|
819 | validator?: Validator;
|
820 | /**
|
821 | * Argument default value.
|
822 | */
|
823 | default?: ParsedArgument;
|
824 | }
|
825 | export interface ArgumentSynopsis {
|
826 | /**
|
827 | * Argument name.
|
828 | */
|
829 | readonly name: string;
|
830 | /**
|
831 | * Boolean indicating if the argument is required.
|
832 | */
|
833 | readonly required: boolean;
|
834 | /**
|
835 | * Synopsis string.
|
836 | */
|
837 | readonly synopsis: string;
|
838 | /**
|
839 | * Boolean indicating if the argument is valiadic,
|
840 | * e.g. can be repeated to contain an array of values.
|
841 | */
|
842 | readonly variadic: boolean;
|
843 | }
|
844 | export interface Argument extends ArgumentSynopsis {
|
845 | readonly default?: ParsedArgument;
|
846 | readonly description: string;
|
847 | readonly choices: ParsedArgument[];
|
848 | readonly validator?: Validator;
|
849 | typeHint?: string;
|
850 | kind: "argument";
|
851 | }
|
852 | export interface Option extends OptionSynopsis {
|
853 | readonly boolean: boolean;
|
854 | readonly default?: ParsedOption;
|
855 | readonly description: string;
|
856 | readonly choices: ParsedOption[];
|
857 | readonly validator?: Validator;
|
858 | readonly required: boolean;
|
859 | readonly visible: boolean;
|
860 | typeHint?: string;
|
861 | kind: "option";
|
862 | }
|
863 | /**
|
864 | * A type that could be wrapped in a Promise, or not
|
865 | */
|
866 | export type Promisable<T> = T | Promise<T>;
|
867 | /**
|
868 | * Parameters object passed to an {@link Action} function
|
869 | */
|
870 | export interface ActionParameters {
|
871 | /**
|
872 | * Parsed command line arguments
|
873 | */
|
874 | args: ParsedArgumentsObject;
|
875 | /**
|
876 | * If the `dash` (double dash) config property is enabled,
|
877 | * this *array* will contain all arguments present
|
878 | * after '--'.
|
879 | */
|
880 | ddash: ParsedArguments;
|
881 | /**
|
882 | * Parsed command line options
|
883 | */
|
884 | options: ParsedOptions;
|
885 | /**
|
886 | * Program instance
|
887 | */
|
888 | program: Program;
|
889 | /**
|
890 | * Contextual command, if any
|
891 | */
|
892 | command?: Command;
|
893 | /**
|
894 | * Logger instance
|
895 | */
|
896 | logger: Logger;
|
897 | }
|
898 | /**
|
899 | * An action is a function that will be executed upon a command call.
|
900 | */
|
901 | export interface Action {
|
902 | (params: ActionParameters): unknown;
|
903 | }
|
904 | export interface ErrorMetadata {
|
905 | [meta: string]: unknown;
|
906 | }
|
907 | export type ParserTypes = string | number | boolean;
|
908 | /**
|
909 | * Available options for the Caporal internal parser.
|
910 | * Arguments must be referenced by their position (0-based) and options by their name (short or long)
|
911 | * in {@link ParserOptions.boolean boolean}, {@link ParserOptions.string string}
|
912 | * and {@link ParserOptions.variadic variadic} parser options.
|
913 | *
|
914 | */
|
915 | export interface ParserOptions {
|
916 | /**
|
917 | * List of {@link Argument Arguments} and {@link Options Options} to be casted as *booleans*.
|
918 | * Arguments must be referenced by their position (0-based) and options by their name (short or long).
|
919 | *
|
920 | * **Example**
|
921 | *
|
922 | * ```ts
|
923 | * import { parseArgv } from "caporal/parser"
|
924 | *
|
925 | * parseArgv({
|
926 | * boolean: [2, 'sendEmail']
|
927 | * })
|
928 | *
|
929 | * // ./my-cli-app first-arg second-arg 3rd-arg --sendEmail=1
|
930 | * // -> "3rd-arg" will be casted to boolean as well as "--sendEmail"
|
931 | * ```
|
932 | */
|
933 | boolean: (string | number)[];
|
934 | /**
|
935 | * List of {@link Argument Arguments} and {@link Options Options} to be casted as *strings*.
|
936 | * Arguments must be referenced by their position (0-based) and options by their name (short or long).
|
937 | *
|
938 | * **Example**
|
939 | *
|
940 | * ```ts
|
941 | * import { parseArgv } from "caporal/parser"
|
942 | *
|
943 | * parseArgv({
|
944 | * string: [1]
|
945 | * })
|
946 | *
|
947 | * // ./my-cli-app first-arg 2
|
948 | * // -> second arg "2" will be casted to string instead of number
|
949 | * ```
|
950 | */
|
951 | string: (string | number)[];
|
952 | /**
|
953 | * List of variadic {@link Argument Arguments} and {@link Options Options}, meaning
|
954 | * that there value is an `Array`.
|
955 | *
|
956 | * Arguments must be referenced by their position (0-based) and options by their name (short or long).
|
957 | *
|
958 | * **Example**
|
959 | *
|
960 | * ```ts
|
961 | * import { parseArgv } from "caporal/parser"
|
962 | *
|
963 | * parseArgv({
|
964 | * variadic: [1]
|
965 | * })
|
966 | *
|
967 | * // ./pizza order margherita regina --add sausages --add basil
|
968 | * {
|
969 | * args: ['order', ['margherita', 'regina']]
|
970 | * options: {
|
971 | * add: ['sausages', 'basil']
|
972 | * }
|
973 | * }
|
974 | * ```
|
975 | */
|
976 | variadic: (string | number)[];
|
977 | /**
|
978 | * Double-dash (--) handling mode. If `true`, the parser will populate the
|
979 | * {@link ParserResult.ddash} property, otherwise, arguments will be added
|
980 | * to {@link ParserResult.args}.
|
981 | */
|
982 | ddash: boolean;
|
983 | /**
|
984 | * Option aliases map.
|
985 | */
|
986 | alias: Record<string, string>;
|
987 | /**
|
988 | * Enable or disable autocasting of arguments and options. Default to `true`.
|
989 | */
|
990 | autoCast: boolean;
|
991 | }
|
992 | export type ParsedArgument = ParserTypes | ParserTypes[];
|
993 | export type ParsedArguments = ParsedArgument[];
|
994 | export interface ParsedArgumentsObject {
|
995 | [arg: string]: ParsedArgument;
|
996 | }
|
997 | export type ParsedOption = ParserTypes | ParserTypes[];
|
998 | export interface ParsedOptions {
|
999 | [opt: string]: ParsedOption;
|
1000 | }
|
1001 | /**
|
1002 | * @internal
|
1003 | */
|
1004 | export interface ArgumentsRange {
|
1005 | min: number;
|
1006 | max: number;
|
1007 | }
|
1008 | export interface ParserResult {
|
1009 | args: ParsedArguments;
|
1010 | options: ParsedOptions;
|
1011 | rawOptions: ParsedOptions;
|
1012 | line: string;
|
1013 | rawArgv: string[];
|
1014 | ddash: ParsedArguments;
|
1015 | }
|
1016 | export interface ParserProcessedResult extends Omit<ParserResult, "args"> {
|
1017 | args: ParsedArgumentsObject;
|
1018 | errors: BaseError[];
|
1019 | }
|
1020 | export interface CreateCommandParameters {
|
1021 | program: Program;
|
1022 | createCommand(description?: string): Command;
|
1023 | }
|
1024 | export interface CommandCreator {
|
1025 | (options: CreateCommandParameters): Command;
|
1026 | }
|
1027 | /**
|
1028 | * Available configuration properties for the program.
|
1029 | */
|
1030 | export interface ProgramConfig {
|
1031 | /**
|
1032 | * Strict checking of arguments count. If enabled, any additional argument willl trigger an error.
|
1033 | * Default to `true`.
|
1034 | */
|
1035 | strictArgsCount: boolean;
|
1036 | /**
|
1037 | * Strict checking of options provided. If enabled, any unknown option will trigger an error.
|
1038 | * Default to `true`.
|
1039 | */
|
1040 | strictOptions: boolean;
|
1041 | /**
|
1042 | * Auto-casting of arguments and options.
|
1043 | * Default to `true`.
|
1044 | */
|
1045 | autoCast: boolean;
|
1046 | /**
|
1047 | * Environment variable to check for log level override.
|
1048 | * Default to "CAPORAL_LOG_LEVEL".
|
1049 | */
|
1050 | logLevelEnvVar: string;
|
1051 | }
|
1052 | export interface CommandConfig {
|
1053 | /**
|
1054 | * Strict checking of arguments count. If enabled, any additional argument willl trigger an error.
|
1055 | */
|
1056 | strictArgsCount?: boolean;
|
1057 | /**
|
1058 | * Strict checking of options provided. If enabled, any unknown option will trigger an error.
|
1059 | */
|
1060 | strictOptions?: boolean;
|
1061 | /**
|
1062 | * Auto-casting of arguments and options.
|
1063 | */
|
1064 | autoCast?: boolean;
|
1065 | /**
|
1066 | * Visibility of the command in help.
|
1067 | */
|
1068 | visible: boolean;
|
1069 | }
|
1070 | export interface Configurator<T extends {}> {
|
1071 | get<K extends keyof T>(key: K): T[K];
|
1072 | getAll(): T;
|
1073 | set(props: Partial<T>): T;
|
1074 | reset(): T;
|
1075 | }
|
1076 | }
|
1077 | declare module "option/find" {
|
1078 | /**
|
1079 | * @packageDocumentation
|
1080 | * @internal
|
1081 | */
|
1082 | import type { Command } from "command/index";
|
1083 | import type { Option } from "types";
|
1084 | /**
|
1085 | * Find an option from its name for a given command
|
1086 | *
|
1087 | * @param cmd Command object
|
1088 | * @param name Option name, short or long, camel-cased
|
1089 | */
|
1090 | export function findOption(cmd: Command, name: string): Option | undefined;
|
1091 | }
|
1092 | declare module "option/validate" {
|
1093 | import { BaseError } from "error/index";
|
1094 | import type { ParsedOptions } from "types";
|
1095 | import type { Command } from "command/index";
|
1096 | export function checkRequiredOpts(cmd: Command, opts: ParsedOptions): BaseError[];
|
1097 | interface OptionsValidationResult {
|
1098 | options: ParsedOptions;
|
1099 | errors: BaseError[];
|
1100 | }
|
1101 | export function validateOptions(cmd: Command, options: ParsedOptions): Promise<OptionsValidationResult>;
|
1102 | }
|
1103 | declare module "command/validate-call" {
|
1104 | /**
|
1105 | * @packageDocumentation
|
1106 | * @internal
|
1107 | */
|
1108 | import { ParserResult, ParserProcessedResult } from "types";
|
1109 | import type { Command } from "command/index";
|
1110 | export function validateCall(cmd: Command, result: ParserResult): Promise<ParserProcessedResult>;
|
1111 | }
|
1112 | declare module "argument/synopsis" {
|
1113 | /**
|
1114 | * @packageDocumentation
|
1115 | * @internal
|
1116 | */
|
1117 | import type { ArgumentSynopsis } from "types";
|
1118 | /**
|
1119 | * Check if the argument is explicitly required
|
1120 | *
|
1121 | * @ignore
|
1122 | * @param synopsis
|
1123 | */
|
1124 | export function isRequired(synopsis: string): boolean;
|
1125 | /**
|
1126 | *
|
1127 | * @param synopsis
|
1128 | */
|
1129 | export function isVariadic(synopsis: string): boolean;
|
1130 | export function parseArgumentSynopsis(synopsis: string): ArgumentSynopsis;
|
1131 | }
|
1132 | declare module "argument/index" {
|
1133 | /**
|
1134 | * @packageDocumentation
|
1135 | * @internal
|
1136 | */
|
1137 | import type { Argument, CreateArgumentOpts } from "types";
|
1138 | /**
|
1139 | *
|
1140 | * @param synopsis - Argument synopsis
|
1141 | * @param description - Argument description
|
1142 | * @param [options] - Various argument options like validator and default value
|
1143 | */
|
1144 | export function createArgument(synopsis: string, description: string, options?: CreateArgumentOpts): Argument;
|
1145 | }
|
1146 | declare module "option/mapping" {
|
1147 | /**
|
1148 | * @packageDocumentation
|
1149 | * @internal
|
1150 | */
|
1151 | import type { Command } from "command/index";
|
1152 | export function getOptsMapping(cmd: Command): Record<string, string>;
|
1153 | }
|
1154 | declare module "config/index" {
|
1155 | /**
|
1156 | * @packageDocumentation
|
1157 | * @internal
|
1158 | */
|
1159 | import type { Configurator } from "types";
|
1160 | export function createConfigurator<T>(defaults: T): Configurator<T>;
|
1161 | }
|
1162 | declare module "command/index" {
|
1163 | import { Completer } from "autocomplete/types";
|
1164 | import type { Program } from "program/index";
|
1165 | import { Action, ParserOptions, ParserResult, Option, Argument, CreateArgumentOpts, CommandConfig, CreateOptionCommandOpts } from "types";
|
1166 | import { CustomizedHelpOpts } from "help/types";
|
1167 | /**
|
1168 | * @ignore
|
1169 | */
|
1170 | export const PROG_CMD = "__self_cmd";
|
1171 | /**
|
1172 | * @ignore
|
1173 | */
|
1174 | export const HELP_CMD = "help";
|
1175 | /**
|
1176 | * Command class
|
1177 | *
|
1178 | */
|
1179 | export class Command {
|
1180 | private program;
|
1181 | private _action?;
|
1182 | private _lastAddedArgOrOpt?;
|
1183 | private _aliases;
|
1184 | private _name;
|
1185 | private _config;
|
1186 | /**
|
1187 | * Command description
|
1188 | *
|
1189 | * @internal
|
1190 | */
|
1191 | readonly description: string;
|
1192 | /**
|
1193 | * Command options array
|
1194 | *
|
1195 | * @internal
|
1196 | */
|
1197 | readonly options: Option[];
|
1198 | /**
|
1199 | * Command arguments array
|
1200 | *
|
1201 | * @internal
|
1202 | */
|
1203 | readonly args: Argument[];
|
1204 | /**
|
1205 | *
|
1206 | * @param program
|
1207 | * @param name
|
1208 | * @param description
|
1209 | * @internal
|
1210 | */
|
1211 | constructor(program: Program, name: string, description: string, config?: Partial<CommandConfig>);
|
1212 | /**
|
1213 | * Add one or more aliases so the command can be called by different names.
|
1214 | *
|
1215 | * @param aliases Command aliases
|
1216 | */
|
1217 | alias(...aliases: string[]): Command;
|
1218 | /**
|
1219 | * Name getter. Will return an empty string in the program-command context
|
1220 | *
|
1221 | * @internal
|
1222 | */
|
1223 | get name(): string;
|
1224 | /**
|
1225 | * Add an argument to the command.
|
1226 | * Synopsis is a string like `<my-argument>` or `[my-argument]`.
|
1227 | * Angled brackets (e.g. `<item>`) indicate required input. Square brackets (e.g. `[env]`) indicate optional input.
|
1228 | *
|
1229 | * Returns the { Command} object to facilitate chaining of methods.
|
1230 | *
|
1231 | * synopsis Argument synopsis.
|
1232 | * description - Argument description.
|
1233 | * default value.
[options] - Optional parameters including validator and |
1234 | */
|
1235 | argument(synopsis: string, description: string, options?: CreateArgumentOpts): Command;
|
1236 | /**
|
1237 | * Set the corresponding action to execute for this command
|
1238 | *
|
1239 | * @param action Action to execute
|
1240 | */
|
1241 | action(action: Action): Command;
|
1242 | /**
|
1243 | * Allow chaining command() calls. See {@link Program.command}.
|
1244 | *
|
1245 | */
|
1246 | command(name: string, description: string, config?: Partial<CommandConfig>): Command;
|
1247 | /**
|
1248 | * Makes the command the default one for the program.
|
1249 | */
|
1250 | default(): Command;
|
1251 | /**
|
1252 | * Checks if the command has the given alias registered.
|
1253 | *
|
1254 | * @param alias
|
1255 | * @internal
|
1256 | */
|
1257 | hasAlias(alias: string): boolean;
|
1258 | /**
|
1259 | * Get command aliases.
|
1260 | * @internal
|
1261 | */
|
1262 | getAliases(): string[];
|
1263 | /**
|
1264 | * @internal
|
1265 | */
|
1266 | isProgramCommand(): boolean;
|
1267 | /**
|
1268 | * @internal
|
1269 | */
|
1270 | isHelpCommand(): boolean;
|
1271 | /**
|
1272 | * Hide the command from help.
|
1273 | * Shortcut to calling `.configure({ visible: false })`.
|
1274 | */
|
1275 | hide(): Command;
|
1276 | /**
|
1277 | * Add an option to the current command.
|
1278 | *
|
1279 | * @param synopsis Option synopsis like '-f, --force', or '-f, --file \<file\>', or '--with-openssl [path]'
|
1280 | * @param description Option description
|
1281 | * @param options Additional parameters
|
1282 | */
|
1283 | option(synopsis: string, description: string, options?: CreateOptionCommandOpts): Command;
|
1284 | /**
|
1285 | * @internal
|
1286 | */
|
1287 | getParserConfig(): Partial<ParserOptions>;
|
1288 | /**
|
1289 | * Return a reformated synopsis string
|
1290 | * @internal
|
1291 | */
|
1292 | get synopsis(): string;
|
1293 | /**
|
1294 | * Customize command help. Can be called multiple times to add more paragraphs and/or sections.
|
1295 | *
|
1296 | * @param text Help contents
|
1297 | * @param options Display options
|
1298 | */
|
1299 | help(text: string, options?: Partial<CustomizedHelpOpts>): Command;
|
1300 | /**
|
1301 | * Configure some behavioral properties.
|
1302 | *
|
1303 | * @param props properties to set/update
|
1304 | */
|
1305 | configure(props: Partial<CommandConfig>): Command;
|
1306 | /**
|
1307 | * Get a configuration property value.
|
1308 | *
|
1309 | * @internal
|
1310 | * @param key Property key to get value for. See {@link CommandConfig}.
|
1311 | */
|
1312 | getConfigProperty<K extends keyof CommandConfig>(key: K): CommandConfig[K];
|
1313 | /**
|
1314 | * Get the auto-casting flag.
|
1315 | *
|
1316 | * @internal
|
1317 | */
|
1318 | get autoCast(): boolean;
|
1319 | /**
|
1320 | * Auto-complete
|
1321 | */
|
1322 | complete(completer: Completer): Command;
|
1323 | /**
|
1324 | * Toggle strict mode.
|
1325 | * Shortcut to calling: `.configure({ strictArgsCount: strict, strictOptions: strict }).
|
1326 | * By default, strict settings are not defined for commands, and inherit from the
|
1327 | * program settings. Calling `.strict(value)` on a command will override the program
|
1328 | * settings.
|
1329 | *
|
1330 | * @param strict boolean enabled flag
|
1331 | */
|
1332 | strict(strict?: boolean): Command;
|
1333 | /**
|
1334 | * Computed strictOptions flag.
|
1335 | *
|
1336 | * @internal
|
1337 | */
|
1338 | get strictOptions(): boolean;
|
1339 | /**
|
1340 | * Computed strictArgsCount flag.
|
1341 | *
|
1342 | * @internal
|
1343 | */
|
1344 | get strictArgsCount(): boolean;
|
1345 | /**
|
1346 | * Enable or disable auto casting of arguments & options for the command.
|
1347 | * This is basically a shortcut to calling `command.configure({ autoCast: enabled })`.
|
1348 | * By default, auto-casting is inherited from the program configuration.
|
1349 | * This method allows overriding what's been set on the program level.
|
1350 | *
|
1351 | * @param enabled
|
1352 | */
|
1353 | cast(enabled: boolean): Command;
|
1354 | /**
|
1355 | * Visible flag
|
1356 | *
|
1357 | * @internal
|
1358 | */
|
1359 | get visible(): boolean;
|
1360 | /**
|
1361 | * Run the action associated with the command
|
1362 | *
|
1363 | * @internal
|
1364 | */
|
1365 | run(parsed: Partial<ParserResult>): Promise<unknown>;
|
1366 | }
|
1367 | /**
|
1368 | * Create a new command
|
1369 | *
|
1370 | * @internal
|
1371 | */
|
1372 | export function createCommand(...args: ConstructorParameters<typeof Command>): InstanceType<typeof Command>;
|
1373 | }
|
1374 | declare module "utils/fs" {
|
1375 | export function readdir(dirPath: string, extensions?: string): Promise<string[]>;
|
1376 | }
|
1377 | declare module "command/scan" {
|
1378 | import type { Command } from "command/index";
|
1379 | import type { Program } from "program/index";
|
1380 | export function scanCommands(program: Program, dirPath: string): Promise<Command[]>;
|
1381 | }
|
1382 | declare module "utils/version" {
|
1383 | export function detectVersion(): string | undefined;
|
1384 | }
|
1385 | declare module "program/index" {
|
1386 | /**
|
1387 | * @packageDocumentation
|
1388 | * @module caporal/program
|
1389 | */
|
1390 | import { EventEmitter } from "events";
|
1391 | import { Command } from "command/index";
|
1392 | import { CustomizedHelpOpts } from "help/types";
|
1393 | import { Action, Logger, ParserTypes, ProgramConfig, CreateArgumentOpts, CreateOptionProgramOpts, CommandConfig } from "types";
|
1394 | import { CaporalValidator } from "types";
|
1395 | /**
|
1396 | * Program class
|
1397 | *
|
1398 | * @noInheritDoc
|
1399 | */
|
1400 | export class Program extends EventEmitter {
|
1401 | private commands;
|
1402 | private _config;
|
1403 | private _version?;
|
1404 | private _name?;
|
1405 | private _description?;
|
1406 | private _programmaticMode;
|
1407 | /**
|
1408 | * @internal
|
1409 | */
|
1410 | defaultCommand?: Command;
|
1411 | private _progCommand?;
|
1412 | private _bin;
|
1413 | private _discoveryPath?;
|
1414 | private _discoveredCommands?;
|
1415 | /**
|
1416 | * Number validator. Check that the value looks like a numeric one
|
1417 | * and cast the provided value to a javascript `Number`.
|
1418 | */
|
1419 | readonly NUMBER = CaporalValidator.NUMBER;
|
1420 | /**
|
1421 | * String validator. Mainly used to make sure the value is a string,
|
1422 | * and prevent Caporal auto-casting of numerical values and boolean
|
1423 | * strings like `true` or `false`.
|
1424 | */
|
1425 | readonly STRING = CaporalValidator.STRING;
|
1426 | /**
|
1427 | * Array validator. Convert any provided value to an array. If a string is provided,
|
1428 | * this validator will try to split it by commas.
|
1429 | */
|
1430 | readonly ARRAY = CaporalValidator.ARRAY;
|
1431 | /**
|
1432 | * Boolean validator. Check that the value looks like a boolean.
|
1433 | * It accepts values like `true`, `false`, `yes`, `no`, `0`, and `1`
|
1434 | * and will auto-cast those values to `true` or `false`.
|
1435 | */
|
1436 | readonly BOOLEAN = CaporalValidator.BOOLEAN;
|
1437 | /**
|
1438 | * Program constructor.
|
1439 | * - Detects the "bin" name from process argv
|
1440 | * - Detects the version from package.json
|
1441 | * - Set up the help command
|
1442 | * @ignore
|
1443 | */
|
1444 | constructor();
|
1445 | /**
|
1446 | * @internal
|
1447 | */
|
1448 | private setupErrorHandlers;
|
1449 | /**
|
1450 | * The program-command is the command attached directly to the program,
|
1451 | * meaning there is no command-keyword used to trigger it.
|
1452 | * Mainly used for programs executing only one possible action.
|
1453 | *
|
1454 | * @internal
|
1455 | */
|
1456 | get progCommand(): Command;
|
1457 | /**
|
1458 | * Setup the help command
|
1459 | */
|
1460 | private setupHelpCommand;
|
1461 | /**
|
1462 | * Customize program help. Can be called multiple times to add more paragraphs and/or sections.
|
1463 | *
|
1464 | * @param text Help contents
|
1465 | * @param options Display options
|
1466 | */
|
1467 | help(text: string, options?: Partial<CustomizedHelpOpts>): Program;
|
1468 | /**
|
1469 | * Toggle strict mode.
|
1470 | * Shortcut to calling: `.configure({ strictArgsCount: strict, strictOptions: strict })`.
|
1471 | * By default, the program is strict, so if you want to disable strict checking,
|
1472 | * just call `.strict(false)`. This setting can be overridden at the command level.
|
1473 | *
|
1474 | * @param strict boolean enabled flag
|
1475 | */
|
1476 | strict(strict?: boolean): Program;
|
1477 | /**
|
1478 | * Configure some behavioral properties.
|
1479 | *
|
1480 | * @param props properties to set/update
|
1481 | */
|
1482 | configure(props: Partial<ProgramConfig>): Program;
|
1483 | /**
|
1484 | * Get a configuration property value. { ProgramConfig Possible keys}.
|
1485 | *
|
1486 | * key Property
|
1487 | *
|
1488 | */
|
1489 | getConfigProperty<K extends keyof ProgramConfig>(key: K): ProgramConfig[K];
|
1490 | /**
|
1491 | * Return a reformatted synopsis string
|
1492 | *
|
1493 | * @internal
|
1494 | */
|
1495 | getSynopsis(): Promise<string>;
|
1496 | /**
|
1497 | * Return the discovery path, if set
|
1498 | *
|
1499 | * @internal
|
1500 | */
|
1501 | get discoveryPath(): string | undefined;
|
1502 | /**
|
1503 | * Return the program version
|
1504 | *
|
1505 | * @internal
|
1506 | */
|
1507 | getVersion(): string | undefined;
|
1508 | /**
|
1509 | * Set the version fo your program.
|
1510 | * You won't likely use this method as Caporal tries to guess it from your package.json
|
1511 | */
|
1512 | version(ver: string): Program;
|
1513 | /**
|
1514 | * Set the program name. If not set, the filename minus the extension will be used.
|
1515 | */
|
1516 | name(name: string): Program;
|
1517 | /**
|
1518 | * Return the program name.
|
1519 | *
|
1520 | * @internal
|
1521 | */
|
1522 | getName(): string | undefined;
|
1523 | /**
|
1524 | * Return the program description.
|
1525 | *
|
1526 | * @internal
|
1527 | */
|
1528 | getDescription(): string | undefined;
|
1529 | /**
|
1530 | * Set the program description displayed in help.
|
1531 | */
|
1532 | description(desc: string): Program;
|
1533 | /**
|
1534 | * Get the bin name (the name of your executable).
|
1535 | *
|
1536 | * @internal
|
1537 | */
|
1538 | getBin(): string;
|
1539 | /**
|
1540 | * Sets the executable name. By default, it's auto-detected from the filename of your program.
|
1541 | *
|
1542 | * @param name Executable name
|
1543 | * @example
|
1544 | * ```ts
|
1545 | * program.bin('myprog')
|
1546 | * ```
|
1547 | */
|
1548 | bin(name: string): Program;
|
1549 | /**
|
1550 | * Set a custom logger for your program.
|
1551 | * Your logger should implement the {@link Logger} interface.
|
1552 | */
|
1553 | logger(logger: Logger): Program;
|
1554 | /**
|
1555 | * Disable a global option. Will warn if the global option
|
1556 | * does not exist of has already been disabled.
|
1557 | *
|
1558 | * @param name Name, short, or long notation of the option to disable.
|
1559 | */
|
1560 | disableGlobalOption(name: string): Program;
|
1561 | /**
|
1562 | * Returns the list of all commands registered
|
1563 | * - By default, Caporal creates one: the "help" command
|
1564 | * - When calling argument() or action() on the program instance,
|
1565 | * Caporal also create what is called the "program command", which
|
1566 | * is a command directly attach to the program, usually used
|
1567 | * in mono-command programs.
|
1568 | * @internal
|
1569 | */
|
1570 | getCommands(): Command[];
|
1571 | /**
|
1572 | * Add a command to the program.
|
1573 | *
|
1574 | * @param name Command name
|
1575 | * @param description Command description
|
1576 | * @example
|
1577 | * ```ts
|
1578 | * program.command('order', 'Order some food')
|
1579 | * ```
|
1580 | */
|
1581 | command(name: string, description: string, config?: Partial<CommandConfig>): Command;
|
1582 | /**
|
1583 | * Check if the program has user-defined commands.
|
1584 | *
|
1585 | * @internal
|
1586 | * @private
|
1587 | */
|
1588 | hasCommands(): Promise<boolean>;
|
1589 | /**
|
1590 | * @internal
|
1591 | */
|
1592 | getAllCommands(): Promise<Command[]>;
|
1593 | /**
|
1594 | * Return the log level override, if any is provided using
|
1595 | * the right environment variable.
|
1596 | *
|
1597 | * @internal
|
1598 | * @private
|
1599 | */
|
1600 | getLogLevelOverride(): string | undefined;
|
1601 | /**
|
1602 | * Enable or disable auto casting of arguments & options at the program level.
|
1603 | *
|
1604 | * @param enabled
|
1605 | */
|
1606 | cast(enabled: boolean): Program;
|
1607 | /**
|
1608 | * Sets a *unique* action for the *entire* program.
|
1609 | *
|
1610 | * @param {Function} action - Action to run
|
1611 | */
|
1612 | action(action: Action): Program;
|
1613 | /**
|
1614 | * Add an argument to the *unique* command of the program.
|
1615 | */
|
1616 | argument(synopsis: string, description: string, options?: CreateArgumentOpts): Command;
|
1617 | /**
|
1618 | * Add an option to the *unique* command of the program,
|
1619 | * or add a global option to the program when `options.global`
|
1620 | * is set to `true`.
|
1621 | *
|
1622 | * @param synopsis Option synopsis like '-f, --force', or '-f, --file \<file\>', or '--with-openssl [path]'
|
1623 | * @param description Option description
|
1624 | * @param options Additional parameters
|
1625 | */
|
1626 | option(synopsis: string, description: string, options?: CreateOptionProgramOpts): Program;
|
1627 | /**
|
1628 | * Discover commands from a specified path.
|
1629 | *
|
1630 | * Commands must be organized into files (one command per file) in a file tree like:
|
1631 | *
|
1632 | * ```sh
|
1633 | * └── commands
|
1634 | * ├── config
|
1635 | * │ ├── set.ts
|
1636 | * │ └── unset.ts
|
1637 | * ├── create
|
1638 | * │ ├── job.ts
|
1639 | * │ └── service.ts
|
1640 | * ├── create.ts
|
1641 | * ├── describe.ts
|
1642 | * └── get.ts
|
1643 | * ```
|
1644 | *
|
1645 | * The code above shows a short example of `kubectl` commands and subcommands.
|
1646 | * In this case, Caporal will generate the following commands:
|
1647 | *
|
1648 | * - kubectl get [args...] [options...]
|
1649 | * - kubectl config set [args...] [options...]
|
1650 | * - kubectl config unset [args...] [options...]
|
1651 | * - kubectl create [args...] [options...]
|
1652 | * - kubectl create job [args...] [options...]
|
1653 | * - kubectl create service [args...] [options...]
|
1654 | * - kubectl describe [args...] [options...]
|
1655 | * - kubectl get [args...] [options...]
|
1656 | *
|
1657 | * Notice how the `config` command has a mandatory subcommand associated,
|
1658 | * hence cannot be called without a subcommand, contrary to the `create` command.
|
1659 | * This is why there is no `config.ts` in the tree.
|
1660 | *
|
1661 | * @param path
|
1662 | */
|
1663 | discover(dirPath: string): Program;
|
1664 | /**
|
1665 | * Do a full scan of the discovery path to get all existing commands
|
1666 | * This should only be used to generate the full list of command,
|
1667 | * as for help rendering
|
1668 | *
|
1669 | * @private
|
1670 | */
|
1671 | private scanCommands;
|
1672 | /**
|
1673 | * Reset all commands
|
1674 | *
|
1675 | * @internal
|
1676 | */
|
1677 | reset(): Program;
|
1678 | /**
|
1679 | * Run the program by parsing command line arguments.
|
1680 | * Caporal will automatically detect command line arguments from `process.argv` values,
|
1681 | * but it can be overridden by providing the `argv` parameter. It returns a Promise
|
1682 | * of the value returned by the *Action* triggered.
|
1683 | *
|
1684 | * ::: warning Be careful
|
1685 | * This method returns a `Promise`. You'll usually ignore the returned promise and call run() like this:
|
1686 | *
|
1687 | * ```ts
|
1688 | * [...]
|
1689 | * program.action(...)
|
1690 | * program.run()
|
1691 | * ```
|
1692 | *
|
1693 | * If you do add some `.catch()` handler to it, Caporal won't display any potential errors
|
1694 | * that the promise could reject, and will let you the responsibility to do it.
|
1695 | * :::
|
1696 | *
|
1697 | * @param argv Command line arguments to parse, default to `process.argv.slice(2)`.
|
1698 | */
|
1699 | run(argv?: string[]): Promise<unknown>;
|
1700 | /**
|
1701 | * Try to find the executed command from argv
|
1702 | * If command cannot be found from argv, return the default command if any,
|
1703 | * then the program-command if any, or finally `undefined`.
|
1704 | * If argv is empty, and there is no defaultCommand or progCommand
|
1705 | * use the help command
|
1706 | *
|
1707 | * @param argv
|
1708 | */
|
1709 | private findCommand;
|
1710 | /**
|
1711 | * Run a command, providing parsed data
|
1712 | *
|
1713 | * @param result
|
1714 | * @param cmd
|
1715 | * @internal
|
1716 | */
|
1717 | private _run;
|
1718 | /**
|
1719 | * Programmatic usage. Execute input command with given arguments & options
|
1720 | *
|
1721 | * Not ideal regarding type casting etc.
|
1722 | *
|
1723 | * @param args argv array
|
1724 | * @param options options object
|
1725 | * @param ddash double dash array
|
1726 | * @public
|
1727 | */
|
1728 | exec(args: string[], options?: Record<string, ParserTypes>, ddash?: string[]): Promise<unknown>;
|
1729 | }
|
1730 | }
|
1731 | declare module "index" {
|
1732 | /**
|
1733 | * Main Caporal module.
|
1734 | *
|
1735 | * ## program
|
1736 | *
|
1737 | * This represents your program. You don't have to instanciate the {@link Program} class,
|
1738 | * it's already done for you.
|
1739 | *
|
1740 | * **Usage**
|
1741 | *
|
1742 | * ```ts
|
1743 | * // The Program instance generated for you
|
1744 | * import program from "@caporal/core"
|
1745 | *
|
1746 | * program
|
1747 | * .command(...)
|
1748 | * .action(...)
|
1749 | * [...]
|
1750 | * ```
|
1751 | *
|
1752 | *
|
1753 | * ## parseArgv()
|
1754 | *
|
1755 | * This is the command line parser internaly used by Caporal.
|
1756 | *
|
1757 | * ::: tip Advanced usage
|
1758 | * Usually, **you won't need to use the parser** directly, but if you
|
1759 | * just want to parse some args without all capabilities brought
|
1760 | * by Caporal, feel free to play with it.
|
1761 | * :::
|
1762 | *
|
1763 | * **Usage**
|
1764 | *
|
1765 | * ```ts
|
1766 | * import { parseArgv } from "@caporal/core"
|
1767 | *
|
1768 | * const {args, options} = parseArgv({
|
1769 | * // ... options
|
1770 | * })
|
1771 | * ```
|
1772 | *
|
1773 | * Checkout `parseArgv()` [documentation here](/api/modules/parser.md).
|
1774 | *
|
1775 | *
|
1776 | * ## chalk
|
1777 | *
|
1778 | * `chalk` npm module re-export
|
1779 | *
|
1780 | * **Usage**
|
1781 | *
|
1782 | * ```ts
|
1783 | * import { program, chalk } from "caporal"
|
1784 | *
|
1785 | * program
|
1786 | * .command('pay')
|
1787 | * .argument('<amount>', 'Amount to pay', Validator.NUMBER)
|
1788 | * .action(({logger, args}) => {
|
1789 | * logger.info("You paid $%s", chalk.red(args.amount))
|
1790 | * })
|
1791 | * [...]
|
1792 | * ```
|
1793 | *
|
1794 | *
|
1795 | * @packageDocumentation
|
1796 | * @module @caporal/core
|
1797 | */
|
1798 | import { Program } from "program/index";
|
1799 | export { Command } from "command/index";
|
1800 | export * from "types";
|
1801 | /**
|
1802 | * @ignore
|
1803 | */
|
1804 | export { default as chalk } from "chalk";
|
1805 | /**
|
1806 | * @ignore
|
1807 | */
|
1808 | export { parseArgv, parseLine } from "parser/index";
|
1809 | /**
|
1810 | * @ignore
|
1811 | */
|
1812 | export const program: Program;
|
1813 | /**
|
1814 | * @ignore
|
1815 | */
|
1816 | export default program;
|
1817 | /**
|
1818 | * @ignore
|
1819 | */
|
1820 | export { Program };
|
1821 | }
|
1822 | /// <amd-module name="@caporal/core" />
|
1823 | declare module "@caporal/core" {
|
1824 | /**
|
1825 | * @packageDocumentation
|
1826 | * @internal
|
1827 | */
|
1828 | export * from "index";
|
1829 | }
|
1830 | declare module "utils/web/autocomplete" {
|
1831 | /**
|
1832 | * Autocomplete mock for caporal-web
|
1833 | *
|
1834 | * @packageDocumentation
|
1835 | * @internal
|
1836 | */
|
1837 | import type { Program } from "program/index";
|
1838 | import type { Argument, Option } from "types";
|
1839 | export function registerCompletion(argOrOpt: Argument | Option, completer: Function): void;
|
1840 | export function installCompletion(program: Program): Promise<void>;
|
1841 | export function uninstallCompletion(program: Program): Promise<void>;
|
1842 | }
|
1843 | declare module "utils/web/process" {
|
1844 | /**
|
1845 | * A process mock for the web
|
1846 | *
|
1847 | * @packageDocumentation
|
1848 | * @internal
|
1849 | */
|
1850 | export const version: string;
|
1851 | export const argv: string[];
|
1852 | export const execArgv: never[];
|
1853 | export const exitCode = 0;
|
1854 | export const fake = true;
|
1855 | export const on: () => void;
|
1856 | export const once: () => void;
|
1857 | export const exit: (code?: number) => void;
|
1858 | }
|