commander 12.0.0 → 12.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +10 -8
- package/esm.mjs +1 -1
- package/lib/argument.js +9 -5
- package/lib/command.js +406 -196
- package/lib/error.js +0 -4
- package/lib/help.js +101 -38
- package/lib/option.js +11 -8
- package/lib/suggestSimilar.js +5 -4
- package/package.json +23 -19
- package/typings/index.d.ts +110 -40
package/typings/index.d.ts
CHANGED
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
// - https://github.com/microsoft/TypeScript/issues/29729
|
|
12
12
|
// - https://github.com/sindresorhus/type-fest/blob/main/source/literal-union.d.ts
|
|
13
13
|
// - https://github.com/sindresorhus/type-fest/blob/main/source/primitive.d.ts
|
|
14
|
-
type LiteralUnion<LiteralType, BaseType extends string | number> =
|
|
14
|
+
type LiteralUnion<LiteralType, BaseType extends string | number> =
|
|
15
|
+
| LiteralType
|
|
16
|
+
| (BaseType & Record<never, never>);
|
|
15
17
|
|
|
16
18
|
export class CommanderError extends Error {
|
|
17
19
|
code: string;
|
|
@@ -24,7 +26,6 @@ export class CommanderError extends Error {
|
|
|
24
26
|
* @param exitCode - suggested exit code which could be used with process.exit
|
|
25
27
|
* @param code - an id string representing the error
|
|
26
28
|
* @param message - human-readable description of the error
|
|
27
|
-
* @constructor
|
|
28
29
|
*/
|
|
29
30
|
constructor(exitCode: number, code: string, message: string);
|
|
30
31
|
}
|
|
@@ -33,13 +34,13 @@ export class InvalidArgumentError extends CommanderError {
|
|
|
33
34
|
/**
|
|
34
35
|
* Constructs the InvalidArgumentError class
|
|
35
36
|
* @param message - explanation of why argument is invalid
|
|
36
|
-
* @constructor
|
|
37
37
|
*/
|
|
38
38
|
constructor(message: string);
|
|
39
39
|
}
|
|
40
40
|
export { InvalidArgumentError as InvalidOptionArgumentError }; // deprecated old name
|
|
41
41
|
|
|
42
|
-
export interface ErrorOptions {
|
|
42
|
+
export interface ErrorOptions {
|
|
43
|
+
// optional parameter for error()
|
|
43
44
|
/** an id string representing the error */
|
|
44
45
|
code?: string;
|
|
45
46
|
/** suggested exit code which could be used with process.exit */
|
|
@@ -162,11 +163,6 @@ export class Option {
|
|
|
162
163
|
*/
|
|
163
164
|
env(name: string): this;
|
|
164
165
|
|
|
165
|
-
/**
|
|
166
|
-
* Calculate the full description, including defaultValue etc.
|
|
167
|
-
*/
|
|
168
|
-
fullDescription(): string;
|
|
169
|
-
|
|
170
166
|
/**
|
|
171
167
|
* Set the custom handler for processing CLI option arguments into option values.
|
|
172
168
|
*/
|
|
@@ -257,7 +253,12 @@ export class Help {
|
|
|
257
253
|
* Wrap the given string to width characters per line, with lines after the first indented.
|
|
258
254
|
* Do not wrap if insufficient room for wrapping (minColumnWidth), or string is manually formatted.
|
|
259
255
|
*/
|
|
260
|
-
wrap(
|
|
256
|
+
wrap(
|
|
257
|
+
str: string,
|
|
258
|
+
width: number,
|
|
259
|
+
indent: number,
|
|
260
|
+
minColumnWidth?: number,
|
|
261
|
+
): string;
|
|
261
262
|
|
|
262
263
|
/** Generate the built-in help text. */
|
|
263
264
|
formatHelp(cmd: Command, helper: Help): string;
|
|
@@ -267,10 +268,12 @@ export type HelpConfiguration = Partial<Help>;
|
|
|
267
268
|
export interface ParseOptions {
|
|
268
269
|
from: 'node' | 'electron' | 'user';
|
|
269
270
|
}
|
|
270
|
-
export interface HelpContext {
|
|
271
|
+
export interface HelpContext {
|
|
272
|
+
// optional parameter for .help() and .outputHelp()
|
|
271
273
|
error: boolean;
|
|
272
274
|
}
|
|
273
|
-
export interface AddHelpTextContext {
|
|
275
|
+
export interface AddHelpTextContext {
|
|
276
|
+
// passed to text function used with .addHelpText()
|
|
274
277
|
error: boolean;
|
|
275
278
|
command: Command;
|
|
276
279
|
}
|
|
@@ -280,13 +283,14 @@ export interface OutputConfiguration {
|
|
|
280
283
|
getOutHelpWidth?(): number;
|
|
281
284
|
getErrHelpWidth?(): number;
|
|
282
285
|
outputError?(str: string, write: (str: string) => void): void;
|
|
283
|
-
|
|
284
286
|
}
|
|
285
287
|
|
|
286
288
|
export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll';
|
|
287
289
|
export type HookEvent = 'preSubcommand' | 'preAction' | 'postAction';
|
|
288
290
|
// The source is a string so author can define their own too.
|
|
289
|
-
export type OptionValueSource =
|
|
291
|
+
export type OptionValueSource =
|
|
292
|
+
| LiteralUnion<'default' | 'config' | 'env' | 'cli' | 'implied', string>
|
|
293
|
+
| undefined;
|
|
290
294
|
|
|
291
295
|
export type OptionValues = Record<string, any>;
|
|
292
296
|
|
|
@@ -334,7 +338,10 @@ export class Command {
|
|
|
334
338
|
* @param opts - configuration options
|
|
335
339
|
* @returns new command
|
|
336
340
|
*/
|
|
337
|
-
command(
|
|
341
|
+
command(
|
|
342
|
+
nameAndArgs: string,
|
|
343
|
+
opts?: CommandOptions,
|
|
344
|
+
): ReturnType<this['createCommand']>;
|
|
338
345
|
/**
|
|
339
346
|
* Define a command, implemented in a separate executable file.
|
|
340
347
|
*
|
|
@@ -353,7 +360,11 @@ export class Command {
|
|
|
353
360
|
* @param opts - configuration options
|
|
354
361
|
* @returns `this` command for chaining
|
|
355
362
|
*/
|
|
356
|
-
command(
|
|
363
|
+
command(
|
|
364
|
+
nameAndArgs: string,
|
|
365
|
+
description: string,
|
|
366
|
+
opts?: ExecutableCommandOptions,
|
|
367
|
+
): this;
|
|
357
368
|
|
|
358
369
|
/**
|
|
359
370
|
* Factory routine to create a new unattached command.
|
|
@@ -394,7 +405,12 @@ export class Command {
|
|
|
394
405
|
*
|
|
395
406
|
* @returns `this` command for chaining
|
|
396
407
|
*/
|
|
397
|
-
argument<T>(
|
|
408
|
+
argument<T>(
|
|
409
|
+
flags: string,
|
|
410
|
+
description: string,
|
|
411
|
+
fn: (value: string, previous: T) => T,
|
|
412
|
+
defaultValue?: T,
|
|
413
|
+
): this;
|
|
398
414
|
argument(name: string, description?: string, defaultValue?: unknown): this;
|
|
399
415
|
|
|
400
416
|
/**
|
|
@@ -444,7 +460,13 @@ export class Command {
|
|
|
444
460
|
/**
|
|
445
461
|
* Add hook for life cycle event.
|
|
446
462
|
*/
|
|
447
|
-
hook(
|
|
463
|
+
hook(
|
|
464
|
+
event: HookEvent,
|
|
465
|
+
listener: (
|
|
466
|
+
thisCommand: Command,
|
|
467
|
+
actionCommand: Command,
|
|
468
|
+
) => void | Promise<void>,
|
|
469
|
+
): this;
|
|
448
470
|
|
|
449
471
|
/**
|
|
450
472
|
* Register callback to use as replacement for calling process.exit.
|
|
@@ -544,10 +566,24 @@ export class Command {
|
|
|
544
566
|
*
|
|
545
567
|
* @returns `this` command for chaining
|
|
546
568
|
*/
|
|
547
|
-
option(
|
|
548
|
-
|
|
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;
|
|
549
580
|
/** @deprecated since v7, instead use choices or a custom function */
|
|
550
|
-
option(
|
|
581
|
+
option(
|
|
582
|
+
flags: string,
|
|
583
|
+
description: string,
|
|
584
|
+
regexp: RegExp,
|
|
585
|
+
defaultValue?: string | boolean | string[],
|
|
586
|
+
): this;
|
|
551
587
|
|
|
552
588
|
/**
|
|
553
589
|
* Define a required option, which must have a value after parsing. This usually means
|
|
@@ -555,10 +591,24 @@ export class Command {
|
|
|
555
591
|
*
|
|
556
592
|
* The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.
|
|
557
593
|
*/
|
|
558
|
-
requiredOption(
|
|
559
|
-
|
|
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;
|
|
560
605
|
/** @deprecated since v7, instead use choices or a custom function */
|
|
561
|
-
requiredOption(
|
|
606
|
+
requiredOption(
|
|
607
|
+
flags: string,
|
|
608
|
+
description: string,
|
|
609
|
+
regexp: RegExp,
|
|
610
|
+
defaultValue?: string | boolean | string[],
|
|
611
|
+
): this;
|
|
562
612
|
|
|
563
613
|
/**
|
|
564
614
|
* Factory routine to create a new unattached option.
|
|
@@ -583,7 +633,9 @@ export class Command {
|
|
|
583
633
|
* @returns `this` command for chaining
|
|
584
634
|
*/
|
|
585
635
|
storeOptionsAsProperties<T extends OptionValues>(): this & T;
|
|
586
|
-
storeOptionsAsProperties<T extends OptionValues>(
|
|
636
|
+
storeOptionsAsProperties<T extends OptionValues>(
|
|
637
|
+
storeAsProperties: true,
|
|
638
|
+
): this & T;
|
|
587
639
|
storeOptionsAsProperties(storeAsProperties?: boolean): this;
|
|
588
640
|
|
|
589
641
|
/**
|
|
@@ -599,7 +651,11 @@ export class Command {
|
|
|
599
651
|
/**
|
|
600
652
|
* Store option value and where the value came from.
|
|
601
653
|
*/
|
|
602
|
-
setOptionValueWithSource(
|
|
654
|
+
setOptionValueWithSource(
|
|
655
|
+
key: string,
|
|
656
|
+
value: unknown,
|
|
657
|
+
source: OptionValueSource,
|
|
658
|
+
): this;
|
|
603
659
|
|
|
604
660
|
/**
|
|
605
661
|
* Get source of option value.
|
|
@@ -607,7 +663,7 @@ export class Command {
|
|
|
607
663
|
getOptionValueSource(key: string): OptionValueSource | undefined;
|
|
608
664
|
|
|
609
665
|
/**
|
|
610
|
-
|
|
666
|
+
* Get source of option value. See also .optsWithGlobals().
|
|
611
667
|
*/
|
|
612
668
|
getOptionValueSourceWithGlobals(key: string): OptionValueSource | undefined;
|
|
613
669
|
|
|
@@ -663,38 +719,49 @@ export class Command {
|
|
|
663
719
|
/**
|
|
664
720
|
* Parse `argv`, setting options and invoking commands when defined.
|
|
665
721
|
*
|
|
666
|
-
*
|
|
667
|
-
*
|
|
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
|
|
668
730
|
*
|
|
669
731
|
* @example
|
|
670
732
|
* ```
|
|
671
|
-
* program.parse(process.argv
|
|
672
|
-
* program.parse(); //
|
|
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
|
|
673
735
|
* program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
674
736
|
* ```
|
|
675
737
|
*
|
|
676
738
|
* @returns `this` command for chaining
|
|
677
739
|
*/
|
|
678
|
-
parse(argv?: readonly string[],
|
|
740
|
+
parse(argv?: readonly string[], parseOptions?: ParseOptions): this;
|
|
679
741
|
|
|
680
742
|
/**
|
|
681
743
|
* Parse `argv`, setting options and invoking commands when defined.
|
|
682
744
|
*
|
|
683
|
-
*
|
|
745
|
+
* Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
684
746
|
*
|
|
685
|
-
*
|
|
686
|
-
*
|
|
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
|
|
687
751
|
*
|
|
688
752
|
* @example
|
|
689
753
|
* ```
|
|
690
|
-
* program.parseAsync(process.argv
|
|
691
|
-
* program.parseAsync(); //
|
|
692
|
-
* program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
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]
|
|
693
757
|
* ```
|
|
694
758
|
*
|
|
695
759
|
* @returns Promise
|
|
696
760
|
*/
|
|
697
|
-
parseAsync(
|
|
761
|
+
parseAsync(
|
|
762
|
+
argv?: readonly string[],
|
|
763
|
+
parseOptions?: ParseOptions,
|
|
764
|
+
): Promise<this>;
|
|
698
765
|
|
|
699
766
|
/**
|
|
700
767
|
* Parse options from `argv` removing known options,
|
|
@@ -869,7 +936,10 @@ export class Command {
|
|
|
869
936
|
* and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.
|
|
870
937
|
*/
|
|
871
938
|
addHelpText(position: AddHelpTextPosition, text: string): this;
|
|
872
|
-
addHelpText(
|
|
939
|
+
addHelpText(
|
|
940
|
+
position: AddHelpTextPosition,
|
|
941
|
+
text: (context: AddHelpTextContext) => string,
|
|
942
|
+
): this;
|
|
873
943
|
|
|
874
944
|
/**
|
|
875
945
|
* Add a listener (callback) for when events occur. (Implemented using EventEmitter.)
|