commander 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -8,13 +8,31 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
8
8
  <!-- markdownlint-disable MD024 -->
9
9
  <!-- markdownlint-disable MD004 -->
10
10
 
11
+ ## [6.1.0] (2020-08-28)
12
+
13
+ ### Added
14
+
15
+ - include URL to relevant section of README for error for potential conflict between Command properties and option values ([#1306])
16
+ - `.combineFlagAndOptionalValue(false)` to ease upgrade path from older versions of Commander ([#1326])
17
+ - allow disabling the built-in help option using `.helpOption(false)` ([#1325])
18
+ - allow just some arguments in `argumentDescription` to `.description()` ([#1323])
19
+
20
+ ### Changed
21
+
22
+ - tidy async test and remove lint override ([#1312])
23
+
24
+ ### Fixed
25
+
26
+ - executable subcommand launching when script path not known ([#1322])
27
+
11
28
  ## [6.0.0] (2020-07-21)
12
29
 
13
30
  ### Added
14
31
 
15
32
  - add support for variadic options ([#1250])
16
33
  - allow options to be added with just a short flag ([#1256])
17
- - throw an error if there might be a clash between option name and a Command property, with advice on how to resolve ([#1275])
34
+ - *Breaking* the option property has same case as flag. e.g. flag `-n` accessed as `opts().n` (previously uppercase)
35
+ - *Breaking* throw an error if there might be a clash between option name and a Command property, with advice on how to resolve ([#1275])
18
36
 
19
37
  ### Fixed
20
38
 
@@ -113,6 +131,9 @@ If you use `program.args` or more complicated tests to detect a missing subcomma
113
131
 
114
132
  If you use `.command('*')` to add a default command, you may be be able to switch to `isDefault:true` with a named command.
115
133
 
134
+ If you want to continue combining short options with optional values as though they were boolean flags, set `combineFlagAndOptionalValue(false)`
135
+ to expand `-fb` to `-f -b` rather than `-f b`.
136
+
116
137
  ## [5.0.0-4] (2020-03-03)
117
138
 
118
139
  (Released in 5.0.0)
@@ -281,8 +302,15 @@ if (program.rawArgs.length < 3) ...
281
302
  [#1256]: https://github.com/tj/commander.js/pull/1256
282
303
  [#1275]: https://github.com/tj/commander.js/pull/1275
283
304
  [#1301]: https://github.com/tj/commander.js/issues/1301
305
+ [#1306]: https://github.com/tj/commander.js/pull/1306
306
+ [#1312]: https://github.com/tj/commander.js/pull/1312
307
+ [#1322]: https://github.com/tj/commander.js/pull/1322
308
+ [#1323]: https://github.com/tj/commander.js/pull/1323
309
+ [#1325]: https://github.com/tj/commander.js/pull/1325
310
+ [#1326]: https://github.com/tj/commander.js/pull/1326
284
311
 
285
312
  [Unreleased]: https://github.com/tj/commander.js/compare/master...develop
313
+ [6.1.0]: https://github.com/tj/commander.js/compare/v6.0.0..v6.1.0
286
314
  [6.0.0]: https://github.com/tj/commander.js/compare/v5.1.0..v6.0.0
287
315
  [6.0.0-0]: https://github.com/tj/commander.js/compare/v5.1.0..v6.0.0-0
288
316
  [5.1.0]: https://github.com/tj/commander.js/compare/v5.0.0..v5.1.0
package/Readme.md CHANGED
@@ -376,18 +376,11 @@ program
376
376
  .version('0.1.0')
377
377
  .arguments('<cmd> [env]')
378
378
  .action(function (cmd, env) {
379
- cmdValue = cmd;
380
- envValue = env;
379
+ console.log('command:', cmdValue);
380
+ console.log('environment:', envValue || 'no environment given');
381
381
  });
382
382
 
383
383
  program.parse(process.argv);
384
-
385
- if (typeof cmdValue === 'undefined') {
386
- console.error('no command given!');
387
- process.exit(1);
388
- }
389
- console.log('command:', cmdValue);
390
- console.log('environment:', envValue || "no environment given");
391
384
  ```
392
385
 
393
386
  The last argument of a command can be variadic, and only the last argument. To make an argument variadic you
@@ -566,7 +559,7 @@ from `--help` listeners.)
566
559
 
567
560
  ### .helpOption(flags, description)
568
561
 
569
- Override the default help flags and description.
562
+ Override the default help flags and description. Pass false to disable the built-in help option.
570
563
 
571
564
  ```js
572
565
  program
package/index.js CHANGED
@@ -127,8 +127,10 @@ class Command extends EventEmitter {
127
127
  this._defaultCommandName = null;
128
128
  this._exitCallback = null;
129
129
  this._aliases = [];
130
+ this._combineFlagAndOptionalValue = true;
130
131
 
131
132
  this._hidden = false;
133
+ this._hasHelpOption = true;
132
134
  this._helpFlags = '-h, --help';
133
135
  this._helpDescription = 'display help for command';
134
136
  this._helpShortFlag = '-h';
@@ -184,6 +186,7 @@ class Command extends EventEmitter {
184
186
  if (opts.isDefault) this._defaultCommandName = cmd._name;
185
187
 
186
188
  cmd._hidden = !!(opts.noHelp || opts.hidden);
189
+ cmd._hasHelpOption = this._hasHelpOption;
187
190
  cmd._helpFlags = this._helpFlags;
188
191
  cmd._helpDescription = this._helpDescription;
189
192
  cmd._helpShortFlag = this._helpShortFlag;
@@ -194,6 +197,7 @@ class Command extends EventEmitter {
194
197
  cmd._exitCallback = this._exitCallback;
195
198
  cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;
196
199
  cmd._passCommandToAction = this._passCommandToAction;
200
+ cmd._combineFlagAndOptionalValue = this._combineFlagAndOptionalValue;
197
201
 
198
202
  cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor
199
203
  this.commands.push(cmd);
@@ -468,7 +472,9 @@ class Command extends EventEmitter {
468
472
  throw new Error(`option '${option.name()}' clashes with existing property '${option.attributeName()}' on Command
469
473
  - call storeOptionsAsProperties(false) to store option values safely,
470
474
  - or call storeOptionsAsProperties(true) to suppress this check,
471
- - or change option name`);
475
+ - or change option name
476
+
477
+ Read more on https://git.io/JJc0W`);
472
478
  }
473
479
  };
474
480
 
@@ -634,6 +640,23 @@ class Command extends EventEmitter {
634
640
  return this._optionEx({ mandatory: true }, flags, description, fn, defaultValue);
635
641
  };
636
642
 
643
+ /**
644
+ * Alter parsing of short flags with optional values.
645
+ *
646
+ * Examples:
647
+ *
648
+ * // for `.option('-f,--flag [value]'):
649
+ * .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
650
+ * .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
651
+ *
652
+ * @param {Boolean} [arg] - if `true` or omitted, an optional value can be specified directly after the flag.
653
+ * @api public
654
+ */
655
+ combineFlagAndOptionalValue(arg) {
656
+ this._combineFlagAndOptionalValue = (arg === undefined) || arg;
657
+ return this;
658
+ };
659
+
637
660
  /**
638
661
  * Allow unknown options on the command line.
639
662
  *
@@ -821,7 +844,11 @@ class Command extends EventEmitter {
821
844
  this._checkForMissingMandatoryOptions();
822
845
 
823
846
  // Want the entry script as the reference for command name and directory for searching for other files.
824
- const scriptPath = this._scriptPath;
847
+ let scriptPath = this._scriptPath;
848
+ // Fallback in case not set, due to how Command created or called.
849
+ if (!scriptPath && process.mainModule) {
850
+ scriptPath = process.mainModule.filename;
851
+ }
825
852
 
826
853
  let baseDir;
827
854
  try {
@@ -1103,7 +1130,7 @@ class Command extends EventEmitter {
1103
1130
  if (arg.length > 2 && arg[0] === '-' && arg[1] !== '-') {
1104
1131
  const option = this._findOption(`-${arg[1]}`);
1105
1132
  if (option) {
1106
- if (option.required || option.optional) {
1133
+ if (option.required || (option.optional && this._combineFlagAndOptionalValue)) {
1107
1134
  // option with value following in same argument
1108
1135
  this.emit(`option:${option.name()}`, arg.slice(2));
1109
1136
  } else {
@@ -1230,7 +1257,8 @@ class Command extends EventEmitter {
1230
1257
  partCommands.unshift(parentCmd.name());
1231
1258
  }
1232
1259
  const fullCommand = partCommands.join(' ');
1233
- const message = `error: unknown command '${this.args[0]}'. See '${fullCommand} ${this._helpLongFlag}'.`;
1260
+ const message = `error: unknown command '${this.args[0]}'.` +
1261
+ (this._hasHelpOption ? ` See '${fullCommand} ${this._helpLongFlag}'.` : '');
1234
1262
  console.error(message);
1235
1263
  this._exit(1, 'commander.unknownCommand', message);
1236
1264
  };
@@ -1339,9 +1367,11 @@ class Command extends EventEmitter {
1339
1367
  const args = this._args.map((arg) => {
1340
1368
  return humanReadableArgName(arg);
1341
1369
  });
1342
- return '[options]' +
1343
- (this.commands.length ? ' [command]' : '') +
1344
- (this._args.length ? ' ' + args.join(' ') : '');
1370
+ return [].concat(
1371
+ (this.options.length || this._hasHelpOption ? '[options]' : []),
1372
+ (this.commands.length ? '[command]' : []),
1373
+ (this._args.length ? args : [])
1374
+ ).join(' ');
1345
1375
  }
1346
1376
 
1347
1377
  this._usage = str;
@@ -1484,8 +1514,8 @@ class Command extends EventEmitter {
1484
1514
  });
1485
1515
 
1486
1516
  // Implicit help
1487
- const showShortHelpFlag = this._helpShortFlag && !this._findOption(this._helpShortFlag);
1488
- const showLongHelpFlag = !this._findOption(this._helpLongFlag);
1517
+ const showShortHelpFlag = this._hasHelpOption && this._helpShortFlag && !this._findOption(this._helpShortFlag);
1518
+ const showLongHelpFlag = this._hasHelpOption && !this._findOption(this._helpLongFlag);
1489
1519
  if (showShortHelpFlag || showLongHelpFlag) {
1490
1520
  let helpFlags = this._helpFlags;
1491
1521
  if (!showShortHelpFlag) {
@@ -1548,7 +1578,7 @@ class Command extends EventEmitter {
1548
1578
  desc.push('Arguments:');
1549
1579
  desc.push('');
1550
1580
  this._args.forEach((arg) => {
1551
- desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name], descriptionWidth, width + 4));
1581
+ desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name] || '', descriptionWidth, width + 4));
1552
1582
  });
1553
1583
  desc.push('');
1554
1584
  }
@@ -1571,11 +1601,14 @@ class Command extends EventEmitter {
1571
1601
  const commandHelp = this.commandHelp();
1572
1602
  if (commandHelp) cmds = [commandHelp];
1573
1603
 
1574
- const options = [
1575
- 'Options:',
1576
- '' + this.optionHelp().replace(/^/gm, ' '),
1577
- ''
1578
- ];
1604
+ let options = [];
1605
+ if (this._hasHelpOption || this.options.length > 0) {
1606
+ options = [
1607
+ 'Options:',
1608
+ '' + this.optionHelp().replace(/^/gm, ' '),
1609
+ ''
1610
+ ];
1611
+ }
1579
1612
 
1580
1613
  return usage
1581
1614
  .concat(desc)
@@ -1609,15 +1642,20 @@ class Command extends EventEmitter {
1609
1642
 
1610
1643
  /**
1611
1644
  * You can pass in flags and a description to override the help
1612
- * flags and help description for your command.
1645
+ * flags and help description for your command. Pass in false to
1646
+ * disable the built-in help option.
1613
1647
  *
1614
- * @param {string} [flags]
1648
+ * @param {string | boolean} [flags]
1615
1649
  * @param {string} [description]
1616
1650
  * @return {Command} `this` command for chaining
1617
1651
  * @api public
1618
1652
  */
1619
1653
 
1620
1654
  helpOption(flags, description) {
1655
+ if (typeof flags === 'boolean') {
1656
+ this._hasHelpOption = flags;
1657
+ return this;
1658
+ }
1621
1659
  this._helpFlags = flags || this._helpFlags;
1622
1660
  this._helpDescription = description || this._helpDescription;
1623
1661
 
@@ -1750,7 +1788,7 @@ function optionalWrap(str, width, indent) {
1750
1788
  */
1751
1789
 
1752
1790
  function outputHelpIfRequested(cmd, args) {
1753
- const helpOption = args.find(arg => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag);
1791
+ const helpOption = cmd._hasHelpOption && args.find(arg => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag);
1754
1792
  if (helpOption) {
1755
1793
  cmd.outputHelp();
1756
1794
  // (Do not have all displayed text available so only passing placeholder.)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "the complete solution for node.js command-line programs",
5
5
  "keywords": [
6
6
  "commander",
@@ -201,6 +201,18 @@ declare namespace commander {
201
201
  */
202
202
  passCommandToAction(value?: boolean): this;
203
203
 
204
+ /**
205
+ * Alter parsing of short flags with optional values.
206
+ *
207
+ * @example
208
+ * // for `.option('-f,--flag [value]'):
209
+ * .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
210
+ * .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
211
+ *
212
+ * @returns `this` command for chaining
213
+ */
214
+ combineFlagAndOptionalValue(arg?: boolean): this;
215
+
204
216
  /**
205
217
  * Allow unknown options on the command line.
206
218
  *
@@ -335,9 +347,10 @@ declare namespace commander {
335
347
 
336
348
  /**
337
349
  * You can pass in flags and a description to override the help
338
- * flags and help description for your command.
350
+ * flags and help description for your command. Pass in false
351
+ * to disable the built-in help option.
339
352
  */
340
- helpOption(flags?: string, description?: string): this;
353
+ helpOption(flags?: string | boolean, description?: string): this;
341
354
 
342
355
  /**
343
356
  * Output help information and exit.