commander 2.6.0 → 2.8.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.
Files changed (5) hide show
  1. package/History.md +34 -0
  2. package/LICENSE +22 -0
  3. package/Readme.md +69 -27
  4. package/index.js +100 -17
  5. package/package.json +31 -12
package/History.md CHANGED
@@ -1,3 +1,37 @@
1
+
2
+ 2.8.1 / 2015-04-22
3
+ ==================
4
+
5
+ * Back out `support multiline description` Close #396 #397
6
+
7
+
8
+
9
+ 2.8.0 / 2015-04-07
10
+ ==================
11
+
12
+ * Add `process.execArg` support, execution args like `--harmony` will be passed to sub-commands #387 @DigitalIO @zhiyelee
13
+ * Fix bug in Git-style sub-commands #372 @zhiyelee
14
+ * Allow commands to be hidden from help #383 @tonylukasavage
15
+ * When git-style sub-commands are in use, yet none are called, display help #382 @claylo
16
+ * Add ability to specify arguments syntax for top-level command #258 @rrthomas
17
+ * Support multiline descriptions #208 @zxqfox
18
+
19
+ 2.7.1 / 2015-03-11
20
+ ==================
21
+
22
+ * Revert #347 (fix collisions when option and first arg have same name) which causes a bug in #367.
23
+
24
+ 2.7.0 / 2015-03-09
25
+ ==================
26
+
27
+ * Fix git-style bug when installed globally. Close #335 #349 @zhiyelee
28
+ * Fix collisions when option and first arg have same name. Close #346 #347 @tonylukasavage
29
+ * Add support for camelCase on `opts()`. Close #353 @nkzawa
30
+ * Add node.js 0.12 and io.js to travis.yml
31
+ * Allow RegEx options. #337 @palanik
32
+ * Fixes exit code when sub-command failing. Close #260 #332 @pirelenito
33
+ * git-style `bin` files in $PATH make sense. Close #196 #327 @zhiyelee
34
+
1
35
  2.6.0 / 2014-12-30
2
36
  ==================
3
37
 
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/Readme.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Commander.js
2
2
 
3
- [![Build Status](https://api.travis-ci.org/tj/commander.js.svg)](http://travis-ci.org/tj/commander.js)
3
+
4
+ [![Build Status](https://api.travis-ci.org/tj/commander.js.svg)](http://travis-ci.org/tj/commander.js)
4
5
  [![NPM Version](http://img.shields.io/npm/v/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
5
6
  [![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
7
+ [![Join the chat at https://gitter.im/tj/commander.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6
8
 
7
9
  The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander).
8
10
  [API documentation](http://tj.github.com/commander.js/)
@@ -29,14 +31,14 @@ program
29
31
  .version('0.0.1')
30
32
  .option('-p, --peppers', 'Add peppers')
31
33
  .option('-P, --pineapple', 'Add pineapple')
32
- .option('-b, --bbq', 'Add bbq sauce')
34
+ .option('-b, --bbq-sauce', 'Add bbq sauce')
33
35
  .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
34
36
  .parse(process.argv);
35
37
 
36
38
  console.log('you ordered a pizza with:');
37
39
  if (program.peppers) console.log(' - peppers');
38
40
  if (program.pineapple) console.log(' - pineapple');
39
- if (program.bbq) console.log(' - bbq');
41
+ if (program.bbqSauce) console.log(' - bbq');
40
42
  console.log(' - %s cheese', program.cheese);
41
43
  ```
42
44
 
@@ -86,6 +88,18 @@ console.log(' verbosity: %j', program.verbose);
86
88
  console.log(' args: %j', program.args);
87
89
  ```
88
90
 
91
+ ## Regular Expression
92
+ ```js
93
+ program
94
+ .version('0.0.1')
95
+ .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
96
+ .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
97
+ .parse(process.argv);
98
+
99
+ console.log(' size: %j', program.size);
100
+ console.log(' drink: %j', program.drink);
101
+ ```
102
+
89
103
  ## Variadic arguments
90
104
 
91
105
  The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to
@@ -118,6 +132,31 @@ program.parse(process.argv);
118
132
  An `Array` is used for the value of a variadic argument. This applies to `program.args` as well as the argument passed
119
133
  to your action as demonstrated above.
120
134
 
135
+ ## Specify the argument syntax
136
+
137
+ ```js
138
+ #!/usr/bin/env node
139
+
140
+ var program = require('../');
141
+
142
+ program
143
+ .version('0.0.1')
144
+ .arguments('<cmd> [env]')
145
+ .action(function (cmd, env) {
146
+ cmdValue = cmd;
147
+ envValue = env;
148
+ });
149
+
150
+ program.parse(process.argv);
151
+
152
+ if (typeof cmdValue === 'undefined') {
153
+ console.error('no command given!');
154
+ process.exit(1);
155
+ }
156
+ console.log('command:', cmdValue);
157
+ console.log('environment:', envValue || "no environment given");
158
+ ```
159
+
121
160
  ## Git-style sub-commands
122
161
 
123
162
  ```js
@@ -133,7 +172,15 @@ program
133
172
  ```
134
173
 
135
174
  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.
136
- The commander will try to find the executable script in __current directory__ with the name `scriptBasename-subcommand`, like `pm-install`, `pm-search`.
175
+ 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`.
176
+
177
+ If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
178
+
179
+ ### `--harmony`
180
+
181
+ You can enable `--harmony` option in two ways:
182
+ * Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. Note some os version don’t support this pattern.
183
+ * Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.
137
184
 
138
185
  ## Automated --help
139
186
 
@@ -221,7 +268,22 @@ Examples:
221
268
 
222
269
  ## .outputHelp()
223
270
 
224
- Output help information without exiting.
271
+ Output help information without exiting.
272
+
273
+ If you want to display help by default (e.g. if no command was provided), you can use something like:
274
+
275
+ ```js
276
+ var program = require('commander');
277
+
278
+ program
279
+ .version('0.0.1')
280
+ .command('getstream [url]', 'get stream URL')
281
+ .parse(process.argv);
282
+
283
+ if (!process.argv.slice(2).length) {
284
+ program.outputHelp();
285
+ }
286
+ ```
225
287
 
226
288
  ## .help()
227
289
 
@@ -272,29 +334,9 @@ program
272
334
  program.parse(process.argv);
273
335
  ```
274
336
 
275
- You can see more Demos in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.
337
+ More Demos can be found in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.
276
338
 
277
339
  ## License
278
340
 
279
- (The MIT License)
280
-
281
- Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
282
-
283
- Permission is hereby granted, free of charge, to any person obtaining
284
- a copy of this software and associated documentation files (the
285
- 'Software'), to deal in the Software without restriction, including
286
- without limitation the rights to use, copy, modify, merge, publish,
287
- distribute, sublicense, and/or sell copies of the Software, and to
288
- permit persons to whom the Software is furnished to do so, subject to
289
- the following conditions:
290
-
291
- The above copyright notice and this permission notice shall be
292
- included in all copies or substantial portions of the Software.
341
+ MIT
293
342
 
294
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
295
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
296
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
297
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
298
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
299
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
300
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/index.js CHANGED
@@ -5,9 +5,11 @@
5
5
 
6
6
  var EventEmitter = require('events').EventEmitter;
7
7
  var spawn = require('child_process').spawn;
8
+ var readlink = require('graceful-readlink').readlinkSync;
8
9
  var path = require('path');
9
10
  var dirname = path.dirname;
10
11
  var basename = path.basename;
12
+ var fs = require('fs');
11
13
 
12
14
  /**
13
15
  * Expose the root command.
@@ -154,7 +156,8 @@ Command.prototype.__proto__ = EventEmitter.prototype;
154
156
  * @api public
155
157
  */
156
158
 
157
- Command.prototype.command = function(name, desc) {
159
+ Command.prototype.command = function(name, desc, opts) {
160
+ opts = opts || {};
158
161
  var args = name.split(/ +/);
159
162
  var cmd = new Command(args.shift());
160
163
 
@@ -164,6 +167,7 @@ Command.prototype.command = function(name, desc) {
164
167
  this._execs[cmd._name] = true;
165
168
  }
166
169
 
170
+ cmd._noHelp = !!opts.noHelp;
167
171
  this.commands.push(cmd);
168
172
  cmd.parseExpectedArgs(args);
169
173
  cmd.parent = this;
@@ -172,6 +176,16 @@ Command.prototype.command = function(name, desc) {
172
176
  return cmd;
173
177
  };
174
178
 
179
+ /**
180
+ * Define argument syntax for the top-level command.
181
+ *
182
+ * @api public
183
+ */
184
+
185
+ Command.prototype.arguments = function (desc) {
186
+ return this.parseExpectedArgs(desc.split(/ +/));
187
+ }
188
+
175
189
  /**
176
190
  * Add an implicit `help [cmd]` subcommand
177
191
  * which invokes `--help` for the given command.
@@ -286,8 +300,10 @@ Command.prototype.action = function(fn) {
286
300
 
287
301
  fn.apply(self, args);
288
302
  };
289
- this.parent.on(this._name, listener);
290
- if (this._alias) this.parent.on(this._alias, listener);
303
+ var parent = this.parent || this;
304
+ var name = parent === this ? '*' : this._name;
305
+ parent.on(name, listener);
306
+ if (this._alias) parent.on(this._alias, listener);
291
307
  return this;
292
308
  };
293
309
 
@@ -348,8 +364,17 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
348
364
 
349
365
  // default as 3rd arg
350
366
  if (typeof fn != 'function') {
351
- defaultValue = fn;
352
- fn = null;
367
+ if (fn instanceof RegExp) {
368
+ var regex = fn;
369
+ fn = function(val, def) {
370
+ var m = regex.exec(val);
371
+ return m ? m[0] : def;
372
+ }
373
+ }
374
+ else {
375
+ defaultValue = fn;
376
+ fn = null;
377
+ }
353
378
  }
354
379
 
355
380
  // preassign default value only for --no-*, [optional], or <required>
@@ -420,6 +445,12 @@ Command.prototype.parse = function(argv) {
420
445
  // guess name
421
446
  this._name = this._name || basename(argv[1], '.js');
422
447
 
448
+ // github-style sub-commands with no sub-command
449
+ if (this.executables && argv.length < 3) {
450
+ // this user needs help
451
+ argv.push('--help');
452
+ }
453
+
423
454
  // process argv
424
455
  var parsed = this.parseOptions(this.normalize(argv.slice(2)));
425
456
  var args = this.args = parsed.args;
@@ -457,22 +488,60 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
457
488
  }
458
489
 
459
490
  // executable
460
- var dir = dirname(argv[1]);
461
- var bin = basename(argv[1], '.js') + '-' + args[0];
491
+ var f = argv[1];
492
+ // name of the subcommand, link `pm-install`
493
+ var bin = basename(f, '.js') + '-' + args[0];
494
+
495
+
496
+ // In case of globally installed, get the base dir where executable
497
+ // subcommand file should be located at
498
+ var baseDir
499
+ , link = readlink(f);
462
500
 
463
- // check for ./<bin> first
464
- var local = path.join(dir, bin);
501
+ // when symbolink is relative path
502
+ if (link !== f && link.charAt(0) !== '/') {
503
+ link = path.join(dirname(f), link)
504
+ }
505
+ baseDir = dirname(link);
506
+
507
+ // prefer local `./<bin>` to bin in the $PATH
508
+ var localBin = path.join(baseDir, bin);
509
+
510
+ // whether bin file is a js script with explicit `.js` extension
511
+ var isExplicitJS = false;
512
+ if (exists(localBin + '.js')) {
513
+ bin = localBin + '.js';
514
+ isExplicitJS = true;
515
+ } else if (exists(localBin)) {
516
+ bin = localBin;
517
+ }
465
518
 
466
- // run it
467
519
  args = args.slice(1);
468
- args.unshift(local);
469
- var proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
520
+
521
+ var proc;
522
+ if (process.platform !== 'win32') {
523
+ if (isExplicitJS) {
524
+ args.unshift(localBin);
525
+ // add executable arguments to spawn
526
+ args = (process.execArgv || []).concat(args);
527
+
528
+ proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
529
+ } else {
530
+ proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
531
+ }
532
+ } else {
533
+ args.unshift(localBin);
534
+ proc = spawn(process.execPath, args, { stdio: 'inherit'});
535
+ }
536
+
537
+ proc.on('close', process.exit.bind(process));
470
538
  proc.on('error', function(err) {
471
539
  if (err.code == "ENOENT") {
472
540
  console.error('\n %s(1) does not exist, try --help\n', bin);
473
541
  } else if (err.code == "EACCES") {
474
542
  console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
475
543
  }
544
+ process.exit(1);
476
545
  });
477
546
 
478
547
  this.runningCommand = proc;
@@ -661,7 +730,7 @@ Command.prototype.opts = function() {
661
730
  , len = this.options.length;
662
731
 
663
732
  for (var i = 0 ; i < len; i++) {
664
- var key = this.options[i].name();
733
+ var key = camelcase(this.options[i].name());
665
734
  result[key] = key === 'version' ? this._version : this[key];
666
735
  }
667
736
  return result;
@@ -708,7 +777,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
708
777
  */
709
778
 
710
779
  Command.prototype.unknownOption = function(flag) {
711
- if(this._allowUnknownOption) return;
780
+ if (this._allowUnknownOption) return;
712
781
  console.error();
713
782
  console.error(" error: unknown option `%s'", flag);
714
783
  console.error();
@@ -812,7 +881,7 @@ Command.prototype.usage = function(str) {
812
881
  * @api public
813
882
  */
814
883
 
815
- Command.prototype.name = function(name) {
884
+ Command.prototype.name = function() {
816
885
  return this._name;
817
886
  };
818
887
 
@@ -857,7 +926,9 @@ Command.prototype.optionHelp = function() {
857
926
  Command.prototype.commandHelp = function() {
858
927
  if (!this.commands.length) return '';
859
928
 
860
- var commands = this.commands.map(function(cmd) {
929
+ var commands = this.commands.filter(function(cmd) {
930
+ return !cmd._noHelp;
931
+ }).map(function(cmd) {
861
932
  var args = cmd._args.map(function(arg) {
862
933
  return humanReadableArgName(arg);
863
934
  }).join(' ');
@@ -907,7 +978,7 @@ Command.prototype.helpInformation = function() {
907
978
  }
908
979
 
909
980
  var cmdName = this._name;
910
- if(this._alias) {
981
+ if (this._alias) {
911
982
  cmdName = cmdName + '|' + this._alias;
912
983
  }
913
984
  var usage = [
@@ -1018,3 +1089,15 @@ function humanReadableArgName(arg) {
1018
1089
  ? '<' + nameOutput + '>'
1019
1090
  : '[' + nameOutput + ']'
1020
1091
  }
1092
+
1093
+ // for versions before node v0.8 when there weren't `fs.existsSync`
1094
+ function exists(file) {
1095
+ try {
1096
+ if (fs.statSync(file).isFile()) {
1097
+ return true;
1098
+ }
1099
+ } catch (e) {
1100
+ return false;
1101
+ }
1102
+ }
1103
+
package/package.json CHANGED
@@ -1,14 +1,33 @@
1
1
  {
2
- "name": "commander"
3
- , "version": "2.6.0"
4
- , "description": "the complete solution for node.js command-line programs"
5
- , "keywords": ["command", "option", "parser", "prompt"]
6
- , "author": "TJ Holowaychuk <tj@vision-media.ca>"
7
- , "license": "MIT"
8
- , "repository": { "type": "git", "url": "https://github.com/tj/commander.js.git" }
9
- , "devDependencies": { "should": ">= 0.0.1" }
10
- , "scripts": { "test": "make test" }
11
- , "main": "index"
12
- , "engines": { "node": ">= 0.6.x" }
13
- , "files": ["index.js"]
2
+ "name": "commander",
3
+ "version": "2.8.1",
4
+ "description": "the complete solution for node.js command-line programs",
5
+ "keywords": [
6
+ "command",
7
+ "option",
8
+ "parser"
9
+ ],
10
+ "author": "TJ Holowaychuk <tj@vision-media.ca>",
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/tj/commander.js.git"
15
+ },
16
+ "devDependencies": {
17
+ "should": ">= 0.0.1",
18
+ "sinon": ">= 1.14.1"
19
+ },
20
+ "scripts": {
21
+ "test": "make test"
22
+ },
23
+ "main": "index",
24
+ "engines": {
25
+ "node": ">= 0.6.x"
26
+ },
27
+ "files": [
28
+ "index.js"
29
+ ],
30
+ "dependencies": {
31
+ "graceful-readlink": ">= 1.0.0"
32
+ }
14
33
  }