commander 4.0.1 → 4.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 +30 -141
- package/Readme.md +57 -3
- package/index.js +137 -38
- package/package.json +6 -7
- package/typings/index.d.ts +55 -46
package/CHANGELOG.md
CHANGED
|
@@ -7,11 +7,32 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
7
7
|
|
|
8
8
|
<!-- markdownlint-disable MD024 -->
|
|
9
9
|
|
|
10
|
+
## [4.1.0] (2020-01-06)
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- two routines to change how option values are handled, and eliminate name clashes with command properties ([#933] [#1102])
|
|
15
|
+
- see storeOptionsAsProperties and passCommandToAction in README
|
|
16
|
+
- `.parseAsync` to use instead of `.parse` if supply async action handlers ([#806] [#1118])
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
- Remove trailing blanks from wrapped help text ([#1096])
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
|
|
24
|
+
- update dependencies
|
|
25
|
+
- extend security coverage for Commander 2.x to 2020-02-03
|
|
26
|
+
- improvements to README
|
|
27
|
+
- improvements to TypeScript definition documentation
|
|
28
|
+
- move old versions out of main CHANGELOG
|
|
29
|
+
- removed explicit use of `ts-node` in tests
|
|
30
|
+
|
|
10
31
|
## [4.0.1] (2019-11-12)
|
|
11
32
|
|
|
12
33
|
### Fixed
|
|
13
34
|
|
|
14
|
-
* display help when requested, even if there are missing required options [
|
|
35
|
+
* display help when requested, even if there are missing required options ([#1091])
|
|
15
36
|
|
|
16
37
|
## [4.0.0] (2019-11-02)
|
|
17
38
|
|
|
@@ -355,152 +376,16 @@ program
|
|
|
355
376
|
|
|
356
377
|
* remove input methods (.prompt, .confirm, etc)
|
|
357
378
|
|
|
358
|
-
##
|
|
359
|
-
|
|
360
|
-
* add support for sub-commands to co-exist with the original command
|
|
361
|
-
|
|
362
|
-
## 1.3.1 / 2013-07-18
|
|
363
|
-
|
|
364
|
-
* add quick .runningCommand hack so you can opt-out of other logic when running a sub command
|
|
365
|
-
|
|
366
|
-
## 1.3.0 / 2013-07-09
|
|
367
|
-
|
|
368
|
-
* add EACCES error handling
|
|
369
|
-
* fix sub-command --help
|
|
370
|
-
|
|
371
|
-
## 1.2.0 / 2013-06-13
|
|
372
|
-
|
|
373
|
-
* allow "-" hyphen as an option argument
|
|
374
|
-
* support for RegExp coercion
|
|
375
|
-
|
|
376
|
-
## 1.1.1 / 2012-11-20
|
|
377
|
-
|
|
378
|
-
* add more sub-command padding
|
|
379
|
-
* fix .usage() when args are present. Closes #106
|
|
380
|
-
|
|
381
|
-
## 1.1.0 / 2012-11-16
|
|
382
|
-
|
|
383
|
-
* add git-style executable subcommand support. Closes #94
|
|
384
|
-
|
|
385
|
-
## 1.0.5 / 2012-10-09
|
|
386
|
-
|
|
387
|
-
* fix `--name` clobbering. Closes #92
|
|
388
|
-
* fix examples/help. Closes #89
|
|
389
|
-
|
|
390
|
-
## 1.0.4 / 2012-09-03
|
|
391
|
-
|
|
392
|
-
* add `outputHelp()` method.
|
|
393
|
-
|
|
394
|
-
## 1.0.3 / 2012-08-30
|
|
395
|
-
|
|
396
|
-
* remove invalid .version() defaulting
|
|
397
|
-
|
|
398
|
-
## 1.0.2 / 2012-08-24
|
|
399
|
-
|
|
400
|
-
* add `--foo=bar` support [arv]
|
|
401
|
-
* fix password on node 0.8.8. Make backward compatible with 0.6 [focusaurus]
|
|
402
|
-
|
|
403
|
-
## 1.0.1 / 2012-08-03
|
|
404
|
-
|
|
405
|
-
* fix issue #56
|
|
406
|
-
* fix tty.setRawMode(mode) was moved to tty.ReadStream#setRawMode() (i.e. process.stdin.setRawMode())
|
|
407
|
-
|
|
408
|
-
## 1.0.0 / 2012-07-05
|
|
409
|
-
|
|
410
|
-
* add support for optional option descriptions
|
|
411
|
-
* add defaulting of `.version()` to package.json's version
|
|
412
|
-
|
|
413
|
-
## 0.6.1 / 2012-06-01
|
|
414
|
-
|
|
415
|
-
* Added: append (yes or no) on confirmation
|
|
416
|
-
* Added: allow node.js v0.7.x
|
|
417
|
-
|
|
418
|
-
## 0.6.0 / 2012-04-10
|
|
419
|
-
|
|
420
|
-
* Added `.prompt(obj, callback)` support. Closes #49
|
|
421
|
-
* Added default support to .choose(). Closes #41
|
|
422
|
-
* Fixed the choice example
|
|
423
|
-
|
|
424
|
-
## 0.5.1 / 2011-12-20
|
|
425
|
-
|
|
426
|
-
* Fixed `password()` for recent nodes. Closes #36
|
|
427
|
-
|
|
428
|
-
## 0.5.0 / 2011-12-04
|
|
429
|
-
|
|
430
|
-
* Added sub-command option support [itay]
|
|
431
|
-
|
|
432
|
-
## 0.4.3 / 2011-12-04
|
|
433
|
-
|
|
434
|
-
* Fixed custom help ordering. Closes #32
|
|
435
|
-
|
|
436
|
-
## 0.4.2 / 2011-11-24
|
|
437
|
-
|
|
438
|
-
* Added travis support
|
|
439
|
-
* Fixed: line-buffered input automatically trimmed. Closes #31
|
|
440
|
-
|
|
441
|
-
## 0.4.1 / 2011-11-18
|
|
442
|
-
|
|
443
|
-
* Removed listening for "close" on --help
|
|
444
|
-
|
|
445
|
-
## 0.4.0 / 2011-11-15
|
|
446
|
-
|
|
447
|
-
* Added support for `--`. Closes #24
|
|
448
|
-
|
|
449
|
-
## 0.3.3 / 2011-11-14
|
|
450
|
-
|
|
451
|
-
* Fixed: wait for close event when writing help info [Jerry Hamlet]
|
|
452
|
-
|
|
453
|
-
## 0.3.2 / 2011-11-01
|
|
454
|
-
|
|
455
|
-
* Fixed long flag definitions with values [felixge]
|
|
456
|
-
|
|
457
|
-
## 0.3.1 / 2011-10-31
|
|
458
|
-
|
|
459
|
-
* Changed `--version` short flag to `-V` from `-v`
|
|
460
|
-
* Changed `.version()` so it's configurable [felixge]
|
|
461
|
-
|
|
462
|
-
## 0.3.0 / 2011-10-31
|
|
463
|
-
|
|
464
|
-
* Added support for long flags only. Closes #18
|
|
465
|
-
|
|
466
|
-
## 0.2.1 / 2011-10-24
|
|
467
|
-
|
|
468
|
-
* "node": ">= 0.4.x < 0.7.0". Closes #20
|
|
469
|
-
|
|
470
|
-
## 0.2.0 / 2011-09-26
|
|
471
|
-
|
|
472
|
-
* Allow for defaults that are not just boolean. Default peassignment only occurs for --no-*, optional, and required arguments. [Jim Isaacs]
|
|
473
|
-
|
|
474
|
-
## 0.1.0 / 2011-08-24
|
|
475
|
-
|
|
476
|
-
* Added support for custom `--help` output
|
|
477
|
-
|
|
478
|
-
## 0.0.5 / 2011-08-18
|
|
479
|
-
|
|
480
|
-
* Changed: when the user enters nothing prompt for password again
|
|
481
|
-
* Fixed issue with passwords beginning with numbers [NuckChorris]
|
|
482
|
-
|
|
483
|
-
## 0.0.4 / 2011-08-15
|
|
484
|
-
|
|
485
|
-
* Fixed `Commander#args`
|
|
486
|
-
|
|
487
|
-
## 0.0.3 / 2011-08-15
|
|
488
|
-
|
|
489
|
-
* Added default option value support
|
|
490
|
-
|
|
491
|
-
## 0.0.2 / 2011-08-15
|
|
492
|
-
|
|
493
|
-
* Added mask support to `Command#password(str[, mask], fn)`
|
|
494
|
-
* Added `Command#password(str, fn)`
|
|
495
|
-
|
|
496
|
-
## 0.0.1 / 2010-01-03
|
|
379
|
+
## Older versions
|
|
497
380
|
|
|
498
|
-
*
|
|
381
|
+
* [1.x](./changelogs/CHANGELOG-1.md)
|
|
382
|
+
* [0.x](./changelogs/CHANGELOG-0.md)
|
|
499
383
|
|
|
500
384
|
[#599]: https://github.com/tj/commander.js/issues/599
|
|
501
385
|
[#611]: https://github.com/tj/commander.js/issues/611
|
|
502
386
|
[#697]: https://github.com/tj/commander.js/issues/697
|
|
503
387
|
[#795]: https://github.com/tj/commander.js/issues/795
|
|
388
|
+
[#806]: https://github.com/tj/commander.js/issues/806
|
|
504
389
|
[#915]: https://github.com/tj/commander.js/issues/915
|
|
505
390
|
[#938]: https://github.com/tj/commander.js/issues/938
|
|
506
391
|
[#963]: https://github.com/tj/commander.js/issues/963
|
|
@@ -509,6 +394,7 @@ program
|
|
|
509
394
|
[#987]: https://github.com/tj/commander.js/issues/987
|
|
510
395
|
[#990]: https://github.com/tj/commander.js/issues/990
|
|
511
396
|
[#991]: https://github.com/tj/commander.js/issues/991
|
|
397
|
+
[#993]: https://github.com/tj/commander.js/issues/993
|
|
512
398
|
[#999]: https://github.com/tj/commander.js/issues/999
|
|
513
399
|
[#1010]: https://github.com/tj/commander.js/pull/1010
|
|
514
400
|
[#1018]: https://github.com/tj/commander.js/pull/1018
|
|
@@ -526,6 +412,9 @@ program
|
|
|
526
412
|
[#1071]: https://github.com/tj/commander.js/pull/1071
|
|
527
413
|
[#1081]: https://github.com/tj/commander.js/pull/1081
|
|
528
414
|
[#1091]: https://github.com/tj/commander.js/pull/1091
|
|
415
|
+
[#1096]: https://github.com/tj/commander.js/pull/1096
|
|
416
|
+
[#1102]: https://github.com/tj/commander.js/pull/1102
|
|
417
|
+
[#1118]: https://github.com/tj/commander.js/pull/1118
|
|
529
418
|
|
|
530
419
|
[Unreleased]: https://github.com/tj/commander.js/compare/master...develop
|
|
531
420
|
[4.0.1]: https://github.com/tj/commander.js/compare/v4.0.0..v4.0.1
|
package/Readme.md
CHANGED
|
@@ -11,7 +11,7 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
|
|
|
11
11
|
|
|
12
12
|
- [Commander.js](#commanderjs)
|
|
13
13
|
- [Installation](#installation)
|
|
14
|
-
- [Declaring
|
|
14
|
+
- [Declaring program variable](#declaring-program-variable)
|
|
15
15
|
- [Options](#options)
|
|
16
16
|
- [Common option types, boolean and value](#common-option-types-boolean-and-value)
|
|
17
17
|
- [Default option value](#default-option-value)
|
|
@@ -31,8 +31,9 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
|
|
|
31
31
|
- [.help(cb)](#helpcb)
|
|
32
32
|
- [Custom event listeners](#custom-event-listeners)
|
|
33
33
|
- [Bits and pieces](#bits-and-pieces)
|
|
34
|
+
- [Avoiding option name clashes](#avoiding-option-name-clashes)
|
|
34
35
|
- [TypeScript](#typescript)
|
|
35
|
-
- [Node options such as
|
|
36
|
+
- [Node options such as --harmony](#node-options-such-as---harmony)
|
|
36
37
|
- [Node debugging](#node-debugging)
|
|
37
38
|
- [Override exit handling](#override-exit-handling)
|
|
38
39
|
- [Examples](#examples)
|
|
@@ -70,6 +71,8 @@ Options are defined with the `.option()` method, also serving as documentation f
|
|
|
70
71
|
|
|
71
72
|
The options can be accessed as properties on the Command object. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc. Multiple short flags may be combined as a single arg, for example `-abc` is equivalent to `-a -b -c`.
|
|
72
73
|
|
|
74
|
+
See also optional new behaviour to [avoid name clashes](#avoiding-option-name-clashes).
|
|
75
|
+
|
|
73
76
|
### Common option types, boolean and value
|
|
74
77
|
|
|
75
78
|
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.
|
|
@@ -374,6 +377,19 @@ program
|
|
|
374
377
|
program.parse(process.argv)
|
|
375
378
|
```
|
|
376
379
|
|
|
380
|
+
You may supply an `async` action handler, in which case you call `.parseAsync` rather than `.parse`.
|
|
381
|
+
|
|
382
|
+
```js
|
|
383
|
+
async function run() { /* code goes here */ }
|
|
384
|
+
|
|
385
|
+
async function main() {
|
|
386
|
+
program
|
|
387
|
+
.command('run')
|
|
388
|
+
.action(run);
|
|
389
|
+
await program.parseAsync(process.argv);
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
377
393
|
A command's options on the command line are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated.
|
|
378
394
|
|
|
379
395
|
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output.
|
|
@@ -550,6 +566,43 @@ program.on('command:*', function () {
|
|
|
550
566
|
|
|
551
567
|
## Bits and pieces
|
|
552
568
|
|
|
569
|
+
### Avoiding option name clashes
|
|
570
|
+
|
|
571
|
+
The original and default behaviour is that the option values are stored
|
|
572
|
+
as properties on the program, and the action handler is passed a
|
|
573
|
+
command object with the options values stored as properties.
|
|
574
|
+
This is very convenient to code, but the downside is possible clashes with
|
|
575
|
+
existing properties of Command.
|
|
576
|
+
|
|
577
|
+
There are two new routines to change the behaviour, and the default behaviour may change in the future:
|
|
578
|
+
|
|
579
|
+
- `storeOptionsAsProperties`: whether to store option values as properties on command object, or store separately (specify false) and access using `.opts()`
|
|
580
|
+
- `passCommandToAction`: whether to pass command to action handler,
|
|
581
|
+
or just the options (specify false)
|
|
582
|
+
|
|
583
|
+
```js
|
|
584
|
+
// file: ./examples/storeOptionsAsProperties.action.js
|
|
585
|
+
program
|
|
586
|
+
.storeOptionsAsProperties(false)
|
|
587
|
+
.passCommandToAction(false);
|
|
588
|
+
|
|
589
|
+
program
|
|
590
|
+
.name('my-program-name')
|
|
591
|
+
.option('-n,--name <name>');
|
|
592
|
+
|
|
593
|
+
program
|
|
594
|
+
.command('show')
|
|
595
|
+
.option('-a,--action <action>')
|
|
596
|
+
.action((options) => {
|
|
597
|
+
console.log(options.action);
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
program.parse(process.argv);
|
|
601
|
+
|
|
602
|
+
const programOptions = program.opts();
|
|
603
|
+
console.log(programOptions.name);
|
|
604
|
+
```
|
|
605
|
+
|
|
553
606
|
### TypeScript
|
|
554
607
|
|
|
555
608
|
The Commander package includes its TypeScript Definition file, but also requires the node types which you need to install yourself. e.g.
|
|
@@ -648,7 +701,8 @@ More Demos can be found in the [examples](https://github.com/tj/commander.js/tre
|
|
|
648
701
|
|
|
649
702
|
## Support
|
|
650
703
|
|
|
651
|
-
Commander is supported on Node 8 and above
|
|
704
|
+
Commander 4.x is supported on Node 8 and above, and is likely to work with Node 6 but not tested.
|
|
705
|
+
(For versions of Node below Node 6, use Commander 3.x or 2.x.)
|
|
652
706
|
|
|
653
707
|
The main forum for free and community support is the project [Issues](https://github.com/tj/commander.js/issues) on GitHub.
|
|
654
708
|
|
package/index.js
CHANGED
|
@@ -115,7 +115,7 @@ exports.CommanderError = CommanderError;
|
|
|
115
115
|
/**
|
|
116
116
|
* Initialize a new `Command`.
|
|
117
117
|
*
|
|
118
|
-
* @param {String} name
|
|
118
|
+
* @param {String} [name]
|
|
119
119
|
* @api public
|
|
120
120
|
*/
|
|
121
121
|
|
|
@@ -126,6 +126,10 @@ function Command(name) {
|
|
|
126
126
|
this._allowUnknownOption = false;
|
|
127
127
|
this._args = [];
|
|
128
128
|
this._name = name || '';
|
|
129
|
+
this._optionValues = {};
|
|
130
|
+
this._storeOptionsAsProperties = true; // backwards compatible by default
|
|
131
|
+
this._passCommandToAction = true; // backwards compatible by default
|
|
132
|
+
this._actionResults = [];
|
|
129
133
|
|
|
130
134
|
this._helpFlags = '-h, --help';
|
|
131
135
|
this._helpDescription = 'output usage information';
|
|
@@ -151,7 +155,7 @@ function Command(name) {
|
|
|
151
155
|
* // Command implemented using separate executable file (description is second parameter to `.command`)
|
|
152
156
|
* program
|
|
153
157
|
* .command('start <service>', 'start named service')
|
|
154
|
-
* .command('stop [service]', 'stop named
|
|
158
|
+
* .command('stop [service]', 'stop named service, or all if no name supplied');
|
|
155
159
|
*
|
|
156
160
|
* @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|
157
161
|
* @param {Object|string} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
|
|
@@ -183,6 +187,8 @@ Command.prototype.command = function(nameAndArgs, actionOptsOrExecDesc, execOpts
|
|
|
183
187
|
cmd._helpShortFlag = this._helpShortFlag;
|
|
184
188
|
cmd._helpLongFlag = this._helpLongFlag;
|
|
185
189
|
cmd._exitCallback = this._exitCallback;
|
|
190
|
+
cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;
|
|
191
|
+
cmd._passCommandToAction = this._passCommandToAction;
|
|
186
192
|
|
|
187
193
|
cmd._executableFile = opts.executableFile; // Custom name for executable file
|
|
188
194
|
this.commands.push(cmd);
|
|
@@ -323,7 +329,7 @@ Command.prototype.action = function(fn) {
|
|
|
323
329
|
var parsed = self.parseOptions(unknown);
|
|
324
330
|
|
|
325
331
|
// Output help if necessary
|
|
326
|
-
|
|
332
|
+
outputHelpIfRequested(self, parsed.unknown);
|
|
327
333
|
self._checkForMissingMandatoryOptions();
|
|
328
334
|
|
|
329
335
|
// If there are still any unknown options, then we simply
|
|
@@ -351,13 +357,23 @@ Command.prototype.action = function(fn) {
|
|
|
351
357
|
// The .action callback takes an extra parameter which is the command itself.
|
|
352
358
|
var expectedArgsCount = self._args.length;
|
|
353
359
|
var actionArgs = args.slice(0, expectedArgsCount);
|
|
354
|
-
|
|
360
|
+
if (self._passCommandToAction) {
|
|
361
|
+
actionArgs[expectedArgsCount] = self;
|
|
362
|
+
} else {
|
|
363
|
+
actionArgs[expectedArgsCount] = self.opts();
|
|
364
|
+
}
|
|
355
365
|
// Add the extra arguments so available too.
|
|
356
366
|
if (args.length > expectedArgsCount) {
|
|
357
367
|
actionArgs.push(args.slice(expectedArgsCount));
|
|
358
368
|
}
|
|
359
369
|
|
|
360
|
-
fn.apply(self, actionArgs);
|
|
370
|
+
const actionResult = fn.apply(self, actionArgs);
|
|
371
|
+
// Remember result in case it is async. Assume parseAsync getting called on root.
|
|
372
|
+
let rootCommand = self;
|
|
373
|
+
while (rootCommand.parent) {
|
|
374
|
+
rootCommand = rootCommand.parent;
|
|
375
|
+
}
|
|
376
|
+
rootCommand._actionResults.push(actionResult);
|
|
361
377
|
};
|
|
362
378
|
var parent = this.parent || this;
|
|
363
379
|
var name = parent === this ? '*' : this._name;
|
|
@@ -405,12 +421,12 @@ Command.prototype._optionEx = function(config, flags, description, fn, defaultVa
|
|
|
405
421
|
if (option.negate || option.optional || option.required || typeof defaultValue === 'boolean') {
|
|
406
422
|
// when --no-foo we make sure default is true, unless a --foo option is already defined
|
|
407
423
|
if (option.negate) {
|
|
408
|
-
|
|
409
|
-
defaultValue =
|
|
424
|
+
const positiveLongFlag = option.long.replace(/^--no-/, '--');
|
|
425
|
+
defaultValue = self.optionFor(positiveLongFlag) ? self._getOptionValue(name) : true;
|
|
410
426
|
}
|
|
411
427
|
// preassign only if we have a default
|
|
412
428
|
if (defaultValue !== undefined) {
|
|
413
|
-
self
|
|
429
|
+
self._setOptionValue(name, defaultValue);
|
|
414
430
|
option.defaultValue = defaultValue;
|
|
415
431
|
}
|
|
416
432
|
}
|
|
@@ -423,22 +439,22 @@ Command.prototype._optionEx = function(config, flags, description, fn, defaultVa
|
|
|
423
439
|
this.on('option:' + oname, function(val) {
|
|
424
440
|
// coercion
|
|
425
441
|
if (val !== null && fn) {
|
|
426
|
-
val = fn(val, self
|
|
442
|
+
val = fn(val, self._getOptionValue(name) === undefined ? defaultValue : self._getOptionValue(name));
|
|
427
443
|
}
|
|
428
444
|
|
|
429
445
|
// unassigned or boolean value
|
|
430
|
-
if (typeof self
|
|
446
|
+
if (typeof self._getOptionValue(name) === 'boolean' || typeof self._getOptionValue(name) === 'undefined') {
|
|
431
447
|
// if no value, negate false, and we have a default, then use it!
|
|
432
448
|
if (val == null) {
|
|
433
|
-
self
|
|
449
|
+
self._setOptionValue(name, option.negate
|
|
434
450
|
? false
|
|
435
|
-
: defaultValue || true;
|
|
451
|
+
: defaultValue || true);
|
|
436
452
|
} else {
|
|
437
|
-
self
|
|
453
|
+
self._setOptionValue(name, val);
|
|
438
454
|
}
|
|
439
455
|
} else if (val !== null) {
|
|
440
456
|
// reassign
|
|
441
|
-
self
|
|
457
|
+
self._setOptionValue(name, option.negate ? false : val);
|
|
442
458
|
}
|
|
443
459
|
});
|
|
444
460
|
|
|
@@ -532,7 +548,70 @@ Command.prototype.allowUnknownOption = function(arg) {
|
|
|
532
548
|
};
|
|
533
549
|
|
|
534
550
|
/**
|
|
535
|
-
|
|
551
|
+
* Whether to store option values as properties on command object,
|
|
552
|
+
* or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|
553
|
+
*
|
|
554
|
+
* @param {boolean} value
|
|
555
|
+
* @return {Command} Command for chaining
|
|
556
|
+
* @api public
|
|
557
|
+
*/
|
|
558
|
+
|
|
559
|
+
Command.prototype.storeOptionsAsProperties = function(value) {
|
|
560
|
+
this._storeOptionsAsProperties = (value === undefined) || value;
|
|
561
|
+
if (this.options.length) {
|
|
562
|
+
// This is for programmer, not end user.
|
|
563
|
+
console.error('Commander usage error: call storeOptionsAsProperties before adding options');
|
|
564
|
+
}
|
|
565
|
+
return this;
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Whether to pass command to action handler,
|
|
570
|
+
* or just the options (specify false).
|
|
571
|
+
*
|
|
572
|
+
* @param {boolean} value
|
|
573
|
+
* @return {Command} Command for chaining
|
|
574
|
+
* @api public
|
|
575
|
+
*/
|
|
576
|
+
|
|
577
|
+
Command.prototype.passCommandToAction = function(value) {
|
|
578
|
+
this._passCommandToAction = (value === undefined) || value;
|
|
579
|
+
return this;
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Store option value
|
|
584
|
+
*
|
|
585
|
+
* @param {String} key
|
|
586
|
+
* @param {Object} value
|
|
587
|
+
* @api private
|
|
588
|
+
*/
|
|
589
|
+
|
|
590
|
+
Command.prototype._setOptionValue = function(key, value) {
|
|
591
|
+
if (this._storeOptionsAsProperties) {
|
|
592
|
+
this[key] = value;
|
|
593
|
+
} else {
|
|
594
|
+
this._optionValues[key] = value;
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Retrieve option value
|
|
600
|
+
*
|
|
601
|
+
* @param {String} key
|
|
602
|
+
* @return {Object} value
|
|
603
|
+
* @api private
|
|
604
|
+
*/
|
|
605
|
+
|
|
606
|
+
Command.prototype._getOptionValue = function(key) {
|
|
607
|
+
if (this._storeOptionsAsProperties) {
|
|
608
|
+
return this[key];
|
|
609
|
+
}
|
|
610
|
+
return this._optionValues[key];
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Parse `argv`, setting options and invoking commands when defined.
|
|
536
615
|
*
|
|
537
616
|
* @param {Array} argv
|
|
538
617
|
* @return {Command} for chaining
|
|
@@ -616,13 +695,27 @@ Command.prototype.parse = function(argv) {
|
|
|
616
695
|
return result;
|
|
617
696
|
};
|
|
618
697
|
|
|
698
|
+
/**
|
|
699
|
+
* Parse `argv`, setting options and invoking commands when defined.
|
|
700
|
+
*
|
|
701
|
+
* Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
|
|
702
|
+
*
|
|
703
|
+
* @param {Array} argv
|
|
704
|
+
* @return {Promise}
|
|
705
|
+
* @api public
|
|
706
|
+
*/
|
|
707
|
+
Command.prototype.parseAsync = function(argv) {
|
|
708
|
+
this.parse(argv);
|
|
709
|
+
return Promise.all(this._actionResults);
|
|
710
|
+
};
|
|
711
|
+
|
|
619
712
|
/**
|
|
620
713
|
* Execute a sub-command executable.
|
|
621
714
|
*
|
|
622
715
|
* @param {Array} argv
|
|
623
716
|
* @param {Array} args
|
|
624
717
|
* @param {Array} unknown
|
|
625
|
-
* @param {String}
|
|
718
|
+
* @param {String} executableFile
|
|
626
719
|
* @api private
|
|
627
720
|
*/
|
|
628
721
|
|
|
@@ -801,7 +894,7 @@ Command.prototype.parseArgs = function(args, unknown) {
|
|
|
801
894
|
this.emit('command:*', args, unknown);
|
|
802
895
|
}
|
|
803
896
|
} else {
|
|
804
|
-
|
|
897
|
+
outputHelpIfRequested(this, unknown);
|
|
805
898
|
|
|
806
899
|
// If there were no args and we have unknown options,
|
|
807
900
|
// then they are extraneous and we need to error.
|
|
@@ -843,7 +936,7 @@ Command.prototype._checkForMissingMandatoryOptions = function() {
|
|
|
843
936
|
// Walk up hierarchy so can call from action handler after checking for displaying help.
|
|
844
937
|
for (var cmd = this; cmd; cmd = cmd.parent) {
|
|
845
938
|
cmd.options.forEach((anOption) => {
|
|
846
|
-
if (anOption.mandatory && (cmd
|
|
939
|
+
if (anOption.mandatory && (cmd._getOptionValue(anOption.attributeName()) === undefined)) {
|
|
847
940
|
cmd.missingMandatoryOptionValue(anOption);
|
|
848
941
|
}
|
|
849
942
|
});
|
|
@@ -855,7 +948,7 @@ Command.prototype._checkForMissingMandatoryOptions = function() {
|
|
|
855
948
|
* void of these options.
|
|
856
949
|
*
|
|
857
950
|
* @param {Array} argv
|
|
858
|
-
* @return {Array}
|
|
951
|
+
* @return {{args: Array, unknown: Array}}
|
|
859
952
|
* @api public
|
|
860
953
|
*/
|
|
861
954
|
|
|
@@ -936,14 +1029,19 @@ Command.prototype.parseOptions = function(argv) {
|
|
|
936
1029
|
* @api public
|
|
937
1030
|
*/
|
|
938
1031
|
Command.prototype.opts = function() {
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
1032
|
+
if (this._storeOptionsAsProperties) {
|
|
1033
|
+
// Preserve original behaviour so backwards compatible when still using properties
|
|
1034
|
+
var result = {},
|
|
1035
|
+
len = this.options.length;
|
|
1036
|
+
|
|
1037
|
+
for (var i = 0; i < len; i++) {
|
|
1038
|
+
var key = this.options[i].attributeName();
|
|
1039
|
+
result[key] = key === this._versionOptionName ? this._version : this[key];
|
|
1040
|
+
}
|
|
1041
|
+
return result;
|
|
945
1042
|
}
|
|
946
|
-
|
|
1043
|
+
|
|
1044
|
+
return this._optionValues;
|
|
947
1045
|
};
|
|
948
1046
|
|
|
949
1047
|
/**
|
|
@@ -962,8 +1060,8 @@ Command.prototype.missingArgument = function(name) {
|
|
|
962
1060
|
/**
|
|
963
1061
|
* `Option` is missing an argument, but received `flag` or nothing.
|
|
964
1062
|
*
|
|
965
|
-
* @param {
|
|
966
|
-
* @param {String} flag
|
|
1063
|
+
* @param {Option} option
|
|
1064
|
+
* @param {String} [flag]
|
|
967
1065
|
* @api private
|
|
968
1066
|
*/
|
|
969
1067
|
|
|
@@ -981,7 +1079,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
|
|
|
981
1079
|
/**
|
|
982
1080
|
* `Option` does not have a value, and is a mandatory option.
|
|
983
1081
|
*
|
|
984
|
-
* @param {
|
|
1082
|
+
* @param {Option} option
|
|
985
1083
|
* @api private
|
|
986
1084
|
*/
|
|
987
1085
|
|
|
@@ -1041,9 +1139,10 @@ Command.prototype.version = function(str, flags, description) {
|
|
|
1041
1139
|
var versionOption = new Option(flags, description);
|
|
1042
1140
|
this._versionOptionName = versionOption.long.substr(2) || 'version';
|
|
1043
1141
|
this.options.push(versionOption);
|
|
1142
|
+
var self = this;
|
|
1044
1143
|
this.on('option:' + this._versionOptionName, function() {
|
|
1045
1144
|
process.stdout.write(str + '\n');
|
|
1046
|
-
|
|
1145
|
+
self._exit(0, 'commander.version', str);
|
|
1047
1146
|
});
|
|
1048
1147
|
return this;
|
|
1049
1148
|
};
|
|
@@ -1052,7 +1151,7 @@ Command.prototype.version = function(str, flags, description) {
|
|
|
1052
1151
|
* Set the description to `str`.
|
|
1053
1152
|
*
|
|
1054
1153
|
* @param {String} str
|
|
1055
|
-
* @param {Object} argsDescription
|
|
1154
|
+
* @param {Object} [argsDescription]
|
|
1056
1155
|
* @return {String|Command}
|
|
1057
1156
|
* @api public
|
|
1058
1157
|
*/
|
|
@@ -1089,7 +1188,7 @@ Command.prototype.alias = function(alias) {
|
|
|
1089
1188
|
/**
|
|
1090
1189
|
* Set / get the command usage `str`.
|
|
1091
1190
|
*
|
|
1092
|
-
* @param {String} str
|
|
1191
|
+
* @param {String} [str]
|
|
1093
1192
|
* @return {String|Command}
|
|
1094
1193
|
* @api public
|
|
1095
1194
|
*/
|
|
@@ -1112,7 +1211,7 @@ Command.prototype.usage = function(str) {
|
|
|
1112
1211
|
/**
|
|
1113
1212
|
* Get or set the name of the command
|
|
1114
1213
|
*
|
|
1115
|
-
* @param {String} str
|
|
1214
|
+
* @param {String} [str]
|
|
1116
1215
|
* @return {String|Command}
|
|
1117
1216
|
* @api public
|
|
1118
1217
|
*/
|
|
@@ -1429,7 +1528,7 @@ function wrap(str, width, indent) {
|
|
|
1429
1528
|
if (line.slice(-1) === '\n') {
|
|
1430
1529
|
line = line.slice(0, line.length - 1);
|
|
1431
1530
|
}
|
|
1432
|
-
return ((i > 0 && indent) ? Array(indent + 1).join(' ') : '') + line;
|
|
1531
|
+
return ((i > 0 && indent) ? Array(indent + 1).join(' ') : '') + line.trimRight();
|
|
1433
1532
|
}).join('\n');
|
|
1434
1533
|
}
|
|
1435
1534
|
|
|
@@ -1456,14 +1555,14 @@ function optionalWrap(str, width, indent) {
|
|
|
1456
1555
|
}
|
|
1457
1556
|
|
|
1458
1557
|
/**
|
|
1459
|
-
* Output help information if
|
|
1558
|
+
* Output help information if help flags specified
|
|
1460
1559
|
*
|
|
1461
|
-
* @param {Command} command to output help for
|
|
1462
|
-
* @param {Array} array of options to search for -h or --help
|
|
1560
|
+
* @param {Command} cmd - command to output help for
|
|
1561
|
+
* @param {Array} options - array of options to search for -h or --help
|
|
1463
1562
|
* @api private
|
|
1464
1563
|
*/
|
|
1465
1564
|
|
|
1466
|
-
function
|
|
1565
|
+
function outputHelpIfRequested(cmd, options) {
|
|
1467
1566
|
options = options || [];
|
|
1468
1567
|
|
|
1469
1568
|
for (var i = 0; i < options.length; i++) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "commander",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "the complete solution for node.js command-line programs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"commander",
|
|
@@ -26,14 +26,13 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/jest": "^24.0.
|
|
30
|
-
"@types/node": "^12.
|
|
31
|
-
"eslint": "^6.
|
|
32
|
-
"eslint-plugin-jest": "^22.
|
|
29
|
+
"@types/jest": "^24.0.23",
|
|
30
|
+
"@types/node": "^12.12.11",
|
|
31
|
+
"eslint": "^6.7.0",
|
|
32
|
+
"eslint-plugin-jest": "^22.21.0",
|
|
33
33
|
"jest": "^24.8.0",
|
|
34
34
|
"standard": "^14.3.1",
|
|
35
|
-
"
|
|
36
|
-
"typescript": "^3.6.3"
|
|
35
|
+
"typescript": "^3.7.2"
|
|
37
36
|
},
|
|
38
37
|
"typings": "typings/index.d.ts",
|
|
39
38
|
"engines": {
|
package/typings/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
// Type definitions for commander
|
|
2
|
-
//
|
|
3
|
-
// Definitions by: Alan Agius <https://github.com/alan-agius4>, Marcelo Dezem <https://github.com/mdezem>, vvakame <https://github.com/vvakame>, Jules Randolph <https://github.com/sveinburne>
|
|
4
|
-
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
1
|
+
// Type definitions for commander
|
|
2
|
+
// Original definitions by: Alan Agius <https://github.com/alan-agius4>, Marcelo Dezem <https://github.com/mdezem>, vvakame <https://github.com/vvakame>, Jules Randolph <https://github.com/sveinburne>
|
|
5
3
|
|
|
6
4
|
///<reference types="node" />
|
|
7
5
|
|
|
@@ -39,7 +37,6 @@ declare namespace commander {
|
|
|
39
37
|
* which will print the version number when passed.
|
|
40
38
|
*
|
|
41
39
|
* You can optionally supply the flags and description to override the defaults.
|
|
42
|
-
*
|
|
43
40
|
*/
|
|
44
41
|
version(str: string, flags?: string, description?: string): Command;
|
|
45
42
|
|
|
@@ -87,8 +84,7 @@ declare namespace commander {
|
|
|
87
84
|
/**
|
|
88
85
|
* Define argument syntax for the top-level command.
|
|
89
86
|
*
|
|
90
|
-
* @
|
|
91
|
-
* @returns {Command} for chaining
|
|
87
|
+
* @returns Command for chaining
|
|
92
88
|
*/
|
|
93
89
|
arguments(desc: string): Command;
|
|
94
90
|
|
|
@@ -97,8 +93,7 @@ declare namespace commander {
|
|
|
97
93
|
*
|
|
98
94
|
* For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
|
|
99
95
|
*
|
|
100
|
-
* @
|
|
101
|
-
* @returns {Command} for chaining
|
|
96
|
+
* @returns Command for chaining
|
|
102
97
|
*/
|
|
103
98
|
parseExpectedArgs(args: string[]): Command;
|
|
104
99
|
|
|
@@ -118,8 +113,7 @@ declare namespace commander {
|
|
|
118
113
|
* // output help here
|
|
119
114
|
* });
|
|
120
115
|
*
|
|
121
|
-
* @
|
|
122
|
-
* @returns {Command} for chaining
|
|
116
|
+
* @returns Command for chaining
|
|
123
117
|
*/
|
|
124
118
|
action(fn: (...args: any[]) => void): Command;
|
|
125
119
|
|
|
@@ -163,11 +157,7 @@ declare namespace commander {
|
|
|
163
157
|
* // optional argument
|
|
164
158
|
* program.option('-c, --cheese [type]', 'add cheese [marble]');
|
|
165
159
|
*
|
|
166
|
-
* @
|
|
167
|
-
* @param {string} [description]
|
|
168
|
-
* @param {((arg1: any, arg2: any) => void) | RegExp} [fn] function or default
|
|
169
|
-
* @param {*} [defaultValue]
|
|
170
|
-
* @returns {Command} for chaining
|
|
160
|
+
* @returns Command for chaining
|
|
171
161
|
*/
|
|
172
162
|
option(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
|
|
173
163
|
option(flags: string, description?: string, defaultValue?: any): Command;
|
|
@@ -181,77 +171,98 @@ declare namespace commander {
|
|
|
181
171
|
requiredOption(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
|
|
182
172
|
requiredOption(flags: string, description?: string, defaultValue?: any): Command;
|
|
183
173
|
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Whether to store option values as properties on command object,
|
|
177
|
+
* or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|
178
|
+
*
|
|
179
|
+
* @return Command for chaining
|
|
180
|
+
*/
|
|
181
|
+
storeOptionsAsProperties(value?: boolean): Command;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Whether to pass command to action handler,
|
|
185
|
+
* or just the options (specify false).
|
|
186
|
+
*
|
|
187
|
+
* @return Command for chaining
|
|
188
|
+
*/
|
|
189
|
+
passCommandToAction(value?: boolean): Command;
|
|
190
|
+
|
|
184
191
|
/**
|
|
185
192
|
* Allow unknown options on the command line.
|
|
186
193
|
*
|
|
187
|
-
* @param
|
|
188
|
-
* @returns
|
|
194
|
+
* @param [arg] if `true` or omitted, no error will be thrown for unknown options.
|
|
195
|
+
* @returns Command for chaining
|
|
189
196
|
*/
|
|
190
197
|
allowUnknownOption(arg?: boolean): Command;
|
|
191
198
|
|
|
192
199
|
/**
|
|
193
|
-
* Parse `argv`,
|
|
200
|
+
* Parse `argv`, setting options and invoking commands when defined.
|
|
194
201
|
*
|
|
195
|
-
* @
|
|
196
|
-
* @returns {Command} for chaining
|
|
202
|
+
* @returns Command for chaining
|
|
197
203
|
*/
|
|
198
204
|
parse(argv: string[]): Command;
|
|
199
205
|
|
|
200
206
|
/**
|
|
201
|
-
* Parse
|
|
207
|
+
* Parse `argv`, setting options and invoking commands when defined.
|
|
208
|
+
*
|
|
209
|
+
* Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
|
|
202
210
|
*
|
|
203
|
-
* @
|
|
204
|
-
|
|
211
|
+
* @returns Promise
|
|
212
|
+
*/
|
|
213
|
+
parseAsync(argv: string[]): Promise<any>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Parse options from `argv` returning `argv` void of these options.
|
|
205
217
|
*/
|
|
206
218
|
parseOptions(argv: string[]): commander.ParseOptionsResult;
|
|
207
219
|
|
|
208
220
|
/**
|
|
209
221
|
* Return an object containing options as key-value pairs
|
|
210
|
-
*
|
|
211
|
-
* @returns {{[key: string]: any}}
|
|
212
222
|
*/
|
|
213
223
|
opts(): { [key: string]: any };
|
|
214
224
|
|
|
215
225
|
/**
|
|
216
|
-
* Set the description
|
|
217
|
-
*
|
|
218
|
-
* @
|
|
219
|
-
* @param {{[argName: string]: string}} argsDescription
|
|
220
|
-
* @return {(Command | string)}
|
|
226
|
+
* Set the description.
|
|
227
|
+
*
|
|
228
|
+
* @returns Command for chaining
|
|
221
229
|
*/
|
|
222
230
|
description(str: string, argsDescription?: {[argName: string]: string}): Command;
|
|
231
|
+
/**
|
|
232
|
+
* Get the description.
|
|
233
|
+
*/
|
|
223
234
|
description(): string;
|
|
224
235
|
|
|
225
236
|
/**
|
|
226
237
|
* Set an alias for the command.
|
|
227
|
-
*
|
|
228
|
-
* @
|
|
229
|
-
* @return {(Command | string)}
|
|
238
|
+
*
|
|
239
|
+
* @returns Command for chaining
|
|
230
240
|
*/
|
|
231
241
|
alias(alias: string): Command;
|
|
242
|
+
/**
|
|
243
|
+
* Get alias for the command.
|
|
244
|
+
*/
|
|
232
245
|
alias(): string;
|
|
233
246
|
|
|
234
247
|
/**
|
|
235
|
-
* Set
|
|
236
|
-
*
|
|
237
|
-
* @
|
|
238
|
-
* @return {(Command | string)}
|
|
248
|
+
* Set the command usage.
|
|
249
|
+
*
|
|
250
|
+
* @returns Command for chaining
|
|
239
251
|
*/
|
|
240
252
|
usage(str: string): Command;
|
|
253
|
+
/**
|
|
254
|
+
* Get the command usage.
|
|
255
|
+
*/
|
|
241
256
|
usage(): string;
|
|
242
257
|
|
|
243
258
|
/**
|
|
244
259
|
* Set the name of the command.
|
|
245
|
-
*
|
|
246
|
-
* @
|
|
247
|
-
* @return {Command}
|
|
260
|
+
*
|
|
261
|
+
* @returns Command for chaining
|
|
248
262
|
*/
|
|
249
263
|
name(str: string): Command;
|
|
250
|
-
|
|
251
264
|
/**
|
|
252
265
|
* Get the name of the command.
|
|
253
|
-
*
|
|
254
|
-
* @return {string}
|
|
255
266
|
*/
|
|
256
267
|
name(): string;
|
|
257
268
|
|
|
@@ -260,8 +271,6 @@ declare namespace commander {
|
|
|
260
271
|
*
|
|
261
272
|
* When listener(s) are available for the helpLongFlag
|
|
262
273
|
* those callbacks are invoked.
|
|
263
|
-
*
|
|
264
|
-
* @param {(str: string) => string} [cb]
|
|
265
274
|
*/
|
|
266
275
|
outputHelp(cb?: (str: string) => string): void;
|
|
267
276
|
|