commander 2.17.1 → 2.20.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 CHANGED
@@ -1,3 +1,34 @@
1
+ 2.20.1 / 2019-09-29
2
+ ==================
3
+
4
+ * Improve executable subcommand tracking
5
+ * Update dev dependencies
6
+
7
+ 2.20.0 / 2019-04-02
8
+ ==================
9
+
10
+ * fix: resolve symbolic links completely when hunting for subcommands (#935)
11
+ * Update index.d.ts (#930)
12
+ * Update Readme.md (#924)
13
+ * Remove --save option as it isn't required anymore (#918)
14
+ * Add link to the license file (#900)
15
+ * Added example of receiving args from options (#858)
16
+ * Added missing semicolon (#882)
17
+ * Add extension to .eslintrc (#876)
18
+
19
+ 2.19.0 / 2018-10-02
20
+ ==================
21
+
22
+ * Removed newline after Options and Commands headers (#864)
23
+ * Bugfix - Error output (#862)
24
+ * Fix to change default value to string (#856)
25
+
26
+ 2.18.0 / 2018-09-07
27
+ ==================
28
+
29
+ * Standardize help output (#853)
30
+ * chmod 644 travis.yml (#851)
31
+ * add support for execute typescript subcommand via ts-node (#849)
1
32
 
2
33
  2.17.1 / 2018-08-07
3
34
  ==================
package/Readme.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  ## Installation
15
15
 
16
- $ npm install commander --save
16
+ $ npm install commander
17
17
 
18
18
  ## Option parsing
19
19
 
@@ -45,7 +45,7 @@ console.log(' - %s cheese', program.cheese);
45
45
 
46
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.
47
47
 
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
+ 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.
49
49
 
50
50
  ```js
51
51
  #!/usr/bin/env node
@@ -65,6 +65,17 @@ if (program.sauce) console.log(' with sauce');
65
65
  else console.log(' without sauce');
66
66
  ```
67
67
 
68
+ To get string arguments from options you will need to use angle brackets <> for required inputs or square brackets [] for optional inputs.
69
+
70
+ e.g. ```.option('-m --myarg [myVar]', 'my super cool description')```
71
+
72
+ Then to access the input if it was passed in.
73
+
74
+ e.g. ```var myInput = program.myarg```
75
+
76
+ **NOTE**: If you pass a argument without using brackets the example above will return true and not the value passed in.
77
+
78
+
68
79
  ## Version option
69
80
 
70
81
  Calling the `version` implicitly adds the `-V` and `--version` options to the command.
@@ -153,7 +164,7 @@ program
153
164
  .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
154
165
  .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
155
166
  .parse(process.argv);
156
-
167
+
157
168
  console.log(' size: %j', program.size);
158
169
  console.log(' drink: %j', program.drink);
159
170
  ```
@@ -248,22 +259,19 @@ You can enable `--harmony` option in two ways:
248
259
  The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
249
260
 
250
261
  ```
251
- $ ./examples/pizza --help
252
-
253
- Usage: pizza [options]
262
+ $ ./examples/pizza --help
263
+ Usage: pizza [options]
254
264
 
255
- An application for pizzas ordering
256
-
257
- Options:
258
-
259
- -h, --help output usage information
260
- -V, --version output the version number
261
- -p, --peppers Add peppers
262
- -P, --pineapple Add pineapple
263
- -b, --bbq Add bbq sauce
264
- -c, --cheese <type> Add the specified type of cheese [marble]
265
- -C, --no-cheese You do not want any cheese
265
+ An application for pizzas ordering
266
266
 
267
+ Options:
268
+ -h, --help output usage information
269
+ -V, --version output the version number
270
+ -p, --peppers Add peppers
271
+ -P, --pineapple Add pineapple
272
+ -b, --bbq Add bbq sauce
273
+ -c, --cheese <type> Add the specified type of cheese [marble]
274
+ -C, --no-cheese You do not want any cheese
267
275
  ```
268
276
 
269
277
  ## Custom help
@@ -271,7 +279,7 @@ You can enable `--harmony` option in two ways:
271
279
  You can display arbitrary `-h, --help` information
272
280
  by listening for "--help". Commander will automatically
273
281
  exit once you are done so that the remainder of your program
274
- does not execute causing undesired behaviours, for example
282
+ does not execute causing undesired behaviors, for example
275
283
  in the following executable "stuff" will not output when
276
284
  `--help` is used.
277
285
 
@@ -294,11 +302,10 @@ program
294
302
  // node's emit() is immediate
295
303
 
296
304
  program.on('--help', function(){
297
- console.log(' Examples:');
298
- console.log('');
299
- console.log(' $ custom-help --help');
300
- console.log(' $ custom-help -h');
301
- console.log('');
305
+ console.log('')
306
+ console.log('Examples:');
307
+ console.log(' $ custom-help --help');
308
+ console.log(' $ custom-help -h');
302
309
  });
303
310
 
304
311
  program.parse(process.argv);
@@ -309,11 +316,9 @@ console.log('stuff');
309
316
  Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:
310
317
 
311
318
  ```
312
-
313
319
  Usage: custom-help [options]
314
320
 
315
321
  Options:
316
-
317
322
  -h, --help output usage information
318
323
  -V, --version output the version number
319
324
  -f, --foo enable some foo
@@ -321,10 +326,8 @@ Options:
321
326
  -B, --baz enable some baz
322
327
 
323
328
  Examples:
324
-
325
329
  $ custom-help --help
326
330
  $ custom-help -h
327
-
328
331
  ```
329
332
 
330
333
  ## .outputHelp(cb)
@@ -402,11 +405,11 @@ program
402
405
  .action(function(cmd, options){
403
406
  console.log('exec "%s" using %s mode', cmd, options.exec_mode);
404
407
  }).on('--help', function() {
405
- console.log(' Examples:');
406
- console.log();
407
- console.log(' $ deploy exec sequential');
408
- console.log(' $ deploy exec async');
409
- console.log();
408
+ console.log('');
409
+ console.log('Examples:');
410
+ console.log('');
411
+ console.log(' $ deploy exec sequential');
412
+ console.log(' $ deploy exec async');
410
413
  });
411
414
 
412
415
  program
@@ -422,4 +425,4 @@ More Demos can be found in the [examples](https://github.com/tj/commander.js/tre
422
425
 
423
426
  ## License
424
427
 
425
- MIT
428
+ [MIT](https://github.com/tj/commander.js/blob/master/LICENSE)
package/index.js CHANGED
@@ -99,7 +99,7 @@ Option.prototype.is = function(arg) {
99
99
  function Command(name) {
100
100
  this.commands = [];
101
101
  this.options = [];
102
- this._execs = {};
102
+ this._execs = new Set();
103
103
  this._allowUnknownOption = false;
104
104
  this._args = [];
105
105
  this._name = name || '';
@@ -178,7 +178,7 @@ Command.prototype.command = function(name, desc, opts) {
178
178
  if (desc) {
179
179
  cmd.description(desc);
180
180
  this.executables = true;
181
- this._execs[cmd._name] = true;
181
+ this._execs.add(cmd._name);
182
182
  if (opts.isDefault) this.defaultExecutable = cmd._name;
183
183
  }
184
184
  cmd._noHelp = !!opts.noHelp;
@@ -484,7 +484,7 @@ Command.prototype.parse = function(argv) {
484
484
  })[0];
485
485
  }
486
486
 
487
- if (this._execs[name] && typeof this._execs[name] !== 'function') {
487
+ if (this._execs.has(name)) {
488
488
  return this.executeSubCommand(argv, args, parsed.unknown);
489
489
  } else if (aliasCommand) {
490
490
  // is alias of a subCommand
@@ -523,27 +523,27 @@ 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, '.js') + '-' + args[0];
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
530
- var baseDir,
531
- link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f;
530
+ var baseDir;
532
531
 
533
- // when symbolink is relative path
534
- if (link !== f && link.charAt(0) !== '/') {
535
- link = path.join(dirname(f), link);
536
- }
537
- baseDir = dirname(link);
532
+ var resolvedLink = fs.realpathSync(f);
533
+
534
+ baseDir = dirname(resolvedLink);
538
535
 
539
536
  // prefer local `./<bin>` to bin in the $PATH
540
537
  var localBin = path.join(baseDir, bin);
541
538
 
542
- // whether bin file is a js script with explicit `.js` extension
539
+ // whether bin file is a js script with explicit `.js` or `.ts` extension
543
540
  var isExplicitJS = false;
544
541
  if (exists(localBin + '.js')) {
545
542
  bin = localBin + '.js';
546
543
  isExplicitJS = true;
544
+ } else if (exists(localBin + '.ts')) {
545
+ bin = localBin + '.ts';
546
+ isExplicitJS = true;
547
547
  } else if (exists(localBin)) {
548
548
  bin = localBin;
549
549
  }
@@ -577,9 +577,9 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
577
577
  proc.on('close', process.exit.bind(process));
578
578
  proc.on('error', function(err) {
579
579
  if (err.code === 'ENOENT') {
580
- console.error('\n %s(1) does not exist, try --help\n', bin);
580
+ console.error('error: %s(1) does not exist, try --help', bin);
581
581
  } else if (err.code === 'EACCES') {
582
- console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
582
+ console.error('error: %s(1) not executable. try chmod or run with root', bin);
583
583
  }
584
584
  process.exit(1);
585
585
  });
@@ -661,7 +661,7 @@ Command.prototype.parseArgs = function(args, unknown) {
661
661
  this.unknownOption(unknown[0]);
662
662
  }
663
663
  if (this.commands.length === 0 &&
664
- this._args.filter(function(a) { return a.required }).length === 0) {
664
+ this._args.filter(function(a) { return a.required; }).length === 0) {
665
665
  this.emit('command:*');
666
666
  }
667
667
  }
@@ -789,9 +789,7 @@ Command.prototype.opts = function() {
789
789
  */
790
790
 
791
791
  Command.prototype.missingArgument = function(name) {
792
- console.error();
793
- console.error(" error: missing required argument `%s'", name);
794
- console.error();
792
+ console.error("error: missing required argument `%s'", name);
795
793
  process.exit(1);
796
794
  };
797
795
 
@@ -804,13 +802,11 @@ Command.prototype.missingArgument = function(name) {
804
802
  */
805
803
 
806
804
  Command.prototype.optionMissingArgument = function(option, flag) {
807
- console.error();
808
805
  if (flag) {
809
- console.error(" error: option `%s' argument missing, got `%s'", option.flags, flag);
806
+ console.error("error: option `%s' argument missing, got `%s'", option.flags, flag);
810
807
  } else {
811
- console.error(" error: option `%s' argument missing", option.flags);
808
+ console.error("error: option `%s' argument missing", option.flags);
812
809
  }
813
- console.error();
814
810
  process.exit(1);
815
811
  };
816
812
 
@@ -823,9 +819,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
823
819
 
824
820
  Command.prototype.unknownOption = function(flag) {
825
821
  if (this._allowUnknownOption) return;
826
- console.error();
827
- console.error(" error: unknown option `%s'", flag);
828
- console.error();
822
+ console.error("error: unknown option `%s'", flag);
829
823
  process.exit(1);
830
824
  };
831
825
 
@@ -837,9 +831,7 @@ Command.prototype.unknownOption = function(flag) {
837
831
  */
838
832
 
839
833
  Command.prototype.variadicArgNotLast = function(name) {
840
- console.error();
841
- console.error(" error: variadic arguments must be last `%s'", name);
842
- console.error();
834
+ console.error("error: variadic arguments must be last `%s'", name);
843
835
  process.exit(1);
844
836
  };
845
837
 
@@ -1050,7 +1042,7 @@ Command.prototype.optionHelp = function() {
1050
1042
  // Append the help information
1051
1043
  return this.options.map(function(option) {
1052
1044
  return pad(option.flags, width) + ' ' + option.description +
1053
- ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + option.defaultValue + ')' : '');
1045
+ ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
1054
1046
  }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
1055
1047
  .join('\n');
1056
1048
  };
@@ -1069,12 +1061,11 @@ Command.prototype.commandHelp = function() {
1069
1061
  var width = this.padWidth();
1070
1062
 
1071
1063
  return [
1072
- ' Commands:',
1073
- '',
1064
+ 'Commands:',
1074
1065
  commands.map(function(cmd) {
1075
1066
  var desc = cmd[1] ? ' ' + cmd[1] : '';
1076
1067
  return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
1077
- }).join('\n').replace(/^/gm, ' '),
1068
+ }).join('\n').replace(/^/gm, ' '),
1078
1069
  ''
1079
1070
  ].join('\n');
1080
1071
  };
@@ -1090,17 +1081,17 @@ Command.prototype.helpInformation = function() {
1090
1081
  var desc = [];
1091
1082
  if (this._description) {
1092
1083
  desc = [
1093
- ' ' + this._description,
1084
+ this._description,
1094
1085
  ''
1095
1086
  ];
1096
1087
 
1097
1088
  var argsDescription = this._argsDescription;
1098
1089
  if (argsDescription && this._args.length) {
1099
1090
  var width = this.padWidth();
1100
- desc.push(' Arguments:');
1091
+ desc.push('Arguments:');
1101
1092
  desc.push('');
1102
1093
  this._args.forEach(function(arg) {
1103
- desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
1094
+ desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
1104
1095
  });
1105
1096
  desc.push('');
1106
1097
  }
@@ -1111,8 +1102,7 @@ Command.prototype.helpInformation = function() {
1111
1102
  cmdName = cmdName + '|' + this._alias;
1112
1103
  }
1113
1104
  var usage = [
1114
- '',
1115
- ' Usage: ' + cmdName + ' ' + this.usage(),
1105
+ 'Usage: ' + cmdName + ' ' + this.usage(),
1116
1106
  ''
1117
1107
  ];
1118
1108
 
@@ -1121,9 +1111,8 @@ Command.prototype.helpInformation = function() {
1121
1111
  if (commandHelp) cmds = [commandHelp];
1122
1112
 
1123
1113
  var options = [
1124
- ' Options:',
1125
- '',
1126
- '' + this.optionHelp().replace(/^/gm, ' '),
1114
+ 'Options:',
1115
+ '' + this.optionHelp().replace(/^/gm, ' '),
1127
1116
  ''
1128
1117
  ];
1129
1118
 
@@ -1131,7 +1120,6 @@ Command.prototype.helpInformation = function() {
1131
1120
  .concat(desc)
1132
1121
  .concat(options)
1133
1122
  .concat(cmds)
1134
- .concat([''])
1135
1123
  .join('\n');
1136
1124
  };
1137
1125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander",
3
- "version": "2.17.1",
3
+ "version": "2.20.1",
4
4
  "description": "the complete solution for node.js command-line programs",
5
5
  "keywords": [
6
6
  "commander",
@@ -26,12 +26,13 @@
26
26
  ],
27
27
  "dependencies": {},
28
28
  "devDependencies": {
29
- "@types/node": "^10.5.7",
30
- "eslint": "^5.3.0",
29
+ "@types/node": "^12.7.8",
30
+ "eslint": "^6.4.0",
31
31
  "should": "^13.2.3",
32
- "sinon": "^6.1.4",
33
- "standard": "^11.0.1",
34
- "typescript": "^2.9.2"
32
+ "sinon": "^7.5.0",
33
+ "standard": "^14.3.1",
34
+ "ts-node": "^8.4.1",
35
+ "typescript": "^3.6.3"
35
36
  },
36
37
  "typings": "typings/index.d.ts"
37
38
  }
@@ -226,9 +226,10 @@ declare namespace local {
226
226
  * Set the description to `str`.
227
227
  *
228
228
  * @param {string} str
229
+ * @param {{[argName: string]: string}} argsDescription
229
230
  * @return {(Command | string)}
230
231
  */
231
- description(str: string): Command;
232
+ description(str: string, argsDescription?: {[argName: string]: string}): Command;
232
233
  description(): string;
233
234
 
234
235
  /**