commander 12.0.0 → 13.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 +15 -29
- package/esm.mjs +1 -1
- package/lib/argument.js +9 -5
- package/lib/command.js +509 -223
- package/lib/error.js +0 -4
- package/lib/help.js +336 -84
- package/lib/option.js +40 -19
- package/lib/suggestSimilar.js +5 -4
- package/package.json +23 -21
- package/typings/index.d.ts +174 -44
package/Readme.md
CHANGED
|
@@ -79,7 +79,8 @@ const { program } = require('commander');
|
|
|
79
79
|
|
|
80
80
|
program
|
|
81
81
|
.option('--first')
|
|
82
|
-
.option('-s, --separator <char>')
|
|
82
|
+
.option('-s, --separator <char>')
|
|
83
|
+
.argument('<string>');
|
|
83
84
|
|
|
84
85
|
program.parse();
|
|
85
86
|
|
|
@@ -678,8 +679,7 @@ async function main() {
|
|
|
678
679
|
}
|
|
679
680
|
```
|
|
680
681
|
|
|
681
|
-
A command's options and arguments on the command line are validated when the command is used. Any unknown options or missing arguments will be reported as an error. You can suppress the unknown option
|
|
682
|
-
pass more arguments than declared, but you can make this an error with `.allowExcessArguments(false)`.
|
|
682
|
+
A command's options and arguments on the command line are validated when the command is used. Any unknown options or missing arguments or excess arguments will be reported as an error. You can suppress the unknown option check with `.allowUnknownOption()`. You can suppress the excess arguments check with `.allowExcessArguments()`.
|
|
683
683
|
|
|
684
684
|
### Stand-alone executable (sub)commands
|
|
685
685
|
|
|
@@ -696,7 +696,7 @@ Example file: [pm](./examples/pm)
|
|
|
696
696
|
program
|
|
697
697
|
.name('pm')
|
|
698
698
|
.version('0.1.0')
|
|
699
|
-
.command('install [
|
|
699
|
+
.command('install [package-names...]', 'install one or more packages')
|
|
700
700
|
.command('search [query]', 'search with optional query')
|
|
701
701
|
.command('update', 'update installed packages', { executableFile: 'myUpdateSubCommand' })
|
|
702
702
|
.command('list', 'list packages installed', { isDefault: true });
|
|
@@ -923,23 +923,7 @@ program.helpCommand('assist [command]', 'show assistance');
|
|
|
923
923
|
The built-in help is formatted using the Help class.
|
|
924
924
|
You can configure the Help behaviour by modifying data properties and methods using `.configureHelp()`, or by subclassing using `.createHelp()` if you prefer.
|
|
925
925
|
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
- `helpWidth`: specify the wrap width, useful for unit tests
|
|
929
|
-
- `sortSubcommands`: sort the subcommands alphabetically
|
|
930
|
-
- `sortOptions`: sort the options alphabetically
|
|
931
|
-
- `showGlobalOptions`: show a section with the global options from the parent command(s)
|
|
932
|
-
|
|
933
|
-
You can override any method on the [Help](./lib/help.js) class. There are methods getting the visible lists of arguments, options, and subcommands. There are methods for formatting the items in the lists, with each item having a _term_ and _description_. Take a look at `.formatHelp()` to see how they are used.
|
|
934
|
-
|
|
935
|
-
Example file: [configure-help.js](./examples/configure-help.js)
|
|
936
|
-
|
|
937
|
-
```js
|
|
938
|
-
program.configureHelp({
|
|
939
|
-
sortSubcommands: true,
|
|
940
|
-
subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.
|
|
941
|
-
});
|
|
942
|
-
```
|
|
926
|
+
For more detail see (./docs/help-in-depth.md)
|
|
943
927
|
|
|
944
928
|
## Custom event listeners
|
|
945
929
|
|
|
@@ -955,22 +939,24 @@ program.on('option:verbose', function () {
|
|
|
955
939
|
|
|
956
940
|
### .parse() and .parseAsync()
|
|
957
941
|
|
|
958
|
-
|
|
942
|
+
Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
959
943
|
|
|
960
|
-
|
|
944
|
+
Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
961
945
|
|
|
962
|
-
- 'node'
|
|
963
|
-
- 'electron'
|
|
964
|
-
- 'user'
|
|
946
|
+
- `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
|
947
|
+
- `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
|
948
|
+
- `'user'`: just user arguments
|
|
965
949
|
|
|
966
950
|
For example:
|
|
967
951
|
|
|
968
952
|
```js
|
|
969
|
-
program.parse(
|
|
970
|
-
program.parse(); //
|
|
971
|
-
program.parse(['
|
|
953
|
+
program.parse(); // parse process.argv and auto-detect electron and special node flags
|
|
954
|
+
program.parse(process.argv); // assume argv[0] is app and argv[1] is script
|
|
955
|
+
program.parse(['--port', '80'], { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
972
956
|
```
|
|
973
957
|
|
|
958
|
+
Use parseAsync instead of parse if any of your action handlers are async.
|
|
959
|
+
|
|
974
960
|
If you want to parse multiple times, create a new program each time. Calling parse does not clear out any previous state.
|
|
975
961
|
|
|
976
962
|
### Parsing Configuration
|
package/esm.mjs
CHANGED
package/lib/argument.js
CHANGED
|
@@ -50,7 +50,7 @@ class Argument {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
|
-
* @package
|
|
53
|
+
* @package
|
|
54
54
|
*/
|
|
55
55
|
|
|
56
56
|
_concatValue(value, previous) {
|
|
@@ -98,7 +98,9 @@ class Argument {
|
|
|
98
98
|
this.argChoices = values.slice();
|
|
99
99
|
this.parseArg = (arg, previous) => {
|
|
100
100
|
if (!this.argChoices.includes(arg)) {
|
|
101
|
-
throw new InvalidArgumentError(
|
|
101
|
+
throw new InvalidArgumentError(
|
|
102
|
+
`Allowed choices are ${this.argChoices.join(', ')}.`,
|
|
103
|
+
);
|
|
102
104
|
}
|
|
103
105
|
if (this.variadic) {
|
|
104
106
|
return this._concatValue(arg, previous);
|
|
@@ -110,6 +112,8 @@ class Argument {
|
|
|
110
112
|
|
|
111
113
|
/**
|
|
112
114
|
* Make argument required.
|
|
115
|
+
*
|
|
116
|
+
* @returns {Argument}
|
|
113
117
|
*/
|
|
114
118
|
argRequired() {
|
|
115
119
|
this.required = true;
|
|
@@ -118,6 +122,8 @@ class Argument {
|
|
|
118
122
|
|
|
119
123
|
/**
|
|
120
124
|
* Make argument optional.
|
|
125
|
+
*
|
|
126
|
+
* @returns {Argument}
|
|
121
127
|
*/
|
|
122
128
|
argOptional() {
|
|
123
129
|
this.required = false;
|
|
@@ -136,9 +142,7 @@ class Argument {
|
|
|
136
142
|
function humanReadableArgName(arg) {
|
|
137
143
|
const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');
|
|
138
144
|
|
|
139
|
-
return arg.required
|
|
140
|
-
? '<' + nameOutput + '>'
|
|
141
|
-
: '[' + nameOutput + ']';
|
|
145
|
+
return arg.required ? '<' + nameOutput + '>' : '[' + nameOutput + ']';
|
|
142
146
|
}
|
|
143
147
|
|
|
144
148
|
exports.Argument = Argument;
|