commander 2.15.1 → 2.18.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 +33 -0
- package/Readme.md +41 -28
- package/index.js +22 -16
- package/package.json +10 -9
- package/typings/index.d.ts +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
|
|
2
|
+
2.18.0 / 2018-09-07
|
|
3
|
+
==================
|
|
4
|
+
|
|
5
|
+
* Standardize help output (#853)
|
|
6
|
+
* chmod 644 travis.yml (#851)
|
|
7
|
+
* add support for execute typescript subcommand via ts-node (#849)
|
|
8
|
+
|
|
9
|
+
2.17.1 / 2018-08-07
|
|
10
|
+
==================
|
|
11
|
+
|
|
12
|
+
* Fix bug in command emit (#844)
|
|
13
|
+
|
|
14
|
+
2.17.0 / 2018-08-03
|
|
15
|
+
==================
|
|
16
|
+
|
|
17
|
+
* fixed newline output after help information (#833)
|
|
18
|
+
* Fix to emit the action even without command (#778)
|
|
19
|
+
* npm update (#823)
|
|
20
|
+
|
|
21
|
+
2.16.0 / 2018-06-29
|
|
22
|
+
==================
|
|
23
|
+
|
|
24
|
+
* Remove Makefile and `test/run` (#821)
|
|
25
|
+
* Make 'npm test' run on Windows (#820)
|
|
26
|
+
* Add badge to display install size (#807)
|
|
27
|
+
* chore: cache node_modules (#814)
|
|
28
|
+
* chore: remove Node.js 4 (EOL), add Node.js 10 (#813)
|
|
29
|
+
* fixed typo in readme (#812)
|
|
30
|
+
* Fix types (#804)
|
|
31
|
+
* Update eslint to resolve vulnerabilities in lodash (#799)
|
|
32
|
+
* updated readme with custom event listeners. (#791)
|
|
33
|
+
* fix tests (#794)
|
|
34
|
+
|
|
2
35
|
2.15.0 / 2018-03-07
|
|
3
36
|
==================
|
|
4
37
|
|
package/Readme.md
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
[](http://travis-ci.org/tj/commander.js)
|
|
5
5
|
[](https://www.npmjs.org/package/commander)
|
|
6
6
|
[](https://npmcharts.com/compare/commander?minimal=true)
|
|
7
|
+
[](https://packagephobia.now.sh/result?p=commander)
|
|
7
8
|
[](https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
8
9
|
|
|
9
10
|
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).
|
|
@@ -44,7 +45,7 @@ console.log(' - %s cheese', program.cheese);
|
|
|
44
45
|
|
|
45
46
|
Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc.
|
|
46
47
|
|
|
47
|
-
Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false.
|
|
48
|
+
Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false.
|
|
48
49
|
|
|
49
50
|
```js
|
|
50
51
|
#!/usr/bin/env node
|
|
@@ -152,7 +153,7 @@ program
|
|
|
152
153
|
.option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
|
|
153
154
|
.option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
|
|
154
155
|
.parse(process.argv);
|
|
155
|
-
|
|
156
|
+
|
|
156
157
|
console.log(' size: %j', program.size);
|
|
157
158
|
console.log(' drink: %j', program.drink);
|
|
158
159
|
```
|
|
@@ -232,7 +233,7 @@ program
|
|
|
232
233
|
When `.command()` is invoked with a description argument, no `.action(callback)` should be called to handle sub-commands, otherwise there will be an error. This tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.
|
|
233
234
|
The commander will try to search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-command`, like `pm-install`, `pm-search`.
|
|
234
235
|
|
|
235
|
-
Options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the
|
|
236
|
+
Options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the subcommand from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.
|
|
236
237
|
|
|
237
238
|
If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
|
|
238
239
|
|
|
@@ -247,22 +248,20 @@ You can enable `--harmony` option in two ways:
|
|
|
247
248
|
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
|
|
248
249
|
|
|
249
250
|
```
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
Usage: pizza [options]
|
|
251
|
+
$ ./examples/pizza --help
|
|
252
|
+
Usage: pizza [options]
|
|
253
253
|
|
|
254
|
-
|
|
254
|
+
An application for pizzas ordering
|
|
255
255
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
-h, --help output usage information
|
|
259
|
-
-V, --version output the version number
|
|
260
|
-
-p, --peppers Add peppers
|
|
261
|
-
-P, --pineapple Add pineapple
|
|
262
|
-
-b, --bbq Add bbq sauce
|
|
263
|
-
-c, --cheese <type> Add the specified type of cheese [marble]
|
|
264
|
-
-C, --no-cheese You do not want any cheese
|
|
256
|
+
Options:
|
|
265
257
|
|
|
258
|
+
-h, --help output usage information
|
|
259
|
+
-V, --version output the version number
|
|
260
|
+
-p, --peppers Add peppers
|
|
261
|
+
-P, --pineapple Add pineapple
|
|
262
|
+
-b, --bbq Add bbq sauce
|
|
263
|
+
-c, --cheese <type> Add the specified type of cheese [marble]
|
|
264
|
+
-C, --no-cheese You do not want any cheese
|
|
266
265
|
```
|
|
267
266
|
|
|
268
267
|
## Custom help
|
|
@@ -270,7 +269,7 @@ You can enable `--harmony` option in two ways:
|
|
|
270
269
|
You can display arbitrary `-h, --help` information
|
|
271
270
|
by listening for "--help". Commander will automatically
|
|
272
271
|
exit once you are done so that the remainder of your program
|
|
273
|
-
does not execute causing undesired
|
|
272
|
+
does not execute causing undesired behaviors, for example
|
|
274
273
|
in the following executable "stuff" will not output when
|
|
275
274
|
`--help` is used.
|
|
276
275
|
|
|
@@ -293,11 +292,11 @@ program
|
|
|
293
292
|
// node's emit() is immediate
|
|
294
293
|
|
|
295
294
|
program.on('--help', function(){
|
|
296
|
-
console.log('
|
|
297
|
-
console.log('');
|
|
298
|
-
console.log(' $ custom-help --help');
|
|
299
|
-
console.log(' $ custom-help -h');
|
|
295
|
+
console.log('')
|
|
296
|
+
console.log('Examples:');
|
|
300
297
|
console.log('');
|
|
298
|
+
console.log(' $ custom-help --help');
|
|
299
|
+
console.log(' $ custom-help -h');
|
|
301
300
|
});
|
|
302
301
|
|
|
303
302
|
program.parse(process.argv);
|
|
@@ -308,7 +307,6 @@ console.log('stuff');
|
|
|
308
307
|
Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:
|
|
309
308
|
|
|
310
309
|
```
|
|
311
|
-
|
|
312
310
|
Usage: custom-help [options]
|
|
313
311
|
|
|
314
312
|
Options:
|
|
@@ -323,7 +321,6 @@ Examples:
|
|
|
323
321
|
|
|
324
322
|
$ custom-help --help
|
|
325
323
|
$ custom-help -h
|
|
326
|
-
|
|
327
324
|
```
|
|
328
325
|
|
|
329
326
|
## .outputHelp(cb)
|
|
@@ -356,6 +353,22 @@ function make_red(txt) {
|
|
|
356
353
|
Output help information and exit immediately.
|
|
357
354
|
Optional callback cb allows post-processing of help text before it is displayed.
|
|
358
355
|
|
|
356
|
+
|
|
357
|
+
## Custom event listeners
|
|
358
|
+
You can execute custom actions by listening to command and option events.
|
|
359
|
+
|
|
360
|
+
```js
|
|
361
|
+
program.on('option:verbose', function () {
|
|
362
|
+
process.env.VERBOSE = this.verbose;
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
// error on unknown commands
|
|
366
|
+
program.on('command:*', function () {
|
|
367
|
+
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
|
|
368
|
+
process.exit(1);
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
359
372
|
## Examples
|
|
360
373
|
|
|
361
374
|
```js
|
|
@@ -385,11 +398,11 @@ program
|
|
|
385
398
|
.action(function(cmd, options){
|
|
386
399
|
console.log('exec "%s" using %s mode', cmd, options.exec_mode);
|
|
387
400
|
}).on('--help', function() {
|
|
388
|
-
console.log('
|
|
389
|
-
console.log();
|
|
390
|
-
console.log('
|
|
391
|
-
console.log('
|
|
392
|
-
console.log();
|
|
401
|
+
console.log('');
|
|
402
|
+
console.log('Examples:');
|
|
403
|
+
console.log('');
|
|
404
|
+
console.log(' $ deploy exec sequential');
|
|
405
|
+
console.log(' $ deploy exec async');
|
|
393
406
|
});
|
|
394
407
|
|
|
395
408
|
program
|
package/index.js
CHANGED
|
@@ -43,9 +43,9 @@ exports.Option = Option;
|
|
|
43
43
|
|
|
44
44
|
function Option(flags, description) {
|
|
45
45
|
this.flags = flags;
|
|
46
|
-
this.required =
|
|
47
|
-
this.optional =
|
|
48
|
-
this.bool =
|
|
46
|
+
this.required = flags.indexOf('<') >= 0;
|
|
47
|
+
this.optional = flags.indexOf('[') >= 0;
|
|
48
|
+
this.bool = flags.indexOf('-no-') === -1;
|
|
49
49
|
flags = flags.split(/[ ,|]+/);
|
|
50
50
|
if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
|
|
51
51
|
this.long = flags.shift();
|
|
@@ -523,7 +523,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
|
|
|
523
523
|
// executable
|
|
524
524
|
var f = argv[1];
|
|
525
525
|
// name of the subcommand, link `pm-install`
|
|
526
|
-
var bin = basename(f,
|
|
526
|
+
var bin = basename(f, path.extname(f)) + '-' + args[0];
|
|
527
527
|
|
|
528
528
|
// In case of globally installed, get the base dir where executable
|
|
529
529
|
// subcommand file should be located at
|
|
@@ -539,11 +539,14 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
|
|
|
539
539
|
// prefer local `./<bin>` to bin in the $PATH
|
|
540
540
|
var localBin = path.join(baseDir, bin);
|
|
541
541
|
|
|
542
|
-
// whether bin file is a js script with explicit `.js` extension
|
|
542
|
+
// whether bin file is a js script with explicit `.js` or `.ts` extension
|
|
543
543
|
var isExplicitJS = false;
|
|
544
544
|
if (exists(localBin + '.js')) {
|
|
545
545
|
bin = localBin + '.js';
|
|
546
546
|
isExplicitJS = true;
|
|
547
|
+
} else if (exists(localBin + '.ts')) {
|
|
548
|
+
bin = localBin + '.ts';
|
|
549
|
+
isExplicitJS = true;
|
|
547
550
|
} else if (exists(localBin)) {
|
|
548
551
|
bin = localBin;
|
|
549
552
|
}
|
|
@@ -577,9 +580,9 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
|
|
|
577
580
|
proc.on('close', process.exit.bind(process));
|
|
578
581
|
proc.on('error', function(err) {
|
|
579
582
|
if (err.code === 'ENOENT') {
|
|
580
|
-
console.error('
|
|
583
|
+
console.error('%s(1) does not exist, try --help', bin);
|
|
581
584
|
} else if (err.code === 'EACCES') {
|
|
582
|
-
console.error('
|
|
585
|
+
console.error('%s(1) not executable. try chmod or run with root', bin);
|
|
583
586
|
}
|
|
584
587
|
process.exit(1);
|
|
585
588
|
});
|
|
@@ -660,6 +663,10 @@ Command.prototype.parseArgs = function(args, unknown) {
|
|
|
660
663
|
if (unknown.length > 0) {
|
|
661
664
|
this.unknownOption(unknown[0]);
|
|
662
665
|
}
|
|
666
|
+
if (this.commands.length === 0 &&
|
|
667
|
+
this._args.filter(function(a) { return a.required }).length === 0) {
|
|
668
|
+
this.emit('command:*');
|
|
669
|
+
}
|
|
663
670
|
}
|
|
664
671
|
|
|
665
672
|
return this;
|
|
@@ -1065,12 +1072,12 @@ Command.prototype.commandHelp = function() {
|
|
|
1065
1072
|
var width = this.padWidth();
|
|
1066
1073
|
|
|
1067
1074
|
return [
|
|
1068
|
-
'
|
|
1075
|
+
'Commands:',
|
|
1069
1076
|
'',
|
|
1070
1077
|
commands.map(function(cmd) {
|
|
1071
1078
|
var desc = cmd[1] ? ' ' + cmd[1] : '';
|
|
1072
1079
|
return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
|
|
1073
|
-
}).join('\n').replace(/^/gm, '
|
|
1080
|
+
}).join('\n').replace(/^/gm, ' '),
|
|
1074
1081
|
''
|
|
1075
1082
|
].join('\n');
|
|
1076
1083
|
};
|
|
@@ -1086,17 +1093,17 @@ Command.prototype.helpInformation = function() {
|
|
|
1086
1093
|
var desc = [];
|
|
1087
1094
|
if (this._description) {
|
|
1088
1095
|
desc = [
|
|
1089
|
-
|
|
1096
|
+
this._description,
|
|
1090
1097
|
''
|
|
1091
1098
|
];
|
|
1092
1099
|
|
|
1093
1100
|
var argsDescription = this._argsDescription;
|
|
1094
1101
|
if (argsDescription && this._args.length) {
|
|
1095
1102
|
var width = this.padWidth();
|
|
1096
|
-
desc.push('
|
|
1103
|
+
desc.push('Arguments:');
|
|
1097
1104
|
desc.push('');
|
|
1098
1105
|
this._args.forEach(function(arg) {
|
|
1099
|
-
desc.push('
|
|
1106
|
+
desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
|
|
1100
1107
|
});
|
|
1101
1108
|
desc.push('');
|
|
1102
1109
|
}
|
|
@@ -1107,8 +1114,7 @@ Command.prototype.helpInformation = function() {
|
|
|
1107
1114
|
cmdName = cmdName + '|' + this._alias;
|
|
1108
1115
|
}
|
|
1109
1116
|
var usage = [
|
|
1110
|
-
'',
|
|
1111
|
-
' Usage: ' + cmdName + ' ' + this.usage(),
|
|
1117
|
+
'Usage: ' + cmdName + ' ' + this.usage(),
|
|
1112
1118
|
''
|
|
1113
1119
|
];
|
|
1114
1120
|
|
|
@@ -1117,9 +1123,9 @@ Command.prototype.helpInformation = function() {
|
|
|
1117
1123
|
if (commandHelp) cmds = [commandHelp];
|
|
1118
1124
|
|
|
1119
1125
|
var options = [
|
|
1120
|
-
'
|
|
1126
|
+
'Options:',
|
|
1121
1127
|
'',
|
|
1122
|
-
'' + this.optionHelp().replace(/^/gm, '
|
|
1128
|
+
'' + this.optionHelp().replace(/^/gm, ' '),
|
|
1123
1129
|
''
|
|
1124
1130
|
];
|
|
1125
1131
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "commander",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.18.0",
|
|
4
4
|
"description": "the complete solution for node.js command-line programs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"commander",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
18
|
"lint": "eslint index.js",
|
|
19
|
-
"test": "
|
|
20
|
-
"test-typings": "
|
|
19
|
+
"test": "node test/run.js && npm run test-typings",
|
|
20
|
+
"test-typings": "tsc -p tsconfig.json"
|
|
21
21
|
},
|
|
22
22
|
"main": "index",
|
|
23
23
|
"files": [
|
|
@@ -26,12 +26,13 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/node": "^
|
|
30
|
-
"eslint": "^
|
|
31
|
-
"should": "^
|
|
32
|
-
"sinon": "^2.
|
|
33
|
-
"standard": "^
|
|
34
|
-
"
|
|
29
|
+
"@types/node": "^10.9.4",
|
|
30
|
+
"eslint": "^5.5.0",
|
|
31
|
+
"should": "^13.2.3",
|
|
32
|
+
"sinon": "^6.2.0",
|
|
33
|
+
"standard": "^12.0.1",
|
|
34
|
+
"ts-node": "^7.0.1",
|
|
35
|
+
"typescript": "^2.9.2"
|
|
35
36
|
},
|
|
36
37
|
"typings": "typings/index.d.ts"
|
|
37
38
|
}
|
package/typings/index.d.ts
CHANGED
|
@@ -218,9 +218,9 @@ declare namespace local {
|
|
|
218
218
|
/**
|
|
219
219
|
* Return an object containing options as key-value pairs
|
|
220
220
|
*
|
|
221
|
-
* @returns {{[key: string]:
|
|
221
|
+
* @returns {{[key: string]: any}}
|
|
222
222
|
*/
|
|
223
|
-
opts(): { [key: string]:
|
|
223
|
+
opts(): { [key: string]: any };
|
|
224
224
|
|
|
225
225
|
/**
|
|
226
226
|
* Set the description to `str`.
|
|
@@ -275,7 +275,7 @@ declare namespace local {
|
|
|
275
275
|
*
|
|
276
276
|
* @param {(str: string) => string} [cb]
|
|
277
277
|
*/
|
|
278
|
-
help(cb?: (str: string) => string):
|
|
278
|
+
help(cb?: (str: string) => string): never;
|
|
279
279
|
}
|
|
280
280
|
|
|
281
281
|
}
|