commander 8.0.0 → 9.0.0-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 +69 -43
- package/lib/argument.js +17 -1
- package/lib/command.js +357 -205
- package/lib/help.js +16 -4
- package/lib/option.js +45 -0
- package/lib/suggestSimilar.js +100 -0
- package/package.json +12 -6
- package/typings/index.d.ts +186 -91
package/Readme.md
CHANGED
|
@@ -32,7 +32,8 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
|
|
|
32
32
|
- [Custom help](#custom-help)
|
|
33
33
|
- [Display help after errors](#display-help-after-errors)
|
|
34
34
|
- [Display help from code](#display-help-from-code)
|
|
35
|
-
- [.
|
|
35
|
+
- [.name](#name)
|
|
36
|
+
- [.usage](#usage)
|
|
36
37
|
- [.helpOption(flags, description)](#helpoptionflags-description)
|
|
37
38
|
- [.addHelpCommand()](#addhelpcommand)
|
|
38
39
|
- [More configuration](#more-configuration-2)
|
|
@@ -65,41 +66,37 @@ Commander exports a global object which is convenient for quick programs.
|
|
|
65
66
|
This is used in the examples in this README for brevity.
|
|
66
67
|
|
|
67
68
|
```js
|
|
69
|
+
// CommonJS (.cjs)
|
|
68
70
|
const { program } = require('commander');
|
|
69
|
-
program.version('0.0.1');
|
|
70
71
|
```
|
|
71
72
|
|
|
72
73
|
For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.
|
|
73
74
|
|
|
74
75
|
```js
|
|
76
|
+
// CommonJS (.cjs)
|
|
75
77
|
const { Command } = require('commander');
|
|
76
78
|
const program = new Command();
|
|
77
|
-
program.version('0.0.1');
|
|
78
79
|
```
|
|
79
80
|
|
|
80
|
-
For named imports in ECMAScript modules, import from `commander/esm.mjs`.
|
|
81
|
-
|
|
82
81
|
```js
|
|
83
|
-
//
|
|
84
|
-
import { Command } from 'commander
|
|
82
|
+
// ECMAScript (.mjs)
|
|
83
|
+
import { Command } from 'commander';
|
|
85
84
|
const program = new Command();
|
|
86
85
|
```
|
|
87
86
|
|
|
88
|
-
And in TypeScript:
|
|
89
|
-
|
|
90
87
|
```ts
|
|
91
|
-
//
|
|
88
|
+
// TypeScript (.ts)
|
|
92
89
|
import { Command } from 'commander';
|
|
93
90
|
const program = new Command();
|
|
94
91
|
```
|
|
95
92
|
|
|
96
|
-
|
|
97
93
|
## Options
|
|
98
94
|
|
|
99
95
|
Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space or vertical bar ('|').
|
|
100
96
|
|
|
101
97
|
The parsed options can be accessed by calling `.opts()` on a `Command` object, and are passed to the action handler.
|
|
102
|
-
You can also use `.getOptionValue()` and `.setOptionValue()` to work with a single option value
|
|
98
|
+
(You can also use `.getOptionValue()` and `.setOptionValue()` to work with a single option value,
|
|
99
|
+
and `.getOptionValueSource()` and `.setOptionValueWithSource()` when it matters where the option value came from.)
|
|
103
100
|
|
|
104
101
|
Multi-word options such as "--template-engine" are camel-cased, becoming `program.opts().templateEngine` etc.
|
|
105
102
|
|
|
@@ -113,7 +110,7 @@ By default options on the command line are not positional, and can be specified
|
|
|
113
110
|
### Common option types, boolean and value
|
|
114
111
|
|
|
115
112
|
The two most used option types are a boolean option, and an option which takes its value
|
|
116
|
-
from the following argument (declared with angle brackets like `--expect <value>`). Both are `undefined` unless specified on command line.
|
|
113
|
+
from the following argument (declared with angle brackets like `--expect <value>`). Both are `undefined` unless specified on command line.
|
|
117
114
|
|
|
118
115
|
Example file: [options-common.js](./examples/options-common.js)
|
|
119
116
|
|
|
@@ -149,7 +146,7 @@ pizza details:
|
|
|
149
146
|
|
|
150
147
|
### Default option value
|
|
151
148
|
|
|
152
|
-
You can specify a default value for an option
|
|
149
|
+
You can specify a default value for an option.
|
|
153
150
|
|
|
154
151
|
Example file: [options-defaults.js](./examples/options-defaults.js)
|
|
155
152
|
|
|
@@ -175,7 +172,7 @@ You can define a boolean option long name with a leading `no-` to set the option
|
|
|
175
172
|
Defined alone this also makes the option true by default.
|
|
176
173
|
|
|
177
174
|
If you define `--foo` first, adding `--no-foo` does not change the default value from what it would
|
|
178
|
-
otherwise be.
|
|
175
|
+
otherwise be.
|
|
179
176
|
|
|
180
177
|
Example file: [options-negatable.js](./examples/options-negatable.js)
|
|
181
178
|
|
|
@@ -308,13 +305,15 @@ program.version('0.0.1', '-v, --vers', 'output the current version');
|
|
|
308
305
|
You can add most options using the `.option()` method, but there are some additional features available
|
|
309
306
|
by constructing an `Option` explicitly for less common cases.
|
|
310
307
|
|
|
311
|
-
Example
|
|
308
|
+
Example files: [options-extra.js](./examples/options-extra.js), [options-env.js](./examples/options-env.js)
|
|
312
309
|
|
|
313
310
|
```js
|
|
314
311
|
program
|
|
315
312
|
.addOption(new Option('-s, --secret').hideHelp())
|
|
316
313
|
.addOption(new Option('-t, --timeout <delay>', 'timeout in seconds').default(60, 'one minute'))
|
|
317
|
-
.addOption(new Option('-d, --drink <size>', 'drink size').choices(['small', 'medium', 'large']))
|
|
314
|
+
.addOption(new Option('-d, --drink <size>', 'drink size').choices(['small', 'medium', 'large']))
|
|
315
|
+
.addOption(new Option('-p, --port <number>', 'port number').env('PORT'))
|
|
316
|
+
.addOption(new Option('--donate [amount]', 'optional donation in dollars').preset('20').argParser(parseFloat));
|
|
318
317
|
```
|
|
319
318
|
|
|
320
319
|
```bash
|
|
@@ -324,10 +323,15 @@ Usage: help [options]
|
|
|
324
323
|
Options:
|
|
325
324
|
-t, --timeout <delay> timeout in seconds (default: one minute)
|
|
326
325
|
-d, --drink <size> drink cup size (choices: "small", "medium", "large")
|
|
326
|
+
-p, --port <number> port number (env: PORT)
|
|
327
|
+
--donate [amount] optional donation in dollars (preset: 20)
|
|
327
328
|
-h, --help display help for command
|
|
328
329
|
|
|
329
330
|
$ extra --drink huge
|
|
330
331
|
error: option '-d, --drink <size>' argument 'huge' is invalid. Allowed choices are small, medium, large.
|
|
332
|
+
|
|
333
|
+
$ PORT=80 extra --donate
|
|
334
|
+
Options: { timeout: 60, donate: 20, port: '80' }
|
|
331
335
|
```
|
|
332
336
|
|
|
333
337
|
### Custom option processing
|
|
@@ -426,7 +430,7 @@ program
|
|
|
426
430
|
.addCommand(build.makeBuildCommand());
|
|
427
431
|
```
|
|
428
432
|
|
|
429
|
-
Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will
|
|
433
|
+
Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will
|
|
430
434
|
remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other
|
|
431
435
|
subcommand is specified ([example](./examples/defaultCommand.js)).
|
|
432
436
|
|
|
@@ -436,7 +440,7 @@ For subcommands, you can specify the argument syntax in the call to `.command()`
|
|
|
436
440
|
is the only method usable for subcommands implemented using a stand-alone executable, but for other subcommands
|
|
437
441
|
you can instead use the following method.
|
|
438
442
|
|
|
439
|
-
To configure a command, you can use `.argument()` to specify each expected command-argument.
|
|
443
|
+
To configure a command, you can use `.argument()` to specify each expected command-argument.
|
|
440
444
|
You supply the argument name and an optional description. The argument may be `<required>` or `[optional]`.
|
|
441
445
|
You can specify a default value for an optional command-argument.
|
|
442
446
|
|
|
@@ -513,7 +517,7 @@ program
|
|
|
513
517
|
### Action handler
|
|
514
518
|
|
|
515
519
|
The action handler gets passed a parameter for each command-argument you declared, and two additional parameters
|
|
516
|
-
which are the parsed options and the command object itself.
|
|
520
|
+
which are the parsed options and the command object itself.
|
|
517
521
|
|
|
518
522
|
Example file: [thank.js](./examples/thank.js)
|
|
519
523
|
|
|
@@ -550,8 +554,9 @@ pass more arguments than declared, but you can make this an error with `.allowEx
|
|
|
550
554
|
### Stand-alone executable (sub)commands
|
|
551
555
|
|
|
552
556
|
When `.command()` is invoked with a description argument, this tells Commander that you're going to use stand-alone executables for subcommands.
|
|
553
|
-
Commander will search the
|
|
554
|
-
You
|
|
557
|
+
Commander will search the files in the directory of the entry script for a file with the name combination `command-subcommand`, like `pm-install` or `pm-search` in the example below. The search includes trying common file extensions, like `.js`.
|
|
558
|
+
You may specify a custom name (and path) with the `executableFile` configuration option.
|
|
559
|
+
You may specify a custom search directory for subcommands with `.executableDir()`.
|
|
555
560
|
|
|
556
561
|
You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.
|
|
557
562
|
|
|
@@ -559,6 +564,7 @@ Example file: [pm](./examples/pm)
|
|
|
559
564
|
|
|
560
565
|
```js
|
|
561
566
|
program
|
|
567
|
+
.name('pm')
|
|
562
568
|
.version('0.1.0')
|
|
563
569
|
.command('install [name]', 'install one or more packages')
|
|
564
570
|
.command('search [query]', 'search with optional query')
|
|
@@ -630,7 +636,7 @@ shell spawn --help
|
|
|
630
636
|
|
|
631
637
|
### Custom help
|
|
632
638
|
|
|
633
|
-
You can add extra text to be displayed along with the built-in help.
|
|
639
|
+
You can add extra text to be displayed along with the built-in help.
|
|
634
640
|
|
|
635
641
|
Example file: [custom-help](./examples/custom-help)
|
|
636
642
|
|
|
@@ -664,7 +670,7 @@ The positions in order displayed are:
|
|
|
664
670
|
- `after`: display extra information after built-in help
|
|
665
671
|
- `afterAll`: add to the program for a global footer (epilog)
|
|
666
672
|
|
|
667
|
-
The positions "beforeAll" and "afterAll" apply to the command and all its subcommands.
|
|
673
|
+
The positions "beforeAll" and "afterAll" apply to the command and all its subcommands.
|
|
668
674
|
|
|
669
675
|
The second parameter can be a string, or a function returning a string. The function is passed a context object for your convenience. The properties are:
|
|
670
676
|
|
|
@@ -673,7 +679,7 @@ The second parameter can be a string, or a function returning a string. The func
|
|
|
673
679
|
|
|
674
680
|
### Display help after errors
|
|
675
681
|
|
|
676
|
-
The default behaviour for usage errors is to just display a short error message.
|
|
682
|
+
The default behaviour for usage errors is to just display a short error message.
|
|
677
683
|
You can change the behaviour to show the full help or a custom help message after an error.
|
|
678
684
|
|
|
679
685
|
```js
|
|
@@ -688,6 +694,18 @@ error: unknown option '--unknown'
|
|
|
688
694
|
(add --help for additional information)
|
|
689
695
|
```
|
|
690
696
|
|
|
697
|
+
You can also show suggestions after an error for an unknown command or option.
|
|
698
|
+
|
|
699
|
+
```js
|
|
700
|
+
program.showSuggestionAfterError();
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
```sh
|
|
704
|
+
$ pizza --hepl
|
|
705
|
+
error: unknown option '--hepl'
|
|
706
|
+
(Did you mean --help?)
|
|
707
|
+
```
|
|
708
|
+
|
|
691
709
|
### Display help from code
|
|
692
710
|
|
|
693
711
|
`.help()`: display help information and exit immediately. You can optionally pass `{ error: true }` to display on stderr and exit with an error status.
|
|
@@ -696,10 +714,25 @@ error: unknown option '--unknown'
|
|
|
696
714
|
|
|
697
715
|
`.helpInformation()`: get the built-in command help information as a string for processing or displaying yourself.
|
|
698
716
|
|
|
699
|
-
### .
|
|
717
|
+
### .name
|
|
700
718
|
|
|
701
|
-
|
|
702
|
-
|
|
719
|
+
The command name appears in the help, and is also used for locating stand-alone executable subcommands.
|
|
720
|
+
|
|
721
|
+
You may specify the program name using `.name()` or in the Command constructor. For the program, Commander will
|
|
722
|
+
fallback to using the script name from the full arguments passed into `.parse()`. However, the script name varies
|
|
723
|
+
depending on how your program is launched so you may wish to specify it explicitly.
|
|
724
|
+
|
|
725
|
+
```js
|
|
726
|
+
program.name('pizza');
|
|
727
|
+
const pm = new Command('pm');
|
|
728
|
+
```
|
|
729
|
+
|
|
730
|
+
Subcommands get a name when specified using `.command()`. If you create the subcommand yourself to use with `.addCommand()`,
|
|
731
|
+
then set the name using `.name()` or in the Command constructor.
|
|
732
|
+
|
|
733
|
+
### .usage
|
|
734
|
+
|
|
735
|
+
This allows you to customise the usage description in the first line of the help. Given:
|
|
703
736
|
|
|
704
737
|
```js
|
|
705
738
|
program
|
|
@@ -715,7 +748,7 @@ Usage: my-command [global options] command
|
|
|
715
748
|
|
|
716
749
|
### .helpOption(flags, description)
|
|
717
750
|
|
|
718
|
-
By default every command has a help option.
|
|
751
|
+
By default every command has a help option. You may change the default help flags and description. Pass false to disable the built-in help option.
|
|
719
752
|
|
|
720
753
|
```js
|
|
721
754
|
program
|
|
@@ -747,7 +780,7 @@ There are methods getting the visible lists of arguments, options, and subcomman
|
|
|
747
780
|
|
|
748
781
|
Example file: [configure-help.js](./examples/configure-help.js)
|
|
749
782
|
|
|
750
|
-
```
|
|
783
|
+
```js
|
|
751
784
|
program.configureHelp({
|
|
752
785
|
sortSubcommands: true,
|
|
753
786
|
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
|
|
@@ -762,13 +795,6 @@ You can execute custom actions by listening to command and option events.
|
|
|
762
795
|
program.on('option:verbose', function () {
|
|
763
796
|
process.env.VERBOSE = this.opts().verbose;
|
|
764
797
|
});
|
|
765
|
-
|
|
766
|
-
program.on('command:*', function (operands) {
|
|
767
|
-
console.error(`error: unknown command '${operands[0]}'`);
|
|
768
|
-
const availableCommands = program.commands.map(cmd => cmd.name());
|
|
769
|
-
mySuggestBestMatch(operands[0], availableCommands);
|
|
770
|
-
process.exitCode = 1;
|
|
771
|
-
});
|
|
772
798
|
```
|
|
773
799
|
|
|
774
800
|
## Bits and pieces
|
|
@@ -809,7 +835,7 @@ program subcommand -b
|
|
|
809
835
|
|
|
810
836
|
By default options are recognised before and after command-arguments. To only process options that come
|
|
811
837
|
before the command-arguments, use `.passThroughOptions()`. This lets you pass the arguments and following options through to another program
|
|
812
|
-
without needing to use `--` to end the option processing.
|
|
838
|
+
without needing to use `--` to end the option processing.
|
|
813
839
|
To use pass through options in a subcommand, the program needs to enable positional options.
|
|
814
840
|
|
|
815
841
|
Example file: [pass-through-options.js](./examples/pass-through-options.js)
|
|
@@ -826,7 +852,7 @@ By default the option processing shows an error for an unknown option. To have a
|
|
|
826
852
|
By default the argument processing does not display an error for more command-arguments than expected.
|
|
827
853
|
To display an error for excess arguments, use`.allowExcessArguments(false)`.
|
|
828
854
|
|
|
829
|
-
### Legacy options as properties
|
|
855
|
+
### Legacy options as properties
|
|
830
856
|
|
|
831
857
|
Before Commander 7, the option values were stored as properties on the command.
|
|
832
858
|
This was convenient to code but the downside was possible clashes with
|
|
@@ -903,7 +929,6 @@ You can modify this behaviour for custom applications. In addition, you can modi
|
|
|
903
929
|
|
|
904
930
|
Example file: [configure-output.js](./examples/configure-output.js)
|
|
905
931
|
|
|
906
|
-
|
|
907
932
|
```js
|
|
908
933
|
function errorColor(str) {
|
|
909
934
|
// Add ANSI escape codes to display text in red.
|
|
@@ -960,6 +985,7 @@ const { Command } = require('commander');
|
|
|
960
985
|
const program = new Command();
|
|
961
986
|
|
|
962
987
|
program
|
|
988
|
+
.name('deploy')
|
|
963
989
|
.version('0.0.1')
|
|
964
990
|
.option('-c, --config <path>', 'set config path', './deploy.conf');
|
|
965
991
|
|
|
@@ -986,7 +1012,7 @@ Examples:
|
|
|
986
1012
|
$ deploy exec sequential
|
|
987
1013
|
$ deploy exec async`
|
|
988
1014
|
);
|
|
989
|
-
|
|
1015
|
+
|
|
990
1016
|
program.parse(process.argv);
|
|
991
1017
|
```
|
|
992
1018
|
|
|
@@ -994,8 +1020,8 @@ More samples can be found in the [examples](https://github.com/tj/commander.js/t
|
|
|
994
1020
|
|
|
995
1021
|
## Support
|
|
996
1022
|
|
|
997
|
-
The current version of Commander is fully supported on Long Term Support versions of
|
|
998
|
-
(For older versions of
|
|
1023
|
+
The current version of Commander is fully supported on Long Term Support versions of Node.js, and requires at least v12.20.0.
|
|
1024
|
+
(For older versions of Node.js, use an older version of Commander.)
|
|
999
1025
|
|
|
1000
1026
|
The main forum for free and community support is the project [Issues](https://github.com/tj/commander.js/issues) on GitHub.
|
|
1001
1027
|
|
package/lib/argument.js
CHANGED
|
@@ -90,7 +90,7 @@ class Argument {
|
|
|
90
90
|
};
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
|
-
* Only allow
|
|
93
|
+
* Only allow argument value to be one of choices.
|
|
94
94
|
*
|
|
95
95
|
* @param {string[]} values
|
|
96
96
|
* @return {Argument}
|
|
@@ -109,6 +109,22 @@ class Argument {
|
|
|
109
109
|
};
|
|
110
110
|
return this;
|
|
111
111
|
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Make argument required.
|
|
115
|
+
*/
|
|
116
|
+
argRequired() {
|
|
117
|
+
this.required = true;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Make argument optional.
|
|
123
|
+
*/
|
|
124
|
+
argOptional() {
|
|
125
|
+
this.required = false;
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
112
128
|
}
|
|
113
129
|
|
|
114
130
|
/**
|