commander 2.17.0 → 2.20.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
@@ -1,4 +1,35 @@
1
1
 
2
+ 2.20.0 / 2019-04-02
3
+ ==================
4
+
5
+ * fix: resolve symbolic links completely when hunting for subcommands (#935)
6
+ * Update index.d.ts (#930)
7
+ * Update Readme.md (#924)
8
+ * Remove --save option as it isn't required anymore (#918)
9
+ * Add link to the license file (#900)
10
+ * Added example of receiving args from options (#858)
11
+ * Added missing semicolon (#882)
12
+ * Add extension to .eslintrc (#876)
13
+
14
+ 2.19.0 / 2018-10-02
15
+ ==================
16
+
17
+ * Removed newline after Options and Commands headers (#864)
18
+ * Bugfix - Error output (#862)
19
+ * Fix to change default value to string (#856)
20
+
21
+ 2.18.0 / 2018-09-07
22
+ ==================
23
+
24
+ * Standardize help output (#853)
25
+ * chmod 644 travis.yml (#851)
26
+ * add support for execute typescript subcommand via ts-node (#849)
27
+
28
+ 2.17.1 / 2018-08-07
29
+ ==================
30
+
31
+ * Fix bug in command emit (#844)
32
+
2
33
  2.17.0 / 2018-08-03
3
34
  ==================
4
35
 
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
@@ -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
  });
@@ -660,7 +660,8 @@ Command.prototype.parseArgs = function(args, unknown) {
660
660
  if (unknown.length > 0) {
661
661
  this.unknownOption(unknown[0]);
662
662
  }
663
- if (this._args.filter(a => a.required).length === 0) {
663
+ if (this.commands.length === 0 &&
664
+ this._args.filter(function(a) { return a.required; }).length === 0) {
664
665
  this.emit('command:*');
665
666
  }
666
667
  }
@@ -788,9 +789,7 @@ Command.prototype.opts = function() {
788
789
  */
789
790
 
790
791
  Command.prototype.missingArgument = function(name) {
791
- console.error();
792
- console.error(" error: missing required argument `%s'", name);
793
- console.error();
792
+ console.error("error: missing required argument `%s'", name);
794
793
  process.exit(1);
795
794
  };
796
795
 
@@ -803,13 +802,11 @@ Command.prototype.missingArgument = function(name) {
803
802
  */
804
803
 
805
804
  Command.prototype.optionMissingArgument = function(option, flag) {
806
- console.error();
807
805
  if (flag) {
808
- 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);
809
807
  } else {
810
- console.error(" error: option `%s' argument missing", option.flags);
808
+ console.error("error: option `%s' argument missing", option.flags);
811
809
  }
812
- console.error();
813
810
  process.exit(1);
814
811
  };
815
812
 
@@ -822,9 +819,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
822
819
 
823
820
  Command.prototype.unknownOption = function(flag) {
824
821
  if (this._allowUnknownOption) return;
825
- console.error();
826
- console.error(" error: unknown option `%s'", flag);
827
- console.error();
822
+ console.error("error: unknown option `%s'", flag);
828
823
  process.exit(1);
829
824
  };
830
825
 
@@ -836,9 +831,7 @@ Command.prototype.unknownOption = function(flag) {
836
831
  */
837
832
 
838
833
  Command.prototype.variadicArgNotLast = function(name) {
839
- console.error();
840
- console.error(" error: variadic arguments must be last `%s'", name);
841
- console.error();
834
+ console.error("error: variadic arguments must be last `%s'", name);
842
835
  process.exit(1);
843
836
  };
844
837
 
@@ -1049,7 +1042,7 @@ Command.prototype.optionHelp = function() {
1049
1042
  // Append the help information
1050
1043
  return this.options.map(function(option) {
1051
1044
  return pad(option.flags, width) + ' ' + option.description +
1052
- ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + option.defaultValue + ')' : '');
1045
+ ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
1053
1046
  }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
1054
1047
  .join('\n');
1055
1048
  };
@@ -1068,12 +1061,11 @@ Command.prototype.commandHelp = function() {
1068
1061
  var width = this.padWidth();
1069
1062
 
1070
1063
  return [
1071
- ' Commands:',
1072
- '',
1064
+ 'Commands:',
1073
1065
  commands.map(function(cmd) {
1074
1066
  var desc = cmd[1] ? ' ' + cmd[1] : '';
1075
1067
  return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
1076
- }).join('\n').replace(/^/gm, ' '),
1068
+ }).join('\n').replace(/^/gm, ' '),
1077
1069
  ''
1078
1070
  ].join('\n');
1079
1071
  };
@@ -1089,17 +1081,17 @@ Command.prototype.helpInformation = function() {
1089
1081
  var desc = [];
1090
1082
  if (this._description) {
1091
1083
  desc = [
1092
- ' ' + this._description,
1084
+ this._description,
1093
1085
  ''
1094
1086
  ];
1095
1087
 
1096
1088
  var argsDescription = this._argsDescription;
1097
1089
  if (argsDescription && this._args.length) {
1098
1090
  var width = this.padWidth();
1099
- desc.push(' Arguments:');
1091
+ desc.push('Arguments:');
1100
1092
  desc.push('');
1101
1093
  this._args.forEach(function(arg) {
1102
- desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
1094
+ desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
1103
1095
  });
1104
1096
  desc.push('');
1105
1097
  }
@@ -1110,8 +1102,7 @@ Command.prototype.helpInformation = function() {
1110
1102
  cmdName = cmdName + '|' + this._alias;
1111
1103
  }
1112
1104
  var usage = [
1113
- '',
1114
- ' Usage: ' + cmdName + ' ' + this.usage(),
1105
+ 'Usage: ' + cmdName + ' ' + this.usage(),
1115
1106
  ''
1116
1107
  ];
1117
1108
 
@@ -1120,9 +1111,8 @@ Command.prototype.helpInformation = function() {
1120
1111
  if (commandHelp) cmds = [commandHelp];
1121
1112
 
1122
1113
  var options = [
1123
- ' Options:',
1124
- '',
1125
- '' + this.optionHelp().replace(/^/gm, ' '),
1114
+ 'Options:',
1115
+ '' + this.optionHelp().replace(/^/gm, ' '),
1126
1116
  ''
1127
1117
  ];
1128
1118
 
@@ -1130,7 +1120,6 @@ Command.prototype.helpInformation = function() {
1130
1120
  .concat(desc)
1131
1121
  .concat(options)
1132
1122
  .concat(cmds)
1133
- .concat([''])
1134
1123
  .join('\n');
1135
1124
  };
1136
1125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander",
3
- "version": "2.17.0",
3
+ "version": "2.20.0",
4
4
  "description": "the complete solution for node.js command-line programs",
5
5
  "keywords": [
6
6
  "commander",
@@ -26,11 +26,12 @@
26
26
  ],
27
27
  "dependencies": {},
28
28
  "devDependencies": {
29
- "@types/node": "^10.5.5",
30
- "eslint": "^5.2.0",
29
+ "@types/node": "^10.11.3",
30
+ "eslint": "^5.6.1",
31
31
  "should": "^13.2.3",
32
- "sinon": "^6.1.4",
33
- "standard": "^11.0.1",
32
+ "sinon": "^6.3.4",
33
+ "standard": "^12.0.1",
34
+ "ts-node": "^7.0.1",
34
35
  "typescript": "^2.9.2"
35
36
  },
36
37
  "typings": "typings/index.d.ts"
@@ -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
  /**