commander 5.0.0 → 5.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 -0
- package/Readme.md +5 -5
- package/index.js +94 -50
- package/package.json +7 -7
- package/typings/index.d.ts +77 -59
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,27 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
|
7
7
|
|
|
8
8
|
<!-- markdownlint-disable MD024 -->
|
|
9
9
|
|
|
10
|
+
## [5.1.0] (2020-04-25)
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- support for multiple command aliases, the first of which is shown in the auto-generated help ([#531], [#1236])
|
|
15
|
+
- configuration support in `addCommand()` for `hidden` and `isDefault` ([#1232])
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- omit masked help flags from the displayed help ([#645], [#1247])
|
|
20
|
+
- remove old short help flag when change help flags using `helpOption` ([#1248])
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
|
|
24
|
+
- remove use of `arguments` to improve auto-generated help in editors ([#1235])
|
|
25
|
+
- rename `.command()` configuration `noHelp` to `hidden` (but not remove old support) ([#1232])
|
|
26
|
+
- improvements to documentation
|
|
27
|
+
- update dependencies
|
|
28
|
+
- update tested versions of node
|
|
29
|
+
- eliminate lint errors in TypeScript ([#1208])
|
|
30
|
+
|
|
10
31
|
## [5.0.0] (2020-03-14)
|
|
11
32
|
|
|
12
33
|
### Added
|
|
@@ -274,8 +295,10 @@ program
|
|
|
274
295
|
[#432]: https://github.com/tj/commander.js/issues/432
|
|
275
296
|
[#508]: https://github.com/tj/commander.js/issues/508
|
|
276
297
|
[#512]: https://github.com/tj/commander.js/issues/512
|
|
298
|
+
[#531]: https://github.com/tj/commander.js/issues/531
|
|
277
299
|
[#599]: https://github.com/tj/commander.js/issues/599
|
|
278
300
|
[#611]: https://github.com/tj/commander.js/issues/611
|
|
301
|
+
[#645]: https://github.com/tj/commander.js/issues/645
|
|
279
302
|
[#697]: https://github.com/tj/commander.js/issues/697
|
|
280
303
|
[#742]: https://github.com/tj/commander.js/issues/742
|
|
281
304
|
[#764]: https://github.com/tj/commander.js/issues/764
|
|
@@ -335,8 +358,15 @@ program
|
|
|
335
358
|
[#1184]: https://github.com/tj/commander.js/pull/1184
|
|
336
359
|
[#1191]: https://github.com/tj/commander.js/pull/1191
|
|
337
360
|
[#1195]: https://github.com/tj/commander.js/pull/1195
|
|
361
|
+
[#1208]: https://github.com/tj/commander.js/pull/1208
|
|
362
|
+
[#1232]: https://github.com/tj/commander.js/pull/1232
|
|
363
|
+
[#1235]: https://github.com/tj/commander.js/pull/1235
|
|
364
|
+
[#1236]: https://github.com/tj/commander.js/pull/1236
|
|
365
|
+
[#1247]: https://github.com/tj/commander.js/pull/1247
|
|
366
|
+
[#1248]: https://github.com/tj/commander.js/pull/1248
|
|
338
367
|
|
|
339
368
|
[Unreleased]: https://github.com/tj/commander.js/compare/master...develop
|
|
369
|
+
[5.1.0]: https://github.com/tj/commander.js/compare/v5.0.0..v5.1.0
|
|
340
370
|
[5.0.0]: https://github.com/tj/commander.js/compare/v4.1.1..v5.0.0
|
|
341
371
|
[5.0.0-4]: https://github.com/tj/commander.js/compare/v5.0.0-3..v5.0.0-4
|
|
342
372
|
[5.0.0-3]: https://github.com/tj/commander.js/compare/v5.0.0-2..v5.0.0-3
|
package/Readme.md
CHANGED
|
@@ -300,7 +300,7 @@ program.version('0.0.1', '-v, --vers', 'output the current version');
|
|
|
300
300
|
|
|
301
301
|
## Commands
|
|
302
302
|
|
|
303
|
-
You can specify (sub)commands
|
|
303
|
+
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
304
|
|
|
305
305
|
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
306
|
|
|
@@ -319,22 +319,22 @@ program
|
|
|
319
319
|
});
|
|
320
320
|
|
|
321
321
|
// Command implemented using stand-alone executable file (description is second parameter to `.command`)
|
|
322
|
-
// Returns
|
|
322
|
+
// Returns `this` for adding more commands.
|
|
323
323
|
program
|
|
324
324
|
.command('start <service>', 'start named service')
|
|
325
325
|
.command('stop [service]', 'stop named service, or all if no name supplied');
|
|
326
326
|
|
|
327
327
|
// Command prepared separately.
|
|
328
|
-
// Returns
|
|
328
|
+
// Returns `this` for adding more commands.
|
|
329
329
|
program
|
|
330
330
|
.addCommand(build.makeBuildCommand());
|
|
331
331
|
```
|
|
332
332
|
|
|
333
|
-
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.
|
|
333
|
+
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
334
|
|
|
335
335
|
### Specify the argument syntax
|
|
336
336
|
|
|
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.
|
|
337
|
+
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
338
|
|
|
339
339
|
```js
|
|
340
340
|
const { program } = require('commander');
|
package/index.js
CHANGED
|
@@ -117,9 +117,9 @@ class Command extends EventEmitter {
|
|
|
117
117
|
this._executableFile = null; // custom name for executable
|
|
118
118
|
this._defaultCommandName = null;
|
|
119
119
|
this._exitCallback = null;
|
|
120
|
-
this.
|
|
120
|
+
this._aliases = [];
|
|
121
121
|
|
|
122
|
-
this.
|
|
122
|
+
this._hidden = false;
|
|
123
123
|
this._helpFlags = '-h, --help';
|
|
124
124
|
this._helpDescription = 'display help for command';
|
|
125
125
|
this._helpShortFlag = '-h';
|
|
@@ -153,7 +153,7 @@ class Command extends EventEmitter {
|
|
|
153
153
|
* @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|
154
154
|
* @param {Object|string} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
|
|
155
155
|
* @param {Object} [execOpts] - configuration options (for executable)
|
|
156
|
-
* @return {Command} returns new command for action handler, or
|
|
156
|
+
* @return {Command} returns new command for action handler, or `this` for executable command
|
|
157
157
|
* @api public
|
|
158
158
|
*/
|
|
159
159
|
|
|
@@ -174,7 +174,7 @@ class Command extends EventEmitter {
|
|
|
174
174
|
}
|
|
175
175
|
if (opts.isDefault) this._defaultCommandName = cmd._name;
|
|
176
176
|
|
|
177
|
-
cmd.
|
|
177
|
+
cmd._hidden = !!(opts.noHelp || opts.hidden);
|
|
178
178
|
cmd._helpFlags = this._helpFlags;
|
|
179
179
|
cmd._helpDescription = this._helpDescription;
|
|
180
180
|
cmd._helpShortFlag = this._helpShortFlag;
|
|
@@ -216,11 +216,12 @@ class Command extends EventEmitter {
|
|
|
216
216
|
* See .command() for creating an attached subcommand which inherits settings from its parent.
|
|
217
217
|
*
|
|
218
218
|
* @param {Command} cmd - new subcommand
|
|
219
|
-
* @
|
|
219
|
+
* @param {Object} [opts] - configuration options
|
|
220
|
+
* @return {Command} `this` command for chaining
|
|
220
221
|
* @api public
|
|
221
222
|
*/
|
|
222
223
|
|
|
223
|
-
addCommand(cmd) {
|
|
224
|
+
addCommand(cmd, opts) {
|
|
224
225
|
if (!cmd._name) throw new Error('Command passed to .addCommand() must have a name');
|
|
225
226
|
|
|
226
227
|
// To keep things simple, block automatic name generation for deeply nested executables.
|
|
@@ -235,13 +236,17 @@ class Command extends EventEmitter {
|
|
|
235
236
|
}
|
|
236
237
|
checkExplicitNames(cmd.commands);
|
|
237
238
|
|
|
239
|
+
opts = opts || {};
|
|
240
|
+
if (opts.isDefault) this._defaultCommandName = cmd._name;
|
|
241
|
+
if (opts.noHelp || opts.hidden) cmd._hidden = true; // modifying passed command due to existing implementation
|
|
242
|
+
|
|
238
243
|
this.commands.push(cmd);
|
|
239
244
|
cmd.parent = this;
|
|
240
245
|
return this;
|
|
241
246
|
};
|
|
242
247
|
|
|
243
248
|
/**
|
|
244
|
-
* Define argument syntax for the
|
|
249
|
+
* Define argument syntax for the command.
|
|
245
250
|
*
|
|
246
251
|
* @api public
|
|
247
252
|
*/
|
|
@@ -257,7 +262,7 @@ class Command extends EventEmitter {
|
|
|
257
262
|
* addHelpCommand(false); // force off
|
|
258
263
|
* addHelpCommand('help [cmd]', 'display help for [cmd]'); // force on with custom detais
|
|
259
264
|
*
|
|
260
|
-
* @return {Command} for chaining
|
|
265
|
+
* @return {Command} `this` command for chaining
|
|
261
266
|
* @api public
|
|
262
267
|
*/
|
|
263
268
|
|
|
@@ -293,7 +298,7 @@ class Command extends EventEmitter {
|
|
|
293
298
|
* For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
|
|
294
299
|
*
|
|
295
300
|
* @param {Array} args
|
|
296
|
-
* @return {Command} for chaining
|
|
301
|
+
* @return {Command} `this` command for chaining
|
|
297
302
|
* @api private
|
|
298
303
|
*/
|
|
299
304
|
|
|
@@ -336,7 +341,7 @@ class Command extends EventEmitter {
|
|
|
336
341
|
* Register callback to use as replacement for calling process.exit.
|
|
337
342
|
*
|
|
338
343
|
* @param {Function} [fn] optional callback which will be passed a CommanderError, defaults to throwing
|
|
339
|
-
* @return {Command} for chaining
|
|
344
|
+
* @return {Command} `this` command for chaining
|
|
340
345
|
* @api public
|
|
341
346
|
*/
|
|
342
347
|
|
|
@@ -386,7 +391,7 @@ class Command extends EventEmitter {
|
|
|
386
391
|
* });
|
|
387
392
|
*
|
|
388
393
|
* @param {Function} fn
|
|
389
|
-
* @return {Command} for chaining
|
|
394
|
+
* @return {Command} `this` command for chaining
|
|
390
395
|
* @api public
|
|
391
396
|
*/
|
|
392
397
|
|
|
@@ -425,7 +430,7 @@ class Command extends EventEmitter {
|
|
|
425
430
|
* @param {string} description
|
|
426
431
|
* @param {Function|*} [fn] - custom option processing function or default vaue
|
|
427
432
|
* @param {*} [defaultValue]
|
|
428
|
-
* @return {Command} for chaining
|
|
433
|
+
* @return {Command} `this` command for chaining
|
|
429
434
|
* @api private
|
|
430
435
|
*/
|
|
431
436
|
|
|
@@ -543,7 +548,7 @@ class Command extends EventEmitter {
|
|
|
543
548
|
* @param {string} description
|
|
544
549
|
* @param {Function|*} [fn] - custom option processing function or default vaue
|
|
545
550
|
* @param {*} [defaultValue]
|
|
546
|
-
* @return {Command} for chaining
|
|
551
|
+
* @return {Command} `this` command for chaining
|
|
547
552
|
* @api public
|
|
548
553
|
*/
|
|
549
554
|
|
|
@@ -561,7 +566,7 @@ class Command extends EventEmitter {
|
|
|
561
566
|
* @param {string} description
|
|
562
567
|
* @param {Function|*} [fn] - custom option processing function or default vaue
|
|
563
568
|
* @param {*} [defaultValue]
|
|
564
|
-
* @return {Command} for chaining
|
|
569
|
+
* @return {Command} `this` command for chaining
|
|
565
570
|
* @api public
|
|
566
571
|
*/
|
|
567
572
|
|
|
@@ -577,7 +582,7 @@ class Command extends EventEmitter {
|
|
|
577
582
|
* @api public
|
|
578
583
|
*/
|
|
579
584
|
allowUnknownOption(arg) {
|
|
580
|
-
this._allowUnknownOption =
|
|
585
|
+
this._allowUnknownOption = (arg === undefined) || arg;
|
|
581
586
|
return this;
|
|
582
587
|
};
|
|
583
588
|
|
|
@@ -586,7 +591,7 @@ class Command extends EventEmitter {
|
|
|
586
591
|
* or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|
587
592
|
*
|
|
588
593
|
* @param {boolean} value
|
|
589
|
-
* @return {Command}
|
|
594
|
+
* @return {Command} `this` command for chaining
|
|
590
595
|
* @api public
|
|
591
596
|
*/
|
|
592
597
|
|
|
@@ -603,7 +608,7 @@ class Command extends EventEmitter {
|
|
|
603
608
|
* or just the options (specify false).
|
|
604
609
|
*
|
|
605
610
|
* @param {boolean} value
|
|
606
|
-
* @return {Command}
|
|
611
|
+
* @return {Command} `this` command for chaining
|
|
607
612
|
* @api public
|
|
608
613
|
*/
|
|
609
614
|
|
|
@@ -658,7 +663,7 @@ class Command extends EventEmitter {
|
|
|
658
663
|
* @param {string[]} [argv] - optional, defaults to process.argv
|
|
659
664
|
* @param {Object} [parseOptions] - optionally specify style of options with from: node/user/electron
|
|
660
665
|
* @param {string} [parseOptions.from] - where the args are from: 'node', 'user', 'electron'
|
|
661
|
-
* @return {Command} for chaining
|
|
666
|
+
* @return {Command} `this` command for chaining
|
|
662
667
|
* @api public
|
|
663
668
|
*/
|
|
664
669
|
|
|
@@ -932,7 +937,7 @@ class Command extends EventEmitter {
|
|
|
932
937
|
*/
|
|
933
938
|
_findCommand(name) {
|
|
934
939
|
if (!name) return undefined;
|
|
935
|
-
return this.commands.find(cmd => cmd._name === name || cmd.
|
|
940
|
+
return this.commands.find(cmd => cmd._name === name || cmd._aliases.includes(name));
|
|
936
941
|
};
|
|
937
942
|
|
|
938
943
|
/**
|
|
@@ -1172,12 +1177,12 @@ class Command extends EventEmitter {
|
|
|
1172
1177
|
* @param {string} str
|
|
1173
1178
|
* @param {string} [flags]
|
|
1174
1179
|
* @param {string} [description]
|
|
1175
|
-
* @return {
|
|
1180
|
+
* @return {this | string} `this` command for chaining, or version string if no arguments
|
|
1176
1181
|
* @api public
|
|
1177
1182
|
*/
|
|
1178
1183
|
|
|
1179
1184
|
version(str, flags, description) {
|
|
1180
|
-
if (
|
|
1185
|
+
if (str === undefined) return this._version;
|
|
1181
1186
|
this._version = str;
|
|
1182
1187
|
flags = flags || '-V, --version';
|
|
1183
1188
|
description = description || 'output the version number';
|
|
@@ -1196,36 +1201,57 @@ class Command extends EventEmitter {
|
|
|
1196
1201
|
*
|
|
1197
1202
|
* @param {string} str
|
|
1198
1203
|
* @param {Object} [argsDescription]
|
|
1199
|
-
* @return {
|
|
1204
|
+
* @return {string|Command}
|
|
1200
1205
|
* @api public
|
|
1201
1206
|
*/
|
|
1202
1207
|
|
|
1203
1208
|
description(str, argsDescription) {
|
|
1204
|
-
if (
|
|
1209
|
+
if (str === undefined && argsDescription === undefined) return this._description;
|
|
1205
1210
|
this._description = str;
|
|
1206
1211
|
this._argsDescription = argsDescription;
|
|
1207
1212
|
return this;
|
|
1208
1213
|
};
|
|
1209
1214
|
|
|
1210
1215
|
/**
|
|
1211
|
-
* Set an alias for the command
|
|
1216
|
+
* Set an alias for the command.
|
|
1212
1217
|
*
|
|
1213
|
-
*
|
|
1214
|
-
*
|
|
1218
|
+
* You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
|
|
1219
|
+
*
|
|
1220
|
+
* @param {string} [alias]
|
|
1221
|
+
* @return {string|Command}
|
|
1215
1222
|
* @api public
|
|
1216
1223
|
*/
|
|
1217
1224
|
|
|
1218
1225
|
alias(alias) {
|
|
1226
|
+
if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility
|
|
1227
|
+
|
|
1219
1228
|
let command = this;
|
|
1220
|
-
if (this.commands.length !== 0) {
|
|
1229
|
+
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
|
|
1230
|
+
// assume adding alias for last added executable subcommand, rather than this
|
|
1221
1231
|
command = this.commands[this.commands.length - 1];
|
|
1222
1232
|
}
|
|
1223
1233
|
|
|
1224
|
-
if (arguments.length === 0) return command._alias;
|
|
1225
|
-
|
|
1226
1234
|
if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
|
|
1227
1235
|
|
|
1228
|
-
command.
|
|
1236
|
+
command._aliases.push(alias);
|
|
1237
|
+
return this;
|
|
1238
|
+
};
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* Set aliases for the command.
|
|
1242
|
+
*
|
|
1243
|
+
* Only the first alias is shown in the auto-generated help.
|
|
1244
|
+
*
|
|
1245
|
+
* @param {string[]} [aliases]
|
|
1246
|
+
* @return {string[]|Command}
|
|
1247
|
+
* @api public
|
|
1248
|
+
*/
|
|
1249
|
+
|
|
1250
|
+
aliases(aliases) {
|
|
1251
|
+
// Getter for the array of aliases is the main reason for having aliases() in addition to alias().
|
|
1252
|
+
if (aliases === undefined) return this._aliases;
|
|
1253
|
+
|
|
1254
|
+
aliases.forEach((alias) => this.alias(alias));
|
|
1229
1255
|
return this;
|
|
1230
1256
|
};
|
|
1231
1257
|
|
|
@@ -1238,17 +1264,18 @@ class Command extends EventEmitter {
|
|
|
1238
1264
|
*/
|
|
1239
1265
|
|
|
1240
1266
|
usage(str) {
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
});
|
|
1267
|
+
if (str === undefined) {
|
|
1268
|
+
if (this._usage) return this._usage;
|
|
1244
1269
|
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1270
|
+
const args = this._args.map((arg) => {
|
|
1271
|
+
return humanReadableArgName(arg);
|
|
1272
|
+
});
|
|
1273
|
+
return '[options]' +
|
|
1274
|
+
(this.commands.length ? ' [command]' : '') +
|
|
1275
|
+
(this._args.length ? ' ' + args.join(' ') : '');
|
|
1276
|
+
}
|
|
1248
1277
|
|
|
1249
|
-
if (arguments.length === 0) return this._usage || usage;
|
|
1250
1278
|
this._usage = str;
|
|
1251
|
-
|
|
1252
1279
|
return this;
|
|
1253
1280
|
};
|
|
1254
1281
|
|
|
@@ -1261,7 +1288,7 @@ class Command extends EventEmitter {
|
|
|
1261
1288
|
*/
|
|
1262
1289
|
|
|
1263
1290
|
name(str) {
|
|
1264
|
-
if (
|
|
1291
|
+
if (str === undefined) return this._name;
|
|
1265
1292
|
this._name = str;
|
|
1266
1293
|
return this;
|
|
1267
1294
|
};
|
|
@@ -1275,7 +1302,7 @@ class Command extends EventEmitter {
|
|
|
1275
1302
|
|
|
1276
1303
|
prepareCommands() {
|
|
1277
1304
|
const commandDetails = this.commands.filter((cmd) => {
|
|
1278
|
-
return !cmd.
|
|
1305
|
+
return !cmd._hidden;
|
|
1279
1306
|
}).map((cmd) => {
|
|
1280
1307
|
const args = cmd._args.map((arg) => {
|
|
1281
1308
|
return humanReadableArgName(arg);
|
|
@@ -1283,7 +1310,7 @@ class Command extends EventEmitter {
|
|
|
1283
1310
|
|
|
1284
1311
|
return [
|
|
1285
1312
|
cmd._name +
|
|
1286
|
-
(cmd.
|
|
1313
|
+
(cmd._aliases[0] ? '|' + cmd._aliases[0] : '') +
|
|
1287
1314
|
(cmd.options.length ? ' [options]' : '') +
|
|
1288
1315
|
(args ? ' ' + args : ''),
|
|
1289
1316
|
cmd._description
|
|
@@ -1374,17 +1401,33 @@ class Command extends EventEmitter {
|
|
|
1374
1401
|
|
|
1375
1402
|
optionHelp() {
|
|
1376
1403
|
const width = this.padWidth();
|
|
1377
|
-
|
|
1378
1404
|
const columns = process.stdout.columns || 80;
|
|
1379
1405
|
const descriptionWidth = columns - width - 4;
|
|
1406
|
+
function padOptionDetails(flags, description) {
|
|
1407
|
+
return pad(flags, width) + ' ' + optionalWrap(description, descriptionWidth, width + 2);
|
|
1408
|
+
};
|
|
1380
1409
|
|
|
1381
|
-
//
|
|
1382
|
-
|
|
1410
|
+
// Explicit options (including version)
|
|
1411
|
+
const help = this.options.map((option) => {
|
|
1383
1412
|
const fullDesc = option.description +
|
|
1384
1413
|
((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
|
|
1385
|
-
return
|
|
1386
|
-
})
|
|
1387
|
-
|
|
1414
|
+
return padOptionDetails(option.flags, fullDesc);
|
|
1415
|
+
});
|
|
1416
|
+
|
|
1417
|
+
// Implicit help
|
|
1418
|
+
const showShortHelpFlag = this._helpShortFlag && !this._findOption(this._helpShortFlag);
|
|
1419
|
+
const showLongHelpFlag = !this._findOption(this._helpLongFlag);
|
|
1420
|
+
if (showShortHelpFlag || showLongHelpFlag) {
|
|
1421
|
+
let helpFlags = this._helpFlags;
|
|
1422
|
+
if (!showShortHelpFlag) {
|
|
1423
|
+
helpFlags = this._helpLongFlag;
|
|
1424
|
+
} else if (!showLongHelpFlag) {
|
|
1425
|
+
helpFlags = this._helpShortFlag;
|
|
1426
|
+
}
|
|
1427
|
+
help.push(padOptionDetails(helpFlags, this._helpDescription));
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
return help.join('\n');
|
|
1388
1431
|
};
|
|
1389
1432
|
|
|
1390
1433
|
/**
|
|
@@ -1443,8 +1486,8 @@ class Command extends EventEmitter {
|
|
|
1443
1486
|
}
|
|
1444
1487
|
|
|
1445
1488
|
let cmdName = this._name;
|
|
1446
|
-
if (this.
|
|
1447
|
-
cmdName = cmdName + '|' + this.
|
|
1489
|
+
if (this._aliases[0]) {
|
|
1490
|
+
cmdName = cmdName + '|' + this._aliases[0];
|
|
1448
1491
|
}
|
|
1449
1492
|
let parentCmdNames = '';
|
|
1450
1493
|
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
|
|
@@ -1501,7 +1544,7 @@ class Command extends EventEmitter {
|
|
|
1501
1544
|
*
|
|
1502
1545
|
* @param {string} [flags]
|
|
1503
1546
|
* @param {string} [description]
|
|
1504
|
-
* @return {Command}
|
|
1547
|
+
* @return {Command} `this` command for chaining
|
|
1505
1548
|
* @api public
|
|
1506
1549
|
*/
|
|
1507
1550
|
|
|
@@ -1511,6 +1554,7 @@ class Command extends EventEmitter {
|
|
|
1511
1554
|
|
|
1512
1555
|
const splitFlags = this._helpFlags.split(/[ ,|]+/);
|
|
1513
1556
|
|
|
1557
|
+
this._helpShortFlag = undefined;
|
|
1514
1558
|
if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();
|
|
1515
1559
|
|
|
1516
1560
|
this._helpLongFlag = splitFlags.shift();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "commander",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "the complete solution for node.js command-line programs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"commander",
|
|
@@ -31,14 +31,14 @@
|
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@types/jest": "^25.1
|
|
35
|
-
"@types/node": "^12.12.
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^2.
|
|
34
|
+
"@types/jest": "^25.2.1",
|
|
35
|
+
"@types/node": "^12.12.36",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^2.29.0",
|
|
37
37
|
"eslint": "^6.8.0",
|
|
38
|
-
"eslint-config-standard-with-typescript": "^
|
|
38
|
+
"eslint-config-standard-with-typescript": "^15.0.1",
|
|
39
39
|
"eslint-plugin-jest": "^23.8.2",
|
|
40
|
-
"jest": "^25.
|
|
41
|
-
"standard": "^14.3.
|
|
40
|
+
"jest": "^25.4.0",
|
|
41
|
+
"standard": "^14.3.3",
|
|
42
42
|
"typescript": "^3.7.5"
|
|
43
43
|
},
|
|
44
44
|
"typings": "typings/index.d.ts",
|
package/typings/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ declare namespace commander {
|
|
|
9
9
|
message: string;
|
|
10
10
|
nestedError?: string;
|
|
11
11
|
}
|
|
12
|
-
type CommanderErrorConstructor =
|
|
12
|
+
type CommanderErrorConstructor = new (exitCode: number, code: string, message: string) => CommanderError;
|
|
13
13
|
|
|
14
14
|
interface Option {
|
|
15
15
|
flags: string;
|
|
@@ -21,7 +21,7 @@ declare namespace commander {
|
|
|
21
21
|
long: string;
|
|
22
22
|
description: string;
|
|
23
23
|
}
|
|
24
|
-
type OptionConstructor =
|
|
24
|
+
type OptionConstructor = new (flags: string, description?: string) => Option;
|
|
25
25
|
|
|
26
26
|
interface ParseOptions {
|
|
27
27
|
from: 'node' | 'electron' | 'user';
|
|
@@ -35,21 +35,21 @@ declare namespace commander {
|
|
|
35
35
|
commands: Command[];
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Set the program version to `str`.
|
|
38
|
+
* Set the program version to `str`.
|
|
39
39
|
*
|
|
40
40
|
* This method auto-registers the "-V, --version" flag
|
|
41
41
|
* which will print the version number when passed.
|
|
42
|
-
*
|
|
42
|
+
*
|
|
43
43
|
* You can optionally supply the flags and description to override the defaults.
|
|
44
44
|
*/
|
|
45
45
|
version(str: string, flags?: string, description?: string): this;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Define a command, implemented using an action handler.
|
|
49
|
-
*
|
|
49
|
+
*
|
|
50
50
|
* @remarks
|
|
51
51
|
* The command description is supplied using `.description`, not as a parameter to `.command`.
|
|
52
|
-
*
|
|
52
|
+
*
|
|
53
53
|
* @example
|
|
54
54
|
* ```ts
|
|
55
55
|
* program
|
|
@@ -59,7 +59,7 @@ declare namespace commander {
|
|
|
59
59
|
* console.log('clone command called');
|
|
60
60
|
* });
|
|
61
61
|
* ```
|
|
62
|
-
*
|
|
62
|
+
*
|
|
63
63
|
* @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|
64
64
|
* @param opts - configuration options
|
|
65
65
|
* @returns new command
|
|
@@ -67,23 +67,23 @@ declare namespace commander {
|
|
|
67
67
|
command(nameAndArgs: string, opts?: CommandOptions): ReturnType<this['createCommand']>;
|
|
68
68
|
/**
|
|
69
69
|
* Define a command, implemented in a separate executable file.
|
|
70
|
-
*
|
|
70
|
+
*
|
|
71
71
|
* @remarks
|
|
72
72
|
* The command description is supplied as the second parameter to `.command`.
|
|
73
|
-
*
|
|
73
|
+
*
|
|
74
74
|
* @example
|
|
75
75
|
* ```ts
|
|
76
76
|
* program
|
|
77
77
|
* .command('start <service>', 'start named service')
|
|
78
78
|
* .command('stop [service]', 'stop named serice, or all if no name supplied');
|
|
79
79
|
* ```
|
|
80
|
-
*
|
|
80
|
+
*
|
|
81
81
|
* @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
|
|
82
82
|
* @param description - description of executable command
|
|
83
83
|
* @param opts - configuration options
|
|
84
|
-
* @returns
|
|
84
|
+
* @returns `this` command for chaining
|
|
85
85
|
*/
|
|
86
|
-
command(nameAndArgs: string, description: string, opts?: commander.
|
|
86
|
+
command(nameAndArgs: string, description: string, opts?: commander.ExecutableCommandOptions): this;
|
|
87
87
|
|
|
88
88
|
/**
|
|
89
89
|
* Factory routine to create a new unattached command.
|
|
@@ -92,20 +92,20 @@ declare namespace commander {
|
|
|
92
92
|
* create the command. You can override createCommand to customise subcommands.
|
|
93
93
|
*/
|
|
94
94
|
createCommand(name?: string): Command;
|
|
95
|
-
|
|
95
|
+
|
|
96
96
|
/**
|
|
97
97
|
* Add a prepared subcommand.
|
|
98
|
-
*
|
|
98
|
+
*
|
|
99
99
|
* See .command() for creating an attached subcommand which inherits settings from its parent.
|
|
100
|
-
*
|
|
101
|
-
* @returns
|
|
100
|
+
*
|
|
101
|
+
* @returns `this` command for chaining
|
|
102
102
|
*/
|
|
103
|
-
addCommand(cmd: Command): this;
|
|
103
|
+
addCommand(cmd: Command, opts?: CommandOptions): this;
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
-
* Define argument syntax for
|
|
106
|
+
* Define argument syntax for command.
|
|
107
107
|
*
|
|
108
|
-
* @returns
|
|
108
|
+
* @returns `this` command for chaining
|
|
109
109
|
*/
|
|
110
110
|
arguments(desc: string): this;
|
|
111
111
|
|
|
@@ -125,7 +125,7 @@ declare namespace commander {
|
|
|
125
125
|
* // output help here
|
|
126
126
|
* });
|
|
127
127
|
*
|
|
128
|
-
* @returns
|
|
128
|
+
* @returns `this` command for chaining
|
|
129
129
|
*/
|
|
130
130
|
action(fn: (...args: any[]) => void | Promise<void>): this;
|
|
131
131
|
|
|
@@ -169,7 +169,7 @@ declare namespace commander {
|
|
|
169
169
|
* // optional argument
|
|
170
170
|
* program.option('-c, --cheese [type]', 'add cheese [marble]');
|
|
171
171
|
*
|
|
172
|
-
* @returns
|
|
172
|
+
* @returns `this` command for chaining
|
|
173
173
|
*/
|
|
174
174
|
option(flags: string, description?: string, defaultValue?: string | boolean): this;
|
|
175
175
|
option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
|
|
@@ -185,20 +185,19 @@ declare namespace commander {
|
|
|
185
185
|
requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
|
|
186
186
|
requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
|
|
187
187
|
|
|
188
|
-
|
|
189
188
|
/**
|
|
190
189
|
* Whether to store option values as properties on command object,
|
|
191
190
|
* or store separately (specify false). In both cases the option values can be accessed using .opts().
|
|
192
191
|
*
|
|
193
|
-
* @
|
|
192
|
+
* @returns `this` command for chaining
|
|
194
193
|
*/
|
|
195
194
|
storeOptionsAsProperties(value?: boolean): this;
|
|
196
195
|
|
|
197
196
|
/**
|
|
198
197
|
* Whether to pass command to action handler,
|
|
199
198
|
* or just the options (specify false).
|
|
200
|
-
*
|
|
201
|
-
* @
|
|
199
|
+
*
|
|
200
|
+
* @returns `this` command for chaining
|
|
202
201
|
*/
|
|
203
202
|
passCommandToAction(value?: boolean): this;
|
|
204
203
|
|
|
@@ -206,13 +205,13 @@ declare namespace commander {
|
|
|
206
205
|
* Allow unknown options on the command line.
|
|
207
206
|
*
|
|
208
207
|
* @param [arg] if `true` or omitted, no error will be thrown for unknown options.
|
|
209
|
-
* @returns
|
|
208
|
+
* @returns `this` command for chaining
|
|
210
209
|
*/
|
|
211
210
|
allowUnknownOption(arg?: boolean): this;
|
|
212
211
|
|
|
213
212
|
/**
|
|
214
213
|
* Parse `argv`, setting options and invoking commands when defined.
|
|
215
|
-
*
|
|
214
|
+
*
|
|
216
215
|
* The default expectation is that the arguments are from node and have the application as argv[0]
|
|
217
216
|
* and the script being run in argv[1], with user parameters after that.
|
|
218
217
|
*
|
|
@@ -221,16 +220,16 @@ declare namespace commander {
|
|
|
221
220
|
* program.parse(process.argv);
|
|
222
221
|
* program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
|
|
223
222
|
* program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
224
|
-
*
|
|
225
|
-
* @returns
|
|
223
|
+
*
|
|
224
|
+
* @returns `this` command for chaining
|
|
226
225
|
*/
|
|
227
226
|
parse(argv?: string[], options?: ParseOptions): this;
|
|
228
227
|
|
|
229
228
|
/**
|
|
230
229
|
* Parse `argv`, setting options and invoking commands when defined.
|
|
231
|
-
*
|
|
230
|
+
*
|
|
232
231
|
* Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
|
|
233
|
-
*
|
|
232
|
+
*
|
|
234
233
|
* The default expectation is that the arguments are from node and have the application as argv[0]
|
|
235
234
|
* and the script being run in argv[1], with user parameters after that.
|
|
236
235
|
*
|
|
@@ -264,8 +263,8 @@ declare namespace commander {
|
|
|
264
263
|
|
|
265
264
|
/**
|
|
266
265
|
* Set the description.
|
|
267
|
-
*
|
|
268
|
-
* @returns
|
|
266
|
+
*
|
|
267
|
+
* @returns `this` command for chaining
|
|
269
268
|
*/
|
|
270
269
|
description(str: string, argsDescription?: {[argName: string]: string}): this;
|
|
271
270
|
/**
|
|
@@ -275,8 +274,10 @@ declare namespace commander {
|
|
|
275
274
|
|
|
276
275
|
/**
|
|
277
276
|
* Set an alias for the command.
|
|
278
|
-
*
|
|
279
|
-
*
|
|
277
|
+
*
|
|
278
|
+
* You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
|
|
279
|
+
*
|
|
280
|
+
* @returns `this` command for chaining
|
|
280
281
|
*/
|
|
281
282
|
alias(alias: string): this;
|
|
282
283
|
/**
|
|
@@ -284,10 +285,23 @@ declare namespace commander {
|
|
|
284
285
|
*/
|
|
285
286
|
alias(): string;
|
|
286
287
|
|
|
288
|
+
/**
|
|
289
|
+
* Set aliases for the command.
|
|
290
|
+
*
|
|
291
|
+
* Only the first alias is shown in the auto-generated help.
|
|
292
|
+
*
|
|
293
|
+
* @returns `this` command for chaining
|
|
294
|
+
*/
|
|
295
|
+
aliases(aliases: string[]): this;
|
|
296
|
+
/**
|
|
297
|
+
* Get aliases for the command.
|
|
298
|
+
*/
|
|
299
|
+
aliases(): string[];
|
|
300
|
+
|
|
287
301
|
/**
|
|
288
302
|
* Set the command usage.
|
|
289
|
-
*
|
|
290
|
-
* @returns
|
|
303
|
+
*
|
|
304
|
+
* @returns `this` command for chaining
|
|
291
305
|
*/
|
|
292
306
|
usage(str: string): this;
|
|
293
307
|
/**
|
|
@@ -297,8 +311,8 @@ declare namespace commander {
|
|
|
297
311
|
|
|
298
312
|
/**
|
|
299
313
|
* Set the name of the command.
|
|
300
|
-
*
|
|
301
|
-
* @returns
|
|
314
|
+
*
|
|
315
|
+
* @returns `this` command for chaining
|
|
302
316
|
*/
|
|
303
317
|
name(str: string): this;
|
|
304
318
|
/**
|
|
@@ -318,21 +332,21 @@ declare namespace commander {
|
|
|
318
332
|
* Return command help documentation.
|
|
319
333
|
*/
|
|
320
334
|
helpInformation(): string;
|
|
321
|
-
|
|
335
|
+
|
|
322
336
|
/**
|
|
323
337
|
* You can pass in flags and a description to override the help
|
|
324
338
|
* flags and help description for your command.
|
|
325
339
|
*/
|
|
326
340
|
helpOption(flags?: string, description?: string): this;
|
|
327
341
|
|
|
328
|
-
/**
|
|
342
|
+
/**
|
|
329
343
|
* Output help information and exit.
|
|
330
344
|
*/
|
|
331
345
|
help(cb?: (str: string) => string): never;
|
|
332
346
|
|
|
333
347
|
/**
|
|
334
348
|
* Add a listener (callback) for when events occur. (Implemented using EventEmitter.)
|
|
335
|
-
*
|
|
349
|
+
*
|
|
336
350
|
* @example
|
|
337
351
|
* program
|
|
338
352
|
* .on('--help', () -> {
|
|
@@ -341,28 +355,32 @@ declare namespace commander {
|
|
|
341
355
|
*/
|
|
342
356
|
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
343
357
|
}
|
|
344
|
-
type CommandConstructor =
|
|
345
|
-
|
|
358
|
+
type CommandConstructor = new (name?: string) => Command;
|
|
346
359
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
360
|
+
interface CommandOptions {
|
|
361
|
+
noHelp?: boolean; // old name for hidden
|
|
362
|
+
hidden?: boolean;
|
|
363
|
+
isDefault?: boolean;
|
|
364
|
+
}
|
|
365
|
+
interface ExecutableCommandOptions extends CommandOptions {
|
|
366
|
+
executableFile?: string;
|
|
367
|
+
}
|
|
352
368
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
369
|
+
interface ParseOptionsResult {
|
|
370
|
+
operands: string[];
|
|
371
|
+
unknown: string[];
|
|
372
|
+
}
|
|
357
373
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
374
|
+
interface CommanderStatic extends Command {
|
|
375
|
+
program: Command;
|
|
376
|
+
Command: CommandConstructor;
|
|
377
|
+
Option: OptionConstructor;
|
|
378
|
+
CommanderError: CommanderErrorConstructor;
|
|
379
|
+
}
|
|
364
380
|
|
|
365
381
|
}
|
|
366
382
|
|
|
383
|
+
// Declaring namespace AND global
|
|
384
|
+
// eslint-disable-next-line no-redeclare
|
|
367
385
|
declare const commander: commander.CommanderStatic;
|
|
368
386
|
export = commander;
|