1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | type LiteralUnion<LiteralType, BaseType extends string | number> =
|
15 | | LiteralType
|
16 | | (BaseType & Record<never, never>);
|
17 |
|
18 | export class CommanderError extends Error {
|
19 | code: string;
|
20 | exitCode: number;
|
21 | message: string;
|
22 | nestedError?: string;
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | constructor(exitCode: number, code: string, message: string);
|
31 | }
|
32 |
|
33 | export class InvalidArgumentError extends CommanderError {
|
34 | |
35 |
|
36 |
|
37 |
|
38 | constructor(message: string);
|
39 | }
|
40 | export { InvalidArgumentError as InvalidOptionArgumentError };
|
41 |
|
42 | export interface ErrorOptions {
|
43 |
|
44 |
|
45 | code?: string;
|
46 |
|
47 | exitCode?: number;
|
48 | }
|
49 |
|
50 | export class Argument {
|
51 | description: string;
|
52 | required: boolean;
|
53 | variadic: boolean;
|
54 | defaultValue?: any;
|
55 | defaultValueDescription?: string;
|
56 | argChoices?: string[];
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | constructor(arg: string, description?: string);
|
64 |
|
65 | /**
|
66 | * Return argument name.
|
67 | */
|
68 | name(): string;
|
69 |
|
70 | /**
|
71 | * Set the default value, and optionally supply the description to be displayed in the help.
|
72 | */
|
73 | default(value: unknown, description?: string): this;
|
74 |
|
75 | /**
|
76 | * Set the custom handler for processing CLI command arguments into argument values.
|
77 | */
|
78 | argParser<T>(fn: (value: string, previous: T) => T): this;
|
79 |
|
80 | /**
|
81 | * Only allow argument value to be one of choices.
|
82 | */
|
83 | choices(values: readonly string[]): this;
|
84 |
|
85 | /**
|
86 | * Make argument required.
|
87 | */
|
88 | argRequired(): this;
|
89 |
|
90 | /**
|
91 | * Make argument optional.
|
92 | */
|
93 | argOptional(): this;
|
94 | }
|
95 |
|
96 | export class Option {
|
97 | flags: string;
|
98 | description: string;
|
99 |
|
100 | required: boolean;
|
101 | optional: boolean;
|
102 | variadic: boolean;
|
103 | mandatory: boolean;
|
104 | short?: string;
|
105 | long?: string;
|
106 | negate: boolean;
|
107 | defaultValue?: any;
|
108 | defaultValueDescription?: string;
|
109 | presetArg?: unknown;
|
110 | envVar?: string;
|
111 | parseArg?: <T>(value: string, previous: T) => T;
|
112 | hidden: boolean;
|
113 | argChoices?: string[];
|
114 |
|
115 | constructor(flags: string, description?: string);
|
116 |
|
117 | /**
|
118 | * Set the default value, and optionally supply the description to be displayed in the help.
|
119 | */
|
120 | default(value: unknown, description?: string): this;
|
121 |
|
122 | /**
|
123 | * Preset to use when option used without option-argument, especially optional but also boolean and negated.
|
124 | * The custom processing (parseArg) is called.
|
125 | *
|
126 | * @example
|
127 | * ```ts
|
128 | * new Option('--color').default('GREYSCALE').preset('RGB');
|
129 | * new Option('--donate [amount]').preset('20').argParser(parseFloat);
|
130 | * ```
|
131 | */
|
132 | preset(arg: unknown): this;
|
133 |
|
134 | /**
|
135 | * Add option name(s) that conflict with this option.
|
136 | * An error will be displayed if conflicting options are found during parsing.
|
137 | *
|
138 | * @example
|
139 | * ```ts
|
140 | * new Option('--rgb').conflicts('cmyk');
|
141 | * new Option('--js').conflicts(['ts', 'jsx']);
|
142 | * ```
|
143 | */
|
144 | conflicts(names: string | string[]): this;
|
145 |
|
146 | /**
|
147 | * Specify implied option values for when this option is set and the implied options are not.
|
148 | *
|
149 | * The custom processing (parseArg) is not called on the implied values.
|
150 | *
|
151 | * @example
|
152 | * program
|
153 | * .addOption(new Option('--log', 'write logging information to file'))
|
154 | * .addOption(new Option('--trace', 'log extra details').implies({ log: 'trace.txt' }));
|
155 | */
|
156 | implies(optionValues: OptionValues): this;
|
157 |
|
158 | /**
|
159 | * Set environment variable to check for option value.
|
160 | *
|
161 | * An environment variables is only used if when processed the current option value is
|
162 | * undefined, or the source of the current value is 'default' or 'config' or 'env'.
|
163 | */
|
164 | env(name: string): this;
|
165 |
|
166 | /**
|
167 | * Set the custom handler for processing CLI option arguments into option values.
|
168 | */
|
169 | argParser<T>(fn: (value: string, previous: T) => T): this;
|
170 |
|
171 | /**
|
172 | * Whether the option is mandatory and must have a value after parsing.
|
173 | */
|
174 | makeOptionMandatory(mandatory?: boolean): this;
|
175 |
|
176 | /**
|
177 | * Hide option in help.
|
178 | */
|
179 | hideHelp(hide?: boolean): this;
|
180 |
|
181 | /**
|
182 | * Only allow option value to be one of choices.
|
183 | */
|
184 | choices(values: readonly string[]): this;
|
185 |
|
186 | /**
|
187 | * Return option name.
|
188 | */
|
189 | name(): string;
|
190 |
|
191 | /**
|
192 | * Return option name, in a camelcase format that can be used
|
193 | * as a object attribute key.
|
194 | */
|
195 | attributeName(): string;
|
196 |
|
197 | /**
|
198 | * Return whether a boolean option.
|
199 | *
|
200 | * Options are one of boolean, negated, required argument, or optional argument.
|
201 | */
|
202 | isBoolean(): boolean;
|
203 | }
|
204 |
|
205 | export class Help {
|
206 |
|
207 | helpWidth?: number;
|
208 | sortSubcommands: boolean;
|
209 | sortOptions: boolean;
|
210 | showGlobalOptions: boolean;
|
211 |
|
212 | constructor();
|
213 |
|
214 | /** Get the command term to show in the list of subcommands. */
|
215 | subcommandTerm(cmd: Command): string;
|
216 | /** Get the command summary to show in the list of subcommands. */
|
217 | subcommandDescription(cmd: Command): string;
|
218 | /** Get the option term to show in the list of options. */
|
219 | optionTerm(option: Option): string;
|
220 | /** Get the option description to show in the list of options. */
|
221 | optionDescription(option: Option): string;
|
222 | /** Get the argument term to show in the list of arguments. */
|
223 | argumentTerm(argument: Argument): string;
|
224 | /** Get the argument description to show in the list of arguments. */
|
225 | argumentDescription(argument: Argument): string;
|
226 |
|
227 | /** Get the command usage to be displayed at the top of the built-in help. */
|
228 | commandUsage(cmd: Command): string;
|
229 | /** Get the description for the command. */
|
230 | commandDescription(cmd: Command): string;
|
231 |
|
232 | /** Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one. */
|
233 | visibleCommands(cmd: Command): Command[];
|
234 | /** Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one. */
|
235 | visibleOptions(cmd: Command): Option[];
|
236 | /** Get an array of the visible global options. (Not including help.) */
|
237 | visibleGlobalOptions(cmd: Command): Option[];
|
238 | /** Get an array of the arguments which have descriptions. */
|
239 | visibleArguments(cmd: Command): Argument[];
|
240 |
|
241 | /** Get the longest command term length. */
|
242 | longestSubcommandTermLength(cmd: Command, helper: Help): number;
|
243 | /** Get the longest option term length. */
|
244 | longestOptionTermLength(cmd: Command, helper: Help): number;
|
245 | /** Get the longest global option term length. */
|
246 | longestGlobalOptionTermLength(cmd: Command, helper: Help): number;
|
247 | /** Get the longest argument term length. */
|
248 | longestArgumentTermLength(cmd: Command, helper: Help): number;
|
249 | /** Calculate the pad width from the maximum term length. */
|
250 | padWidth(cmd: Command, helper: Help): number;
|
251 |
|
252 | /**
|
253 | * Wrap the given string to width characters per line, with lines after the first indented.
|
254 | * Do not wrap if insufficient room for wrapping (minColumnWidth), or string is manually formatted.
|
255 | */
|
256 | wrap(
|
257 | str: string,
|
258 | width: number,
|
259 | indent: number,
|
260 | minColumnWidth?: number,
|
261 | ): string;
|
262 |
|
263 | /** Generate the built-in help text. */
|
264 | formatHelp(cmd: Command, helper: Help): string;
|
265 | }
|
266 | export type HelpConfiguration = Partial<Help>;
|
267 |
|
268 | export interface ParseOptions {
|
269 | from: 'node' | 'electron' | 'user';
|
270 | }
|
271 | export interface HelpContext {
|
272 |
|
273 | error: boolean;
|
274 | }
|
275 | export interface AddHelpTextContext {
|
276 |
|
277 | error: boolean;
|
278 | command: Command;
|
279 | }
|
280 | export interface OutputConfiguration {
|
281 | writeOut?(str: string): void;
|
282 | writeErr?(str: string): void;
|
283 | getOutHelpWidth?(): number;
|
284 | getErrHelpWidth?(): number;
|
285 | outputError?(str: string, write: (str: string) => void): void;
|
286 | }
|
287 |
|
288 | export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll';
|
289 | export type HookEvent = 'preSubcommand' | 'preAction' | 'postAction';
|
290 | // The source is a string so author can define their own too.
|
291 | export type OptionValueSource =
|
292 | | LiteralUnion<'default' | 'config' | 'env' | 'cli' | 'implied', string>
|
293 | | undefined;
|
294 |
|
295 | export type OptionValues = Record<string, any>;
|
296 |
|
297 | export class Command {
|
298 | args: string[];
|
299 | processedArgs: any[];
|
300 | readonly commands: readonly Command[];
|
301 | readonly options: readonly Option[];
|
302 | readonly registeredArguments: readonly Argument[];
|
303 | parent: Command | null;
|
304 |
|
305 | constructor(name?: string);
|
306 |
|
307 | /**
|
308 | * Set the program version to `str`.
|
309 | *
|
310 | * This method auto-registers the "-V, --version" flag
|
311 | * which will print the version number when passed.
|
312 | *
|
313 | * You can optionally supply the flags and description to override the defaults.
|
314 | */
|
315 | version(str: string, flags?: string, description?: string): this;
|
316 | /**
|
317 | * Get the program version.
|
318 | */
|
319 | version(): string | undefined;
|
320 |
|
321 | /**
|
322 | * Define a command, implemented using an action handler.
|
323 | *
|
324 | * @remarks
|
325 | * The command description is supplied using `.description`, not as a parameter to `.command`.
|
326 | *
|
327 | * @example
|
328 | * ```ts
|
329 | * program
|
330 | * .command('clone <source> [destination]')
|
331 | * .description('clone a repository into a newly created directory')
|
332 | * .action((source, destination) => {
|
333 | * console.log('clone command called');
|
334 | * });
|
335 | * ```
|
336 | *
|
337 | * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
338 | * @param opts - configuration options
|
339 | * @returns new command
|
340 | */
|
341 | command(
|
342 | nameAndArgs: string,
|
343 | opts?: CommandOptions,
|
344 | ): ReturnType<this['createCommand']>;
|
345 | /**
|
346 | * Define a command, implemented in a separate executable file.
|
347 | *
|
348 | * @remarks
|
349 | * The command description is supplied as the second parameter to `.command`.
|
350 | *
|
351 | * @example
|
352 | * ```ts
|
353 | * program
|
354 | * .command('start <service>', 'start named service')
|
355 | * .command('stop [service]', 'stop named service, or all if no name supplied');
|
356 | * ```
|
357 | *
|
358 | * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
359 | * @param description - description of executable command
|
360 | * @param opts - configuration options
|
361 | * @returns `this` command for chaining
|
362 | */
|
363 | command(
|
364 | nameAndArgs: string,
|
365 | description: string,
|
366 | opts?: ExecutableCommandOptions,
|
367 | ): this;
|
368 |
|
369 | /**
|
370 | * Factory routine to create a new unattached command.
|
371 | *
|
372 | * See .command() for creating an attached subcommand, which uses this routine to
|
373 | * create the command. You can override createCommand to customise subcommands.
|
374 | */
|
375 | createCommand(name?: string): Command;
|
376 |
|
377 | /**
|
378 | * Add a prepared subcommand.
|
379 | *
|
380 | * See .command() for creating an attached subcommand which inherits settings from its parent.
|
381 | *
|
382 | * @returns `this` command for chaining
|
383 | */
|
384 | addCommand(cmd: Command, opts?: CommandOptions): this;
|
385 |
|
386 | /**
|
387 | * Factory routine to create a new unattached argument.
|
388 | *
|
389 | * See .argument() for creating an attached argument, which uses this routine to
|
390 | * create the argument. You can override createArgument to return a custom argument.
|
391 | */
|
392 | createArgument(name: string, description?: string): Argument;
|
393 |
|
394 | /**
|
395 | * Define argument syntax for command.
|
396 | *
|
397 | * The default is that the argument is required, and you can explicitly
|
398 | * indicate this with <> around the name. Put [] around the name for an optional argument.
|
399 | *
|
400 | * @example
|
401 | * ```
|
402 | * program.argument('<input-file>');
|
403 | * program.argument('[output-file]');
|
404 | * ```
|
405 | *
|
406 | * @returns `this` command for chaining
|
407 | */
|
408 | argument<T>(
|
409 | flags: string,
|
410 | description: string,
|
411 | fn: (value: string, previous: T) => T,
|
412 | defaultValue?: T,
|
413 | ): this;
|
414 | argument(name: string, description?: string, defaultValue?: unknown): this;
|
415 |
|
416 | /**
|
417 | * Define argument syntax for command, adding a prepared argument.
|
418 | *
|
419 | * @returns `this` command for chaining
|
420 | */
|
421 | addArgument(arg: Argument): this;
|
422 |
|
423 | /**
|
424 | * Define argument syntax for command, adding multiple at once (without descriptions).
|
425 | *
|
426 | * See also .argument().
|
427 | *
|
428 | * @example
|
429 | * ```
|
430 | * program.arguments('<cmd> [env]');
|
431 | * ```
|
432 | *
|
433 | * @returns `this` command for chaining
|
434 | */
|
435 | arguments(names: string): this;
|
436 |
|
437 | /**
|
438 | * Customise or override default help command. By default a help command is automatically added if your command has subcommands.
|
439 | *
|
440 | * @example
|
441 | * ```ts
|
442 | * program.helpCommand('help [cmd]');
|
443 | * program.helpCommand('help [cmd]', 'show help');
|
444 | * program.helpCommand(false); // suppress default help command
|
445 | * program.helpCommand(true); // add help command even if no subcommands
|
446 | * ```
|
447 | */
|
448 | helpCommand(nameAndArgs: string, description?: string): this;
|
449 | helpCommand(enable: boolean): this;
|
450 |
|
451 | /**
|
452 | * Add prepared custom help command.
|
453 | */
|
454 | addHelpCommand(cmd: Command): this;
|
455 | /** @deprecated since v12, instead use helpCommand */
|
456 | addHelpCommand(nameAndArgs: string, description?: string): this;
|
457 | /** @deprecated since v12, instead use helpCommand */
|
458 | addHelpCommand(enable?: boolean): this;
|
459 |
|
460 | /**
|
461 | * Add hook for life cycle event.
|
462 | */
|
463 | hook(
|
464 | event: HookEvent,
|
465 | listener: (
|
466 | thisCommand: Command,
|
467 | actionCommand: Command,
|
468 | ) => void | Promise<void>,
|
469 | ): this;
|
470 |
|
471 | /**
|
472 | * Register callback to use as replacement for calling process.exit.
|
473 | */
|
474 | exitOverride(callback?: (err: CommanderError) => never | void): this;
|
475 |
|
476 | /**
|
477 | * Display error message and exit (or call exitOverride).
|
478 | */
|
479 | error(message: string, errorOptions?: ErrorOptions): never;
|
480 |
|
481 | /**
|
482 | * You can customise the help with a subclass of Help by overriding createHelp,
|
483 | * or by overriding Help properties using configureHelp().
|
484 | */
|
485 | createHelp(): Help;
|
486 |
|
487 | /**
|
488 | * You can customise the help by overriding Help properties using configureHelp(),
|
489 | * or with a subclass of Help by overriding createHelp().
|
490 | */
|
491 | configureHelp(configuration: HelpConfiguration): this;
|
492 | /** Get configuration */
|
493 | configureHelp(): HelpConfiguration;
|
494 |
|
495 | /**
|
496 | * The default output goes to stdout and stderr. You can customise this for special
|
497 | * applications. You can also customise the display of errors by overriding outputError.
|
498 | *
|
499 | * The configuration properties are all functions:
|
500 | * ```
|
501 | * // functions to change where being written, stdout and stderr
|
502 | * writeOut(str)
|
503 | * writeErr(str)
|
504 | * // matching functions to specify width for wrapping help
|
505 | * getOutHelpWidth()
|
506 | * getErrHelpWidth()
|
507 | * // functions based on what is being written out
|
508 | * outputError(str, write) // used for displaying errors, and not used for displaying help
|
509 | * ```
|
510 | */
|
511 | configureOutput(configuration: OutputConfiguration): this;
|
512 | /** Get configuration */
|
513 | configureOutput(): OutputConfiguration;
|
514 |
|
515 | /**
|
516 | * Copy settings that are useful to have in common across root command and subcommands.
|
517 | *
|
518 | * (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)
|
519 | */
|
520 | copyInheritedSettings(sourceCommand: Command): this;
|
521 |
|
522 | /**
|
523 | * Display the help or a custom message after an error occurs.
|
524 | */
|
525 | showHelpAfterError(displayHelp?: boolean | string): this;
|
526 |
|
527 | /**
|
528 | * Display suggestion of similar commands for unknown commands, or options for unknown options.
|
529 | */
|
530 | showSuggestionAfterError(displaySuggestion?: boolean): this;
|
531 |
|
532 | /**
|
533 | * Register callback `fn` for the command.
|
534 | *
|
535 | * @example
|
536 | * ```
|
537 | * program
|
538 | * .command('serve')
|
539 | * .description('start service')
|
540 | * .action(function() {
|
541 | *
|
542 | * });
|
543 | * ```
|
544 | *
|
545 | * @returns `this` command for chaining
|
546 | */
|
547 | action(fn: (...args: any[]) => void | Promise<void>): this;
|
548 |
|
549 | /**
|
550 | * Define option with `flags`, `description`, and optional argument parsing function or `defaultValue` or both.
|
551 | *
|
552 | * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space. A required
|
553 | * option-argument is indicated by `<>` and an optional option-argument by `[]`.
|
554 | *
|
555 | * See the README for more details, and see also addOption() and requiredOption().
|
556 | *
|
557 | * @example
|
558 | *
|
559 | * ```js
|
560 | * program
|
561 | * .option('-p, --pepper', 'add pepper')
|
562 | * .option('-p, --pizza-type <TYPE>', 'type of pizza') // required option-argument
|
563 | * .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default
|
564 | * .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function
|
565 | * ```
|
566 | *
|
567 | * @returns `this` command for chaining
|
568 | */
|
569 | option(
|
570 | flags: string,
|
571 | description?: string,
|
572 | defaultValue?: string | boolean | string[],
|
573 | ): this;
|
574 | option<T>(
|
575 | flags: string,
|
576 | description: string,
|
577 | parseArg: (value: string, previous: T) => T,
|
578 | defaultValue?: T,
|
579 | ): this;
|
580 | /** @deprecated since v7, instead use choices or a custom function */
|
581 | option(
|
582 | flags: string,
|
583 | description: string,
|
584 | regexp: RegExp,
|
585 | defaultValue?: string | boolean | string[],
|
586 | ): this;
|
587 |
|
588 | /**
|
589 | * Define a required option, which must have a value after parsing. This usually means
|
590 | * the option must be specified on the command line. (Otherwise the same as .option().)
|
591 | *
|
592 | * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
|
593 | */
|
594 | requiredOption(
|
595 | flags: string,
|
596 | description?: string,
|
597 | defaultValue?: string | boolean | string[],
|
598 | ): this;
|
599 | requiredOption<T>(
|
600 | flags: string,
|
601 | description: string,
|
602 | parseArg: (value: string, previous: T) => T,
|
603 | defaultValue?: T,
|
604 | ): this;
|
605 | /** @deprecated since v7, instead use choices or a custom function */
|
606 | requiredOption(
|
607 | flags: string,
|
608 | description: string,
|
609 | regexp: RegExp,
|
610 | defaultValue?: string | boolean | string[],
|
611 | ): this;
|
612 |
|
613 | /**
|
614 | * Factory routine to create a new unattached option.
|
615 | *
|
616 | * See .option() for creating an attached option, which uses this routine to
|
617 | * create the option. You can override createOption to return a custom option.
|
618 | */
|
619 |
|
620 | createOption(flags: string, description?: string): Option;
|
621 |
|
622 | /**
|
623 | * Add a prepared Option.
|
624 | *
|
625 | * See .option() and .requiredOption() for creating and attaching an option in a single call.
|
626 | */
|
627 | addOption(option: Option): this;
|
628 |
|
629 | /**
|
630 | * Whether to store option values as properties on command object,
|
631 | * or store separately (specify false). In both cases the option values can be accessed using .opts().
|
632 | *
|
633 | * @returns `this` command for chaining
|
634 | */
|
635 | storeOptionsAsProperties<T extends OptionValues>(): this & T;
|
636 | storeOptionsAsProperties<T extends OptionValues>(
|
637 | storeAsProperties: true,
|
638 | ): this & T;
|
639 | storeOptionsAsProperties(storeAsProperties?: boolean): this;
|
640 |
|
641 | /**
|
642 | * Retrieve option value.
|
643 | */
|
644 | getOptionValue(key: string): any;
|
645 |
|
646 | /**
|
647 | * Store option value.
|
648 | */
|
649 | setOptionValue(key: string, value: unknown): this;
|
650 |
|
651 | /**
|
652 | * Store option value and where the value came from.
|
653 | */
|
654 | setOptionValueWithSource(
|
655 | key: string,
|
656 | value: unknown,
|
657 | source: OptionValueSource,
|
658 | ): this;
|
659 |
|
660 | /**
|
661 | * Get source of option value.
|
662 | */
|
663 | getOptionValueSource(key: string): OptionValueSource | undefined;
|
664 |
|
665 | /**
|
666 | * Get source of option value. See also .optsWithGlobals().
|
667 | */
|
668 | getOptionValueSourceWithGlobals(key: string): OptionValueSource | undefined;
|
669 |
|
670 | /**
|
671 | * Alter parsing of short flags with optional values.
|
672 | *
|
673 | * @example
|
674 | * ```
|
675 | * // for `.option('-f,--flag [value]'):
|
676 | * .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
|
677 | * .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
|
678 | * ```
|
679 | *
|
680 | * @returns `this` command for chaining
|
681 | */
|
682 | combineFlagAndOptionalValue(combine?: boolean): this;
|
683 |
|
684 | /**
|
685 | * Allow unknown options on the command line.
|
686 | *
|
687 | * @returns `this` command for chaining
|
688 | */
|
689 | allowUnknownOption(allowUnknown?: boolean): this;
|
690 |
|
691 | /**
|
692 | * Allow excess command-arguments on the command line. Pass false to make excess arguments an error.
|
693 | *
|
694 | * @returns `this` command for chaining
|
695 | */
|
696 | allowExcessArguments(allowExcess?: boolean): this;
|
697 |
|
698 | /**
|
699 | * Enable positional options. Positional means global options are specified before subcommands which lets
|
700 | * subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.
|
701 | *
|
702 | * The default behaviour is non-positional and global options may appear anywhere on the command line.
|
703 | *
|
704 | * @returns `this` command for chaining
|
705 | */
|
706 | enablePositionalOptions(positional?: boolean): this;
|
707 |
|
708 | /**
|
709 | * Pass through options that come after command-arguments rather than treat them as command-options,
|
710 | * so actual command-options come before command-arguments. Turning this on for a subcommand requires
|
711 | * positional options to have been enabled on the program (parent commands).
|
712 | *
|
713 | * The default behaviour is non-positional and options may appear before or after command-arguments.
|
714 | *
|
715 | * @returns `this` command for chaining
|
716 | */
|
717 | passThroughOptions(passThrough?: boolean): this;
|
718 |
|
719 | /**
|
720 | * Parse `argv`, setting options and invoking commands when defined.
|
721 | *
|
722 | * Use parseAsync instead of parse if any of your action handlers are async.
|
723 | *
|
724 | * Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
725 | *
|
726 | * Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
727 | * - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
728 | * - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
729 | * - `'user'`: just user arguments
|
730 | *
|
731 | * @example
|
732 | * ```
|
733 | * program.parse(); // parse process.argv and auto-detect electron and special node flags
|
734 | * program.parse(process.argv); // assume argv[0] is app and argv[1] is script
|
735 | * program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
736 | * ```
|
737 | *
|
738 | * @returns `this` command for chaining
|
739 | */
|
740 | parse(argv?: readonly string[], parseOptions?: ParseOptions): this;
|
741 |
|
742 | /**
|
743 | * Parse `argv`, setting options and invoking commands when defined.
|
744 | *
|
745 | * Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
746 | *
|
747 | * Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
748 | * - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
749 | * - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
750 | * - `'user'`: just user arguments
|
751 | *
|
752 | * @example
|
753 | * ```
|
754 | * await program.parseAsync(); // parse process.argv and auto-detect electron and special node flags
|
755 | * await program.parseAsync(process.argv); // assume argv[0] is app and argv[1] is script
|
756 | * await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
757 | * ```
|
758 | *
|
759 | * @returns Promise
|
760 | */
|
761 | parseAsync(
|
762 | argv?: readonly string[],
|
763 | parseOptions?: ParseOptions,
|
764 | ): Promise<this>;
|
765 |
|
766 | /**
|
767 | * Parse options from `argv` removing known options,
|
768 | * and return argv split into operands and unknown arguments.
|
769 | *
|
770 | * argv => operands, unknown
|
771 | * --known kkk op => [op], []
|
772 | * op --known kkk => [op], []
|
773 | * sub --unknown uuu op => [sub], [--unknown uuu op]
|
774 | * sub -- --unknown uuu op => [sub --unknown uuu op], []
|
775 | */
|
776 | parseOptions(argv: string[]): ParseOptionsResult;
|
777 |
|
778 | |
779 |
|
780 |
|
781 | opts<T extends OptionValues>(): T;
|
782 |
|
783 | |
784 |
|
785 |
|
786 | optsWithGlobals<T extends OptionValues>(): T;
|
787 |
|
788 | |
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 | description(str: string): this;
|
795 |
|
796 | description(str: string, argsDescription: Record<string, string>): this;
|
797 | |
798 |
|
799 |
|
800 | description(): string;
|
801 |
|
802 | |
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 | summary(str: string): this;
|
809 | |
810 |
|
811 |
|
812 | summary(): string;
|
813 |
|
814 | |
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 | alias(alias: string): this;
|
822 | |
823 |
|
824 |
|
825 | alias(): string;
|
826 |
|
827 | |
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 | aliases(aliases: readonly string[]): this;
|
835 | |
836 |
|
837 |
|
838 | aliases(): string[];
|
839 |
|
840 | |
841 |
|
842 |
|
843 |
|
844 |
|
845 | usage(str: string): this;
|
846 | |
847 |
|
848 |
|
849 | usage(): string;
|
850 |
|
851 | |
852 |
|
853 |
|
854 |
|
855 |
|
856 | name(str: string): this;
|
857 | |
858 |
|
859 |
|
860 | name(): string;
|
861 |
|
862 | |
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 | nameFromFilename(filename: string): this;
|
876 |
|
877 | |
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 | executableDir(path: string): this;
|
890 | |
891 |
|
892 |
|
893 | executableDir(): string | null;
|
894 |
|
895 | |
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 | outputHelp(context?: HelpContext): void;
|
902 |
|
903 | outputHelp(cb?: (str: string) => string): void;
|
904 |
|
905 | |
906 |
|
907 |
|
908 | helpInformation(context?: HelpContext): string;
|
909 |
|
910 | |
911 |
|
912 |
|
913 |
|
914 |
|
915 | helpOption(flags?: string | boolean, description?: string): this;
|
916 |
|
917 | |
918 |
|
919 |
|
920 |
|
921 | addHelpOption(option: Option): this;
|
922 |
|
923 | |
924 |
|
925 |
|
926 |
|
927 |
|
928 | help(context?: HelpContext): never;
|
929 |
|
930 | help(cb?: (str: string) => string): never;
|
931 |
|
932 | |
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 | addHelpText(position: AddHelpTextPosition, text: string): this;
|
939 | addHelpText(
|
940 | position: AddHelpTextPosition,
|
941 | text: (context: AddHelpTextContext) => string,
|
942 | ): this;
|
943 |
|
944 | |
945 |
|
946 |
|
947 | on(event: string | symbol, listener: (...args: any[]) => void): this;
|
948 | }
|
949 |
|
950 | export interface CommandOptions {
|
951 | hidden?: boolean;
|
952 | isDefault?: boolean;
|
953 |
|
954 | noHelp?: boolean;
|
955 | }
|
956 | export interface ExecutableCommandOptions extends CommandOptions {
|
957 | executableFile?: string;
|
958 | }
|
959 |
|
960 | export interface ParseOptionsResult {
|
961 | operands: string[];
|
962 | unknown: string[];
|
963 | }
|
964 |
|
965 | export function createCommand(name?: string): Command;
|
966 | export function createOption(flags: string, description?: string): Option;
|
967 | export function createArgument(name: string, description?: string): Argument;
|
968 |
|
969 | export const program: Command;
|