commander 3.0.0 → 3.0.1
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 +44 -14
- package/Readme.md +69 -27
- package/index.js +9 -8
- package/package.json +5 -5
- package/typings/index.d.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|
6
|
+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). (Format adopted after v3.0.0.)
|
|
7
|
+
|
|
8
|
+
## [3.0.1] (2019-08-30)
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
* .name and .usage to README ([#1010])
|
|
13
|
+
* Table of Contents to README ([#1010])
|
|
14
|
+
* TypeScript definition for `executableFile` in CommandOptions ([#1028])
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
* consistently use `const` rather than `var` in README ([#1026])
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
* help for sub commands with custom executableFile ([#1018])
|
|
23
|
+
|
|
1
24
|
3.0.0 / 2019-08-08
|
|
2
25
|
=================
|
|
3
26
|
|
|
@@ -47,20 +70,6 @@ program
|
|
|
47
70
|
.command('action2', { noHelp: true }) // Correct
|
|
48
71
|
```
|
|
49
72
|
|
|
50
|
-
[#599]: https://github.com/tj/commander.js/issues/599
|
|
51
|
-
[#611]: https://github.com/tj/commander.js/issues/611
|
|
52
|
-
[#697]: https://github.com/tj/commander.js/issues/697
|
|
53
|
-
[#795]: https://github.com/tj/commander.js/issues/795
|
|
54
|
-
[#915]: https://github.com/tj/commander.js/issues/915
|
|
55
|
-
[#938]: https://github.com/tj/commander.js/issues/938
|
|
56
|
-
[#963]: https://github.com/tj/commander.js/issues/963
|
|
57
|
-
[#974]: https://github.com/tj/commander.js/issues/974
|
|
58
|
-
[#980]: https://github.com/tj/commander.js/issues/980
|
|
59
|
-
[#987]: https://github.com/tj/commander.js/issues/987
|
|
60
|
-
[#990]: https://github.com/tj/commander.js/issues/990
|
|
61
|
-
[#991]: https://github.com/tj/commander.js/issues/991
|
|
62
|
-
[#999]: https://github.com/tj/commander.js/issues/999
|
|
63
|
-
|
|
64
73
|
3.0.0-0 Prerelease / 2019-07-28
|
|
65
74
|
==============================
|
|
66
75
|
|
|
@@ -473,3 +482,24 @@ program
|
|
|
473
482
|
==================
|
|
474
483
|
|
|
475
484
|
* Initial release
|
|
485
|
+
|
|
486
|
+
[#599]: https://github.com/tj/commander.js/issues/599
|
|
487
|
+
[#611]: https://github.com/tj/commander.js/issues/611
|
|
488
|
+
[#697]: https://github.com/tj/commander.js/issues/697
|
|
489
|
+
[#795]: https://github.com/tj/commander.js/issues/795
|
|
490
|
+
[#915]: https://github.com/tj/commander.js/issues/915
|
|
491
|
+
[#938]: https://github.com/tj/commander.js/issues/938
|
|
492
|
+
[#963]: https://github.com/tj/commander.js/issues/963
|
|
493
|
+
[#974]: https://github.com/tj/commander.js/issues/974
|
|
494
|
+
[#980]: https://github.com/tj/commander.js/issues/980
|
|
495
|
+
[#987]: https://github.com/tj/commander.js/issues/987
|
|
496
|
+
[#990]: https://github.com/tj/commander.js/issues/990
|
|
497
|
+
[#991]: https://github.com/tj/commander.js/issues/991
|
|
498
|
+
[#999]: https://github.com/tj/commander.js/issues/999
|
|
499
|
+
[#1010]: https://github.com/tj/commander.js/pull/1010
|
|
500
|
+
[#1018]: https://github.com/tj/commander.js/pull/1018
|
|
501
|
+
[#1026]: https://github.com/tj/commander.js/pull/1026
|
|
502
|
+
[#1028]: https://github.com/tj/commander.js/pull/1028
|
|
503
|
+
|
|
504
|
+
[Unreleased]: https://github.com/tj/commander.js/compare/master...develop
|
|
505
|
+
[3.0.1]: https://github.com/tj/commander.js/compare/v3.0.0...v3.0.1
|
package/Readme.md
CHANGED
|
@@ -7,9 +7,39 @@
|
|
|
7
7
|
|
|
8
8
|
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).
|
|
9
9
|
|
|
10
|
+
- [Commander.js](#commanderjs)
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Declaring _program_ variable](#declaring-program-variable)
|
|
13
|
+
- [Options](#options)
|
|
14
|
+
- [Common option types, boolean and value](#common-option-types-boolean-and-value)
|
|
15
|
+
- [Default option value](#default-option-value)
|
|
16
|
+
- [Other option types, negatable boolean and flag|value](#other-option-types-negatable-boolean-and-flagvalue)
|
|
17
|
+
- [Custom option processing](#custom-option-processing)
|
|
18
|
+
- [Version option](#version-option)
|
|
19
|
+
- [Commands](#commands)
|
|
20
|
+
- [Specify the argument syntax](#specify-the-argument-syntax)
|
|
21
|
+
- [Action handler (sub)commands](#action-handler-subcommands)
|
|
22
|
+
- [Git-style executable (sub)commands](#git-style-executable-subcommands)
|
|
23
|
+
- [Automated --help](#automated---help)
|
|
24
|
+
- [Custom help](#custom-help)
|
|
25
|
+
- [.usage and .name](#usage-and-name)
|
|
26
|
+
- [.outputHelp(cb)](#outputhelpcb)
|
|
27
|
+
- [.helpOption(flags, description)](#helpoptionflags-description)
|
|
28
|
+
- [.help(cb)](#helpcb)
|
|
29
|
+
- [Custom event listeners](#custom-event-listeners)
|
|
30
|
+
- [Bits and pieces](#bits-and-pieces)
|
|
31
|
+
- [TypeScript](#typescript)
|
|
32
|
+
- [Node options such as `--harmony`](#node-options-such-as---harmony)
|
|
33
|
+
- [Node debugging](#node-debugging)
|
|
34
|
+
- [Examples](#examples)
|
|
35
|
+
- [License](#license)
|
|
36
|
+
- [Support](#support)
|
|
37
|
+
|
|
10
38
|
## Installation
|
|
11
39
|
|
|
12
|
-
|
|
40
|
+
```bash
|
|
41
|
+
npm install commander
|
|
42
|
+
```
|
|
13
43
|
|
|
14
44
|
## Declaring _program_ variable
|
|
15
45
|
|
|
@@ -29,7 +59,6 @@ For larger programs which may use commander in multiple ways, including unit tes
|
|
|
29
59
|
program.version('0.0.1');
|
|
30
60
|
```
|
|
31
61
|
|
|
32
|
-
|
|
33
62
|
## Options
|
|
34
63
|
|
|
35
64
|
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.
|
|
@@ -119,7 +148,7 @@ console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);
|
|
|
119
148
|
```
|
|
120
149
|
|
|
121
150
|
```bash
|
|
122
|
-
$ pizza-options
|
|
151
|
+
$ pizza-options
|
|
123
152
|
You ordered a pizza with sauce and mozzarella cheese
|
|
124
153
|
$ pizza-options --sauce
|
|
125
154
|
error: unknown option '--sauce'
|
|
@@ -155,7 +184,7 @@ add cheese type mozzarella
|
|
|
155
184
|
|
|
156
185
|
### Custom option processing
|
|
157
186
|
|
|
158
|
-
You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the
|
|
187
|
+
You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the
|
|
159
188
|
previous value for the option. It returns the new value for the option.
|
|
160
189
|
|
|
161
190
|
This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.
|
|
@@ -259,9 +288,8 @@ program
|
|
|
259
288
|
|
|
260
289
|
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.
|
|
261
290
|
|
|
262
|
-
|
|
263
291
|
```js
|
|
264
|
-
|
|
292
|
+
const program = require('commander');
|
|
265
293
|
|
|
266
294
|
program
|
|
267
295
|
.version('0.1.0')
|
|
@@ -285,7 +313,7 @@ console.log('environment:', envValue || "no environment given");
|
|
|
285
313
|
append `...` to the argument name. For example:
|
|
286
314
|
|
|
287
315
|
```js
|
|
288
|
-
|
|
316
|
+
const program = require('commander');
|
|
289
317
|
|
|
290
318
|
program
|
|
291
319
|
.version('0.1.0')
|
|
@@ -311,7 +339,7 @@ The action handler gets passed a parameter for each argument you declared, and o
|
|
|
311
339
|
command object itself. This command argument has the values for the command-specific options added as properties.
|
|
312
340
|
|
|
313
341
|
```js
|
|
314
|
-
|
|
342
|
+
const program = require('commander');
|
|
315
343
|
|
|
316
344
|
program
|
|
317
345
|
.command('rm <dir>')
|
|
@@ -337,7 +365,7 @@ You handle the options for an executable (sub)command in the executable, and don
|
|
|
337
365
|
|
|
338
366
|
```js
|
|
339
367
|
// file: ./examples/pm
|
|
340
|
-
|
|
368
|
+
const program = require('commander');
|
|
341
369
|
|
|
342
370
|
program
|
|
343
371
|
.version('0.1.0')
|
|
@@ -357,7 +385,7 @@ If the program is designed to be installed globally, make sure the executables h
|
|
|
357
385
|
|
|
358
386
|
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
|
|
359
387
|
|
|
360
|
-
```
|
|
388
|
+
```bash
|
|
361
389
|
$ ./examples/pizza --help
|
|
362
390
|
Usage: pizza [options]
|
|
363
391
|
|
|
@@ -373,7 +401,7 @@ Options:
|
|
|
373
401
|
-h, --help output usage information
|
|
374
402
|
```
|
|
375
403
|
|
|
376
|
-
|
|
404
|
+
### Custom help
|
|
377
405
|
|
|
378
406
|
You can display arbitrary `-h, --help` information
|
|
379
407
|
by listening for "--help". Commander will automatically
|
|
@@ -385,11 +413,7 @@ Options:
|
|
|
385
413
|
```js
|
|
386
414
|
#!/usr/bin/env node
|
|
387
415
|
|
|
388
|
-
|
|
389
|
-
* Module dependencies.
|
|
390
|
-
*/
|
|
391
|
-
|
|
392
|
-
var program = require('commander');
|
|
416
|
+
const program = require('commander');
|
|
393
417
|
|
|
394
418
|
program
|
|
395
419
|
.version('0.1.0')
|
|
@@ -414,7 +438,7 @@ console.log('stuff');
|
|
|
414
438
|
|
|
415
439
|
Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:
|
|
416
440
|
|
|
417
|
-
```
|
|
441
|
+
```Text
|
|
418
442
|
Usage: custom-help [options]
|
|
419
443
|
|
|
420
444
|
Options:
|
|
@@ -429,7 +453,24 @@ Examples:
|
|
|
429
453
|
$ custom-help -h
|
|
430
454
|
```
|
|
431
455
|
|
|
432
|
-
|
|
456
|
+
### .usage and .name
|
|
457
|
+
|
|
458
|
+
These allow you to customise the usage description in the first line of the help. The name is otherwise
|
|
459
|
+
deduced from the (full) program arguments. Given:
|
|
460
|
+
|
|
461
|
+
```js
|
|
462
|
+
program
|
|
463
|
+
.name("my-command")
|
|
464
|
+
.usage("[global options] command")
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
The help will start with:
|
|
468
|
+
|
|
469
|
+
```Text
|
|
470
|
+
Usage: my-command [global options] command
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### .outputHelp(cb)
|
|
433
474
|
|
|
434
475
|
Output help information without exiting.
|
|
435
476
|
Optional callback cb allows post-processing of help text before it is displayed.
|
|
@@ -437,8 +478,8 @@ Optional callback cb allows post-processing of help text before it is displayed.
|
|
|
437
478
|
If you want to display help by default (e.g. if no command was provided), you can use something like:
|
|
438
479
|
|
|
439
480
|
```js
|
|
440
|
-
|
|
441
|
-
|
|
481
|
+
const program = require('commander');
|
|
482
|
+
const colors = require('colors');
|
|
442
483
|
|
|
443
484
|
program
|
|
444
485
|
.version('0.1.0')
|
|
@@ -454,7 +495,7 @@ function make_red(txt) {
|
|
|
454
495
|
}
|
|
455
496
|
```
|
|
456
497
|
|
|
457
|
-
|
|
498
|
+
### .helpOption(flags, description)
|
|
458
499
|
|
|
459
500
|
Override the default help flags and description.
|
|
460
501
|
|
|
@@ -463,13 +504,13 @@ program
|
|
|
463
504
|
.helpOption('-e, --HELP', 'read more information');
|
|
464
505
|
```
|
|
465
506
|
|
|
466
|
-
|
|
507
|
+
### .help(cb)
|
|
467
508
|
|
|
468
509
|
Output help information and exit immediately.
|
|
469
510
|
Optional callback cb allows post-processing of help text before it is displayed.
|
|
470
511
|
|
|
471
|
-
|
|
472
512
|
## Custom event listeners
|
|
513
|
+
|
|
473
514
|
You can execute custom actions by listening to command and option events.
|
|
474
515
|
|
|
475
516
|
```js
|
|
@@ -504,8 +545,9 @@ node -r ts-node/register pm.ts
|
|
|
504
545
|
### Node options such as `--harmony`
|
|
505
546
|
|
|
506
547
|
You can enable `--harmony` option in two ways:
|
|
507
|
-
|
|
508
|
-
|
|
548
|
+
|
|
549
|
+
- Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. (Note Windows does not support this pattern.)
|
|
550
|
+
- Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.
|
|
509
551
|
|
|
510
552
|
### Node debugging
|
|
511
553
|
|
|
@@ -515,7 +557,7 @@ the inspector port is incremented by 1 for the spawned subcommand.
|
|
|
515
557
|
## Examples
|
|
516
558
|
|
|
517
559
|
```js
|
|
518
|
-
|
|
560
|
+
const program = require('commander');
|
|
519
561
|
|
|
520
562
|
program
|
|
521
563
|
.version('0.1.0')
|
|
@@ -528,7 +570,7 @@ program
|
|
|
528
570
|
.description('run setup commands for all envs')
|
|
529
571
|
.option("-s, --setup_mode [mode]", "Which setup mode to use")
|
|
530
572
|
.action(function(env, options){
|
|
531
|
-
|
|
573
|
+
const mode = options.setup_mode || "normal";
|
|
532
574
|
env = env || 'all';
|
|
533
575
|
console.log('setup for %s env(s) with %s mode', env, mode);
|
|
534
576
|
});
|
package/index.js
CHANGED
|
@@ -458,6 +458,14 @@ Command.prototype.parse = function(argv) {
|
|
|
458
458
|
|
|
459
459
|
var result = this.parseArgs(this.args, parsed.unknown);
|
|
460
460
|
|
|
461
|
+
if (args[0] === 'help' && args.length === 1) this.help();
|
|
462
|
+
|
|
463
|
+
// <cmd> --help
|
|
464
|
+
if (args[0] === 'help') {
|
|
465
|
+
args[0] = args[1];
|
|
466
|
+
args[1] = this._helpLongFlag;
|
|
467
|
+
}
|
|
468
|
+
|
|
461
469
|
// executable sub-commands
|
|
462
470
|
// (Debugging note for future: args[0] is not right if an action has been called)
|
|
463
471
|
var name = result.args[0];
|
|
@@ -511,13 +519,6 @@ Command.prototype.executeSubCommand = function(argv, args, unknown, executableFi
|
|
|
511
519
|
args = args.concat(unknown);
|
|
512
520
|
|
|
513
521
|
if (!args.length) this.help();
|
|
514
|
-
if (args[0] === 'help' && args.length === 1) this.help();
|
|
515
|
-
|
|
516
|
-
// <cmd> --help
|
|
517
|
-
if (args[0] === 'help') {
|
|
518
|
-
args[0] = args[1];
|
|
519
|
-
args[1] = this._helpLongFlag;
|
|
520
|
-
}
|
|
521
522
|
|
|
522
523
|
var isExplicitJS = false; // Whether to use node to launch "executable"
|
|
523
524
|
|
|
@@ -1258,7 +1259,7 @@ function outputHelpIfNecessary(cmd, options) {
|
|
|
1258
1259
|
}
|
|
1259
1260
|
|
|
1260
1261
|
/**
|
|
1261
|
-
* Takes an argument
|
|
1262
|
+
* Takes an argument and returns its human readable equivalent for help usage.
|
|
1262
1263
|
*
|
|
1263
1264
|
* @param {Object} arg
|
|
1264
1265
|
* @return {String}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "commander",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "the complete solution for node.js command-line programs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"commander",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/node": "^12.
|
|
30
|
-
"eslint": "^6.0
|
|
29
|
+
"@types/node": "^12.7.2",
|
|
30
|
+
"eslint": "^6.1.0",
|
|
31
31
|
"should": "^13.2.3",
|
|
32
|
-
"sinon": "^7.
|
|
33
|
-
"standard": "^13.0
|
|
32
|
+
"sinon": "^7.4.1",
|
|
33
|
+
"standard": "^13.1.0",
|
|
34
34
|
"ts-node": "^8.3.0",
|
|
35
35
|
"typescript": "^3.5.3"
|
|
36
36
|
},
|