commander 5.0.0 → 6.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/CHANGELOG.md +82 -109
- package/Readme.md +90 -51
- package/index.js +281 -111
- package/package.json +9 -9
- package/typings/index.d.ts +93 -62
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,62 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). (Format adopted after v3.0.0.)
|
|
7
7
|
|
|
8
8
|
<!-- markdownlint-disable MD024 -->
|
|
9
|
+
<!-- markdownlint-disable MD004 -->
|
|
10
|
+
|
|
11
|
+
## [6.1.0] (2020-08-28)
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- include URL to relevant section of README for error for potential conflict between Command properties and option values ([#1306])
|
|
16
|
+
- `.combineFlagAndOptionalValue(false)` to ease upgrade path from older versions of Commander ([#1326])
|
|
17
|
+
- allow disabling the built-in help option using `.helpOption(false)` ([#1325])
|
|
18
|
+
- allow just some arguments in `argumentDescription` to `.description()` ([#1323])
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- tidy async test and remove lint override ([#1312])
|
|
23
|
+
|
|
24
|
+
### Fixed
|
|
25
|
+
|
|
26
|
+
- executable subcommand launching when script path not known ([#1322])
|
|
27
|
+
|
|
28
|
+
## [6.0.0] (2020-07-21)
|
|
29
|
+
|
|
30
|
+
### Added
|
|
31
|
+
|
|
32
|
+
- add support for variadic options ([#1250])
|
|
33
|
+
- allow options to be added with just a short flag ([#1256])
|
|
34
|
+
- *Breaking* the option property has same case as flag. e.g. flag `-n` accessed as `opts().n` (previously uppercase)
|
|
35
|
+
- *Breaking* throw an error if there might be a clash between option name and a Command property, with advice on how to resolve ([#1275])
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- Options which contain -no- in the middle of the option flag should not be treated as negatable. ([#1301])
|
|
40
|
+
|
|
41
|
+
## [6.0.0-0] (2020-06-20)
|
|
42
|
+
|
|
43
|
+
(Released in 6.0.0)
|
|
44
|
+
|
|
45
|
+
## [5.1.0] (2020-04-25)
|
|
46
|
+
|
|
47
|
+
### Added
|
|
48
|
+
|
|
49
|
+
- support for multiple command aliases, the first of which is shown in the auto-generated help ([#531], [#1236])
|
|
50
|
+
- configuration support in `addCommand()` for `hidden` and `isDefault` ([#1232])
|
|
51
|
+
|
|
52
|
+
### Fixed
|
|
53
|
+
|
|
54
|
+
- omit masked help flags from the displayed help ([#645], [#1247])
|
|
55
|
+
- remove old short help flag when change help flags using `helpOption` ([#1248])
|
|
56
|
+
|
|
57
|
+
### Changed
|
|
58
|
+
|
|
59
|
+
- remove use of `arguments` to improve auto-generated help in editors ([#1235])
|
|
60
|
+
- rename `.command()` configuration `noHelp` to `hidden` (but not remove old support) ([#1232])
|
|
61
|
+
- improvements to documentation
|
|
62
|
+
- update dependencies
|
|
63
|
+
- update tested versions of node
|
|
64
|
+
- eliminate lint errors in TypeScript ([#1208])
|
|
9
65
|
|
|
10
66
|
## [5.0.0] (2020-03-14)
|
|
11
67
|
|
|
@@ -75,6 +131,9 @@ If you use `program.args` or more complicated tests to detect a missing subcomma
|
|
|
75
131
|
|
|
76
132
|
If you use `.command('*')` to add a default command, you may be be able to switch to `isDefault:true` with a named command.
|
|
77
133
|
|
|
134
|
+
If you want to continue combining short options with optional values as though they were boolean flags, set `combineFlagAndOptionalValue(false)`
|
|
135
|
+
to expand `-fb` to `-f -b` rather than `-f b`.
|
|
136
|
+
|
|
78
137
|
## [5.0.0-4] (2020-03-03)
|
|
79
138
|
|
|
80
139
|
(Released in 5.0.0)
|
|
@@ -177,95 +236,9 @@ if (program.rawArgs.length < 3) ...
|
|
|
177
236
|
|
|
178
237
|
(Released in 4.0.0)
|
|
179
238
|
|
|
180
|
-
## [2.20.1] (2019-09-29)
|
|
181
|
-
|
|
182
|
-
### Fixed
|
|
183
|
-
|
|
184
|
-
* Improve tracking of executable subcommands.
|
|
185
|
-
|
|
186
|
-
### Changed
|
|
187
|
-
|
|
188
|
-
* update development dependencies
|
|
189
|
-
|
|
190
|
-
## [3.0.2] (2019-09-27)
|
|
191
|
-
|
|
192
|
-
### Fixed
|
|
193
|
-
|
|
194
|
-
* Improve tracking of executable subcommands.
|
|
195
|
-
|
|
196
|
-
### Changed
|
|
197
|
-
|
|
198
|
-
* update development dependencies
|
|
199
|
-
|
|
200
|
-
## [3.0.1] (2019-08-30)
|
|
201
|
-
|
|
202
|
-
### Added
|
|
203
|
-
|
|
204
|
-
* .name and .usage to README ([#1010])
|
|
205
|
-
* Table of Contents to README ([#1010])
|
|
206
|
-
* TypeScript definition for `executableFile` in CommandOptions ([#1028])
|
|
207
|
-
|
|
208
|
-
### Changed
|
|
209
|
-
|
|
210
|
-
* consistently use `const` rather than `var` in README ([#1026])
|
|
211
|
-
|
|
212
|
-
### Fixed
|
|
213
|
-
|
|
214
|
-
* help for sub commands with custom executableFile ([#1018])
|
|
215
|
-
|
|
216
|
-
## [3.0.0] / 2019-08-08
|
|
217
|
-
|
|
218
|
-
* Add option to specify executable file name ([#999])
|
|
219
|
-
* e.g. `.command('clone', 'clone description', { executableFile: 'myClone' })`
|
|
220
|
-
* Change docs for `.command` to contrast action handler vs git-style executable. ([#938] [#990])
|
|
221
|
-
* **Breaking** Change TypeScript to use overloaded function for `.command`. ([#938] [#990])
|
|
222
|
-
* Change to use straight quotes around strings in error messages (like 'this' instead of `this') ([#915])
|
|
223
|
-
* Add TypeScript "reference types" for node ([#974])
|
|
224
|
-
* Add support for hyphen as an option argument in subcommands ([#697])
|
|
225
|
-
* Add support for a short option flag and its value to be concatenated for action handler subcommands ([#599])
|
|
226
|
-
* e.g. `-p 80` can also be supplied as `-p80`
|
|
227
|
-
* Add executable arguments to spawn in win32, for git-style executables ([#611])
|
|
228
|
-
* e.g. `node --harmony myCommand.js clone`
|
|
229
|
-
* Add parent command as prefix of subcommand in help ([#980])
|
|
230
|
-
* Add optional custom description to `.version` ([#963])
|
|
231
|
-
* e.g. `program.version('0.0.1', '-v, --vers', 'output the current version')`
|
|
232
|
-
* Add `.helpOption(flags, description)` routine to customise help flags and description ([#963])
|
|
233
|
-
* e.g. `.helpOption('-e, --HELP', 'read more information')`
|
|
234
|
-
* Fix behavior of --no-* options ([#795])
|
|
235
|
-
* can now define both `--foo` and `--no-foo`
|
|
236
|
-
* **Breaking** custom event listeners: `--no-foo` on cli now emits `option:no-foo` (previously `option:foo`)
|
|
237
|
-
* **Breaking** default value: defining `--no-foo` after defining `--foo` leaves the default value unchanged (previously set it to false)
|
|
238
|
-
* allow boolean default value, such as from environment ([#987])
|
|
239
|
-
* Increment inspector port for spawned subcommands ([#991])
|
|
240
|
-
* e.g. `node --inspect myCommand.js clone`
|
|
241
|
-
|
|
242
|
-
### Migration Tips
|
|
243
|
-
|
|
244
|
-
The custom event for a negated option like `--no-foo` is `option:no-foo` (previously `option:foo`).
|
|
245
|
-
|
|
246
|
-
```js
|
|
247
|
-
program
|
|
248
|
-
.option('--no-foo')
|
|
249
|
-
.on('option:no-foo', () => {
|
|
250
|
-
console.log('removing foo');
|
|
251
|
-
});
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
When using TypeScript, adding a command does not allow an explicit `undefined` for an unwanted executable description (e.g
|
|
255
|
-
for a command with an action handler).
|
|
256
|
-
|
|
257
|
-
```js
|
|
258
|
-
program
|
|
259
|
-
.command('action1', undefined, { noHelp: true }) // No longer valid
|
|
260
|
-
.command('action2', { noHelp: true }) // Correct
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
## 3.0.0-0 Prerelease / 2019-07-28
|
|
264
|
-
|
|
265
|
-
(Released as 3.0.0)
|
|
266
|
-
|
|
267
239
|
## Older versions
|
|
268
240
|
|
|
241
|
+
* [3.x](./changelogs/CHANGELOG-3.md)
|
|
269
242
|
* [2.x](./changelogs/CHANGELOG-2.md)
|
|
270
243
|
* [1.x](./changelogs/CHANGELOG-1.md)
|
|
271
244
|
* [0.x](./changelogs/CHANGELOG-0.md)
|
|
@@ -274,33 +247,17 @@ program
|
|
|
274
247
|
[#432]: https://github.com/tj/commander.js/issues/432
|
|
275
248
|
[#508]: https://github.com/tj/commander.js/issues/508
|
|
276
249
|
[#512]: https://github.com/tj/commander.js/issues/512
|
|
277
|
-
[#
|
|
278
|
-
[#
|
|
279
|
-
[#697]: https://github.com/tj/commander.js/issues/697
|
|
250
|
+
[#531]: https://github.com/tj/commander.js/issues/531
|
|
251
|
+
[#645]: https://github.com/tj/commander.js/issues/645
|
|
280
252
|
[#742]: https://github.com/tj/commander.js/issues/742
|
|
281
253
|
[#764]: https://github.com/tj/commander.js/issues/764
|
|
282
|
-
[#795]: https://github.com/tj/commander.js/issues/795
|
|
283
254
|
[#802]: https://github.com/tj/commander.js/issues/802
|
|
284
255
|
[#806]: https://github.com/tj/commander.js/issues/806
|
|
285
256
|
[#809]: https://github.com/tj/commander.js/issues/809
|
|
286
|
-
[#915]: https://github.com/tj/commander.js/issues/915
|
|
287
|
-
[#938]: https://github.com/tj/commander.js/issues/938
|
|
288
257
|
[#948]: https://github.com/tj/commander.js/issues/948
|
|
289
258
|
[#962]: https://github.com/tj/commander.js/issues/962
|
|
290
|
-
[#963]: https://github.com/tj/commander.js/issues/963
|
|
291
|
-
[#974]: https://github.com/tj/commander.js/issues/974
|
|
292
|
-
[#980]: https://github.com/tj/commander.js/issues/980
|
|
293
|
-
[#987]: https://github.com/tj/commander.js/issues/987
|
|
294
|
-
[#990]: https://github.com/tj/commander.js/issues/990
|
|
295
|
-
[#991]: https://github.com/tj/commander.js/issues/991
|
|
296
|
-
[#993]: https://github.com/tj/commander.js/issues/993
|
|
297
259
|
[#995]: https://github.com/tj/commander.js/issues/995
|
|
298
|
-
[#999]: https://github.com/tj/commander.js/issues/999
|
|
299
|
-
[#1010]: https://github.com/tj/commander.js/pull/1010
|
|
300
|
-
[#1018]: https://github.com/tj/commander.js/pull/1018
|
|
301
|
-
[#1026]: https://github.com/tj/commander.js/pull/1026
|
|
302
260
|
[#1027]: https://github.com/tj/commander.js/pull/1027
|
|
303
|
-
[#1028]: https://github.com/tj/commander.js/pull/1028
|
|
304
261
|
[#1032]: https://github.com/tj/commander.js/issues/1032
|
|
305
262
|
[#1035]: https://github.com/tj/commander.js/pull/1035
|
|
306
263
|
[#1040]: https://github.com/tj/commander.js/pull/1040
|
|
@@ -335,8 +292,28 @@ program
|
|
|
335
292
|
[#1184]: https://github.com/tj/commander.js/pull/1184
|
|
336
293
|
[#1191]: https://github.com/tj/commander.js/pull/1191
|
|
337
294
|
[#1195]: https://github.com/tj/commander.js/pull/1195
|
|
295
|
+
[#1208]: https://github.com/tj/commander.js/pull/1208
|
|
296
|
+
[#1232]: https://github.com/tj/commander.js/pull/1232
|
|
297
|
+
[#1235]: https://github.com/tj/commander.js/pull/1235
|
|
298
|
+
[#1236]: https://github.com/tj/commander.js/pull/1236
|
|
299
|
+
[#1247]: https://github.com/tj/commander.js/pull/1247
|
|
300
|
+
[#1248]: https://github.com/tj/commander.js/pull/1248
|
|
301
|
+
[#1250]: https://github.com/tj/commander.js/pull/1250
|
|
302
|
+
[#1256]: https://github.com/tj/commander.js/pull/1256
|
|
303
|
+
[#1275]: https://github.com/tj/commander.js/pull/1275
|
|
304
|
+
[#1301]: https://github.com/tj/commander.js/issues/1301
|
|
305
|
+
[#1306]: https://github.com/tj/commander.js/pull/1306
|
|
306
|
+
[#1312]: https://github.com/tj/commander.js/pull/1312
|
|
307
|
+
[#1322]: https://github.com/tj/commander.js/pull/1322
|
|
308
|
+
[#1323]: https://github.com/tj/commander.js/pull/1323
|
|
309
|
+
[#1325]: https://github.com/tj/commander.js/pull/1325
|
|
310
|
+
[#1326]: https://github.com/tj/commander.js/pull/1326
|
|
338
311
|
|
|
339
312
|
[Unreleased]: https://github.com/tj/commander.js/compare/master...develop
|
|
313
|
+
[6.1.0]: https://github.com/tj/commander.js/compare/v6.0.0..v6.1.0
|
|
314
|
+
[6.0.0]: https://github.com/tj/commander.js/compare/v5.1.0..v6.0.0
|
|
315
|
+
[6.0.0-0]: https://github.com/tj/commander.js/compare/v5.1.0..v6.0.0-0
|
|
316
|
+
[5.1.0]: https://github.com/tj/commander.js/compare/v5.0.0..v5.1.0
|
|
340
317
|
[5.0.0]: https://github.com/tj/commander.js/compare/v4.1.1..v5.0.0
|
|
341
318
|
[5.0.0-4]: https://github.com/tj/commander.js/compare/v5.0.0-3..v5.0.0-4
|
|
342
319
|
[5.0.0-3]: https://github.com/tj/commander.js/compare/v5.0.0-2..v5.0.0-3
|
|
@@ -349,7 +326,3 @@ program
|
|
|
349
326
|
[4.0.0]: https://github.com/tj/commander.js/compare/v3.0.2..v4.0.0
|
|
350
327
|
[4.0.0-1]: https://github.com/tj/commander.js/compare/v4.0.0-0..v4.0.0-1
|
|
351
328
|
[4.0.0-0]: https://github.com/tj/commander.js/compare/v3.0.2...v4.0.0-0
|
|
352
|
-
[3.0.2]: https://github.com/tj/commander.js/compare/v3.0.1...v3.0.2
|
|
353
|
-
[3.0.1]: https://github.com/tj/commander.js/compare/v3.0.0...v3.0.1
|
|
354
|
-
[3.0.0]: https://github.com/tj/commander.js/compare/v2.20.1...v3.0.0
|
|
355
|
-
[2.20.1]: https://github.com/tj/commander.js/compare/v2.20.0...v2.20.1
|
package/Readme.md
CHANGED
|
@@ -18,6 +18,7 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
|
|
|
18
18
|
- [Other option types, negatable boolean and flag|value](#other-option-types-negatable-boolean-and-flagvalue)
|
|
19
19
|
- [Custom option processing](#custom-option-processing)
|
|
20
20
|
- [Required option](#required-option)
|
|
21
|
+
- [Variadic option](#variadic-option)
|
|
21
22
|
- [Version option](#version-option)
|
|
22
23
|
- [Commands](#commands)
|
|
23
24
|
- [Specify the argument syntax](#specify-the-argument-syntax)
|
|
@@ -37,11 +38,11 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
|
|
|
37
38
|
- [Avoiding option name clashes](#avoiding-option-name-clashes)
|
|
38
39
|
- [TypeScript](#typescript)
|
|
39
40
|
- [createCommand()](#createcommand)
|
|
41
|
+
- [Import into ECMAScript Module](#import-into-ecmascript-module)
|
|
40
42
|
- [Node options such as `--harmony`](#node-options-such-as---harmony)
|
|
41
43
|
- [Debugging stand-alone executable subcommands](#debugging-stand-alone-executable-subcommands)
|
|
42
44
|
- [Override exit handling](#override-exit-handling)
|
|
43
45
|
- [Examples](#examples)
|
|
44
|
-
- [License](#license)
|
|
45
46
|
- [Support](#support)
|
|
46
47
|
- [Commander for enterprise](#commander-for-enterprise)
|
|
47
48
|
|
|
@@ -63,11 +64,11 @@ program.version('0.0.1');
|
|
|
63
64
|
|
|
64
65
|
For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
```js
|
|
68
|
+
const { Command } = require('commander');
|
|
69
|
+
const program = new Command();
|
|
70
|
+
program.version('0.0.1');
|
|
71
|
+
```
|
|
71
72
|
|
|
72
73
|
## Options
|
|
73
74
|
|
|
@@ -88,9 +89,9 @@ Options on the command line are not positional, and can be specified before or a
|
|
|
88
89
|
|
|
89
90
|
The two most used option types are a boolean flag, and an option which takes a value (declared using angle brackets). Both are `undefined` unless specified on command line.
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
const { program } = require('commander');
|
|
92
|
+
Example file: [options-common.js](./examples/options-common.js)
|
|
93
93
|
|
|
94
|
+
```js
|
|
94
95
|
program
|
|
95
96
|
.option('-d, --debug', 'output extra debugging')
|
|
96
97
|
.option('-s, --small', 'small pizza size')
|
|
@@ -126,9 +127,9 @@ pizza details:
|
|
|
126
127
|
|
|
127
128
|
You can specify a default value for an option which takes a value.
|
|
128
129
|
|
|
129
|
-
|
|
130
|
-
const { program } = require('commander');
|
|
130
|
+
Example file: [options-defaults.js](./examples/options-defaults.js)
|
|
131
131
|
|
|
132
|
+
```js
|
|
132
133
|
program
|
|
133
134
|
.option('-c, --cheese <type>', 'add the specified type of cheese', 'blue');
|
|
134
135
|
|
|
@@ -152,9 +153,9 @@ Defined alone this also makes the option true by default.
|
|
|
152
153
|
If you define `--foo` first, adding `--no-foo` does not change the default value from what it would
|
|
153
154
|
otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.
|
|
154
155
|
|
|
155
|
-
|
|
156
|
-
const { program } = require('commander');
|
|
156
|
+
Example file: [options-negatable.js](./examples/options-negatable.js)
|
|
157
157
|
|
|
158
|
+
```js
|
|
158
159
|
program
|
|
159
160
|
.option('--no-sauce', 'Remove sauce')
|
|
160
161
|
.option('--cheese <flavour>', 'cheese flavour', 'mozzarella')
|
|
@@ -179,9 +180,9 @@ You ordered a pizza with no sauce and no cheese
|
|
|
179
180
|
|
|
180
181
|
You can specify an option which functions as a flag but may also take a value (declared using square brackets).
|
|
181
182
|
|
|
182
|
-
|
|
183
|
-
const { program } = require('commander');
|
|
183
|
+
Example file: [options-flag-or-value.js](./examples/options-flag-or-value.js)
|
|
184
184
|
|
|
185
|
+
```js
|
|
185
186
|
program
|
|
186
187
|
.option('-c, --cheese [type]', 'Add cheese with optional type');
|
|
187
188
|
|
|
@@ -210,9 +211,9 @@ This allows you to coerce the option value to the desired type, or accumulate va
|
|
|
210
211
|
|
|
211
212
|
You can optionally specify the default/starting value for the option after the function.
|
|
212
213
|
|
|
213
|
-
|
|
214
|
-
const { program } = require('commander');
|
|
214
|
+
Example file: [options-custom-processing.js](./examples/options-custom-processing.js)
|
|
215
215
|
|
|
216
|
+
```js
|
|
216
217
|
function myParseInt(value, dummyPrevious) {
|
|
217
218
|
// parseInt takes a string and an optional radix
|
|
218
219
|
return parseInt(value);
|
|
@@ -264,9 +265,9 @@ $ custom --list x,y,z
|
|
|
264
265
|
|
|
265
266
|
You may specify a required (mandatory) option using `.requiredOption`. The option must have a value after parsing, usually specified on the command line, or perhaps from a default value (say from environment). The method is otherwise the same as `.option` in format, taking flags and description, and optional default value or custom processing.
|
|
266
267
|
|
|
267
|
-
|
|
268
|
-
const { program } = require('commander');
|
|
268
|
+
Example file: [options-required.js](./examples/options-required.js)
|
|
269
269
|
|
|
270
|
+
```js
|
|
270
271
|
program
|
|
271
272
|
.requiredOption('-c, --cheese <type>', 'pizza must have cheese');
|
|
272
273
|
|
|
@@ -278,6 +279,38 @@ $ pizza
|
|
|
278
279
|
error: required option '-c, --cheese <type>' not specified
|
|
279
280
|
```
|
|
280
281
|
|
|
282
|
+
### Variadic option
|
|
283
|
+
|
|
284
|
+
You may make an option variadic by appending `...` to the value placeholder when declaring the option. On the command line you
|
|
285
|
+
can then specify multiple option arguments, and the parsed option value will be an array. The extra arguments
|
|
286
|
+
are read until the first argument starting with a dash. The special argument `--` stops option processing entirely. If a value
|
|
287
|
+
is specified in the same argument as the option then no further values are read.
|
|
288
|
+
|
|
289
|
+
Example file: [options-variadic.js](./examples/options-variadic.js)
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
program
|
|
293
|
+
.option('-n, --number <numbers...>', 'specify numbers')
|
|
294
|
+
.option('-l, --letter [letters...]', 'specify letters');
|
|
295
|
+
|
|
296
|
+
program.parse();
|
|
297
|
+
|
|
298
|
+
console.log('Options: ', program.opts());
|
|
299
|
+
console.log('Remaining arguments: ', program.args);
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
```bash
|
|
303
|
+
$ collect -n 1 2 3 --letter a b c
|
|
304
|
+
Options: { number: [ '1', '2', '3' ], letter: [ 'a', 'b', 'c' ] }
|
|
305
|
+
Remaining arguments: []
|
|
306
|
+
$ collect --letter=A -n80 operand
|
|
307
|
+
Options: { number: [ '80' ], letter: [ 'A' ] }
|
|
308
|
+
Remaining arguments: [ 'operand' ]
|
|
309
|
+
$ collect --letter -n 1 -n 2 3 -- operand
|
|
310
|
+
Options: { number: [ '1', '2', '3' ], letter: true }
|
|
311
|
+
Remaining arguments: [ 'operand' ]
|
|
312
|
+
```
|
|
313
|
+
|
|
281
314
|
### Version option
|
|
282
315
|
|
|
283
316
|
The optional `version` method adds handling for displaying the command version. The default option flags are `-V` and `--version`, and when present the command prints the version number and exits.
|
|
@@ -292,7 +325,7 @@ $ ./examples/pizza -V
|
|
|
292
325
|
```
|
|
293
326
|
|
|
294
327
|
You may change the flags and description by passing additional parameters to the `version` method, using
|
|
295
|
-
the same syntax for flags as the `option` method.
|
|
328
|
+
the same syntax for flags as the `option` method.
|
|
296
329
|
|
|
297
330
|
```js
|
|
298
331
|
program.version('0.0.1', '-v, --vers', 'output the current version');
|
|
@@ -300,7 +333,7 @@ program.version('0.0.1', '-v, --vers', 'output the current version');
|
|
|
300
333
|
|
|
301
334
|
## Commands
|
|
302
335
|
|
|
303
|
-
You can specify (sub)commands
|
|
336
|
+
You can specify (sub)commands using `.command()` or `.addCommand()`. There are two ways these can be implemented: using an action handler attached to the command, or as a stand-alone executable file (described in more detail later). The subcommands may be nested ([example](./examples/nestedCommands.js)).
|
|
304
337
|
|
|
305
338
|
In the first parameter to `.command()` you specify the command name and any command arguments. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.
|
|
306
339
|
|
|
@@ -319,42 +352,35 @@ program
|
|
|
319
352
|
});
|
|
320
353
|
|
|
321
354
|
// Command implemented using stand-alone executable file (description is second parameter to `.command`)
|
|
322
|
-
// Returns
|
|
355
|
+
// Returns `this` for adding more commands.
|
|
323
356
|
program
|
|
324
357
|
.command('start <service>', 'start named service')
|
|
325
358
|
.command('stop [service]', 'stop named service, or all if no name supplied');
|
|
326
359
|
|
|
327
360
|
// Command prepared separately.
|
|
328
|
-
// Returns
|
|
361
|
+
// Returns `this` for adding more commands.
|
|
329
362
|
program
|
|
330
363
|
.addCommand(build.makeBuildCommand());
|
|
331
364
|
```
|
|
332
365
|
|
|
333
|
-
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.
|
|
366
|
+
Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `true` for `opts.hidden` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified ([example](./examples/defaultCommand.js)).
|
|
334
367
|
|
|
335
368
|
### Specify the argument syntax
|
|
336
369
|
|
|
337
|
-
You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.
|
|
370
|
+
You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are usually included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.
|
|
338
371
|
|
|
339
|
-
|
|
340
|
-
const { program } = require('commander');
|
|
372
|
+
Example file: [env](./examples/env)
|
|
341
373
|
|
|
374
|
+
```js
|
|
342
375
|
program
|
|
343
376
|
.version('0.1.0')
|
|
344
377
|
.arguments('<cmd> [env]')
|
|
345
378
|
.action(function (cmd, env) {
|
|
346
|
-
cmdValue
|
|
347
|
-
envValue
|
|
379
|
+
console.log('command:', cmdValue);
|
|
380
|
+
console.log('environment:', envValue || 'no environment given');
|
|
348
381
|
});
|
|
349
382
|
|
|
350
383
|
program.parse(process.argv);
|
|
351
|
-
|
|
352
|
-
if (typeof cmdValue === 'undefined') {
|
|
353
|
-
console.error('no command given!');
|
|
354
|
-
process.exit(1);
|
|
355
|
-
}
|
|
356
|
-
console.log('command:', cmdValue);
|
|
357
|
-
console.log('environment:', envValue || "no environment given");
|
|
358
384
|
```
|
|
359
385
|
|
|
360
386
|
The last argument of a command can be variadic, and only the last argument. To make an argument variadic you
|
|
@@ -422,17 +448,17 @@ You can specify a custom name with the `executableFile` configuration option.
|
|
|
422
448
|
|
|
423
449
|
You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.
|
|
424
450
|
|
|
425
|
-
|
|
426
|
-
// file: ./examples/pm
|
|
427
|
-
const { program } = require('commander');
|
|
451
|
+
Example file: [pm](./examples/pm)
|
|
428
452
|
|
|
453
|
+
```js
|
|
429
454
|
program
|
|
430
455
|
.version('0.1.0')
|
|
431
456
|
.command('install [name]', 'install one or more packages')
|
|
432
457
|
.command('search [query]', 'search with optional query')
|
|
433
|
-
.command('update', 'update installed packages', {executableFile: 'myUpdateSubCommand'})
|
|
434
|
-
.command('list', 'list packages installed', {isDefault: true})
|
|
435
|
-
|
|
458
|
+
.command('update', 'update installed packages', { executableFile: 'myUpdateSubCommand' })
|
|
459
|
+
.command('list', 'list packages installed', { isDefault: true });
|
|
460
|
+
|
|
461
|
+
program.parse(process.argv);
|
|
436
462
|
```
|
|
437
463
|
|
|
438
464
|
If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
|
|
@@ -440,7 +466,9 @@ If the program is designed to be installed globally, make sure the executables h
|
|
|
440
466
|
## Automated help
|
|
441
467
|
|
|
442
468
|
The help information is auto-generated based on the information commander already knows about your program. The default
|
|
443
|
-
help option is `-h,--help`.
|
|
469
|
+
help option is `-h,--help`.
|
|
470
|
+
|
|
471
|
+
Example file: [pizza](./examples/pizza)
|
|
444
472
|
|
|
445
473
|
```bash
|
|
446
474
|
$ node ./examples/pizza --help
|
|
@@ -469,7 +497,9 @@ shell spawn --help
|
|
|
469
497
|
|
|
470
498
|
### Custom help
|
|
471
499
|
|
|
472
|
-
You can display extra information by listening for "--help".
|
|
500
|
+
You can display extra information by listening for "--help".
|
|
501
|
+
|
|
502
|
+
Example file: [custom-help](./examples/custom-help)
|
|
473
503
|
|
|
474
504
|
```js
|
|
475
505
|
program
|
|
@@ -529,7 +559,7 @@ from `--help` listeners.)
|
|
|
529
559
|
|
|
530
560
|
### .helpOption(flags, description)
|
|
531
561
|
|
|
532
|
-
Override the default help flags and description.
|
|
562
|
+
Override the default help flags and description. Pass false to disable the built-in help option.
|
|
533
563
|
|
|
534
564
|
```js
|
|
535
565
|
program
|
|
@@ -597,7 +627,7 @@ There are two new routines to change the behaviour, and the default behaviour ma
|
|
|
597
627
|
- `passCommandToAction`: whether to pass command to action handler,
|
|
598
628
|
or just the options (specify false)
|
|
599
629
|
|
|
600
|
-
|
|
630
|
+
Example file: [storeOptionsAsProperties-action.js](./examples/storeOptionsAsProperties-action.js)
|
|
601
631
|
|
|
602
632
|
```js
|
|
603
633
|
program
|
|
@@ -644,6 +674,17 @@ const program = createCommand();
|
|
|
644
674
|
when creating subcommands using `.command()`, and you may override it to
|
|
645
675
|
customise the new subcommand (examples using [subclass](./examples/custom-command-class.js) and [function](./examples/custom-command-function.js)).
|
|
646
676
|
|
|
677
|
+
### Import into ECMAScript Module
|
|
678
|
+
|
|
679
|
+
Commander is currently a CommonJS package, and the default export can be imported into an ES Module:
|
|
680
|
+
|
|
681
|
+
```js
|
|
682
|
+
// index.mjs
|
|
683
|
+
import commander from 'commander';
|
|
684
|
+
const program = commander.program;
|
|
685
|
+
const newCommand = new commander.Command();
|
|
686
|
+
```
|
|
687
|
+
|
|
647
688
|
### Node options such as `--harmony`
|
|
648
689
|
|
|
649
690
|
You can enable `--harmony` option in two ways:
|
|
@@ -668,7 +709,7 @@ this behaviour and optionally supply a callback. The default override throws a `
|
|
|
668
709
|
The override callback is passed a `CommanderError` with properties `exitCode` number, `code` string, and `message`. The default override behaviour is to throw the error, except for async handling of executable subcommand completion which carries on. The normal display of error messages or version or help
|
|
669
710
|
is not affected by the override which is called after the display.
|
|
670
711
|
|
|
671
|
-
```
|
|
712
|
+
```js
|
|
672
713
|
program.exitOverride();
|
|
673
714
|
|
|
674
715
|
try {
|
|
@@ -680,6 +721,8 @@ try {
|
|
|
680
721
|
|
|
681
722
|
## Examples
|
|
682
723
|
|
|
724
|
+
Example file: [deploy](./examples/deploy)
|
|
725
|
+
|
|
683
726
|
```js
|
|
684
727
|
const { program } = require('commander');
|
|
685
728
|
|
|
@@ -719,13 +762,9 @@ program.parse(process.argv);
|
|
|
719
762
|
|
|
720
763
|
More Demos can be found in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.
|
|
721
764
|
|
|
722
|
-
## License
|
|
723
|
-
|
|
724
|
-
[MIT](https://github.com/tj/commander.js/blob/master/LICENSE)
|
|
725
|
-
|
|
726
765
|
## Support
|
|
727
766
|
|
|
728
|
-
Commander
|
|
767
|
+
The current version of Commander is fully supported on Long Term Support versions of Node, and is likely to work with Node 6 but not tested.
|
|
729
768
|
(For versions of Node below Node 6, use Commander 3.x or 2.x.)
|
|
730
769
|
|
|
731
770
|
The main forum for free and community support is the project [Issues](https://github.com/tj/commander.js/issues) on GitHub.
|