commander 2.1.0 → 2.2.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.
Files changed (3) hide show
  1. package/Readme.md +13 -0
  2. package/index.js +29 -28
  3. package/package.json +1 -1
package/Readme.md CHANGED
@@ -69,6 +69,15 @@ function list(val) {
69
69
  return val.split(',');
70
70
  }
71
71
 
72
+ function collect(val, memo) {
73
+ memo.push(val);
74
+ return memo;
75
+ }
76
+
77
+ function increaseVerbosity(v, total) {
78
+ return total + 1;
79
+ }
80
+
72
81
  program
73
82
  .version('0.0.1')
74
83
  .usage('[options] <file ...>')
@@ -77,6 +86,8 @@ program
77
86
  .option('-r, --range <a>..<b>', 'A range', range)
78
87
  .option('-l, --list <items>', 'A list', list)
79
88
  .option('-o, --optional [value]', 'An optional value')
89
+ .option('-c, --collect [value]', 'A repeatable value', [])
90
+ .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
80
91
  .parse(process.argv);
81
92
 
82
93
  console.log(' int: %j', program.integer);
@@ -85,6 +96,8 @@ console.log(' optional: %j', program.optional);
85
96
  program.range = program.range || [];
86
97
  console.log(' range: %j..%j', program.range[0], program.range[1]);
87
98
  console.log(' list: %j', program.list);
99
+ console.log(' collect: %j', program.collect);
100
+ console.log(' verbosity: %j', program.verbose);
88
101
  console.log(' args: %j', program.args);
89
102
  ```
90
103
 
package/index.js CHANGED
@@ -114,28 +114,28 @@ Command.prototype.__proto__ = EventEmitter.prototype;
114
114
  * .option('-C, --chdir <path>', 'change the working directory')
115
115
  * .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
116
116
  * .option('-T, --no-tests', 'ignore test hook')
117
- *
117
+ *
118
118
  * program
119
119
  * .command('setup')
120
120
  * .description('run remote setup commands')
121
121
  * .action(function(){
122
122
  * console.log('setup');
123
123
  * });
124
- *
124
+ *
125
125
  * program
126
126
  * .command('exec <cmd>')
127
127
  * .description('run the given remote command')
128
128
  * .action(function(cmd){
129
129
  * console.log('exec "%s"', cmd);
130
130
  * });
131
- *
131
+ *
132
132
  * program
133
133
  * .command('*')
134
134
  * .description('deploy the given env')
135
135
  * .action(function(env){
136
136
  * console.log('deploying "%s"', env);
137
137
  * });
138
- *
138
+ *
139
139
  * program.parse(process.argv);
140
140
  *
141
141
  * @param {String} name
@@ -213,30 +213,30 @@ Command.prototype.parseExpectedArgs = function(args){
213
213
 
214
214
  Command.prototype.action = function(fn){
215
215
  var self = this;
216
- this.parent.on(this._name, function(args, unknown){
216
+ this.parent.on(this._name, function(args, unknown){
217
217
  // Parse any so-far unknown options
218
218
  unknown = unknown || [];
219
219
  var parsed = self.parseOptions(unknown);
220
-
220
+
221
221
  // Output help if necessary
222
222
  outputHelpIfNecessary(self, parsed.unknown);
223
-
224
- // If there are still any unknown options, then we simply
223
+
224
+ // If there are still any unknown options, then we simply
225
225
  // die, unless someone asked for help, in which case we give it
226
226
  // to them, and then we die.
227
- if (parsed.unknown.length > 0) {
227
+ if (parsed.unknown.length > 0) {
228
228
  self.unknownOption(parsed.unknown[0]);
229
229
  }
230
-
230
+
231
231
  // Leftover arguments need to be pushed back. Fixes issue #56
232
232
  if (parsed.args.length) args = parsed.args.concat(args);
233
-
233
+
234
234
  self._args.forEach(function(arg, i){
235
235
  if (arg.required && null == args[i]) {
236
236
  self.missingArgument(arg.name);
237
237
  }
238
238
  });
239
-
239
+
240
240
  // Always append ourselves to the end of the arguments,
241
241
  // to make sure we match the number of arguments the user
242
242
  // expects
@@ -245,7 +245,7 @@ Command.prototype.action = function(fn){
245
245
  } else {
246
246
  args.push(self);
247
247
  }
248
-
248
+
249
249
  fn.apply(this, args);
250
250
  });
251
251
  return this;
@@ -253,7 +253,7 @@ Command.prototype.action = function(fn){
253
253
 
254
254
  /**
255
255
  * Define option with `flags`, `description` and optional
256
- * coercion `fn`.
256
+ * coercion `fn`.
257
257
  *
258
258
  * The `flags` string should contain both the short and long flags,
259
259
  * separated by comma, a pipe or space. The following are all valid
@@ -324,7 +324,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue){
324
324
  // and conditionally invoke the callback
325
325
  this.on(oname, function(val){
326
326
  // coercion
327
- if (null != val && fn) val = fn(val);
327
+ if (null !== val && fn) val = fn(val, undefined === self[name] ? defaultValue : self[name]);
328
328
 
329
329
  // unassigned or bool
330
330
  if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) {
@@ -361,12 +361,12 @@ Command.prototype.parse = function(argv){
361
361
  this.rawArgs = argv;
362
362
 
363
363
  // guess name
364
- this._name = this._name || basename(argv[1]);
364
+ this._name = this._name || basename(argv[1], '.js');
365
365
 
366
366
  // process argv
367
367
  var parsed = this.parseOptions(this.normalize(argv.slice(2)));
368
368
  var args = this.args = parsed.args;
369
-
369
+
370
370
  var result = this.parseArgs(this.args, parsed.unknown);
371
371
 
372
372
  // executable sub-commands
@@ -399,14 +399,15 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
399
399
 
400
400
  // executable
401
401
  var dir = dirname(argv[1]);
402
- var bin = basename(argv[1]) + '-' + args[0];
402
+ var bin = basename(argv[1], '.js') + '-' + args[0];
403
403
 
404
404
  // check for ./<bin> first
405
405
  var local = path.join(dir, bin);
406
406
 
407
407
  // run it
408
408
  args = args.slice(1);
409
- var proc = spawn(local, args, { stdio: 'inherit', customFds: [0, 1, 2] });
409
+ args.unshift(local);
410
+ var proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
410
411
  proc.on('error', function(err){
411
412
  if (err.code == "ENOENT") {
412
413
  console.error('\n %s(1) does not exist, try --help\n', bin);
@@ -437,7 +438,7 @@ Command.prototype.normalize = function(args){
437
438
  for (var i = 0, len = args.length; i < len; ++i) {
438
439
  arg = args[i];
439
440
  i > 0 && (lastOpt = this.optionFor(args[i-1]));
440
-
441
+
441
442
  if (lastOpt && lastOpt.required) {
442
443
  ret.push(arg);
443
444
  } else if (arg.length > 1 && '-' == arg[0] && '-' != arg[1]) {
@@ -480,10 +481,10 @@ Command.prototype.parseArgs = function(args, unknown){
480
481
  }
481
482
  } else {
482
483
  outputHelpIfNecessary(this, unknown);
483
-
484
+
484
485
  // If there were no args and we have unknown options,
485
486
  // then they are extraneous and we need to error.
486
- if (unknown.length > 0) {
487
+ if (unknown.length > 0) {
487
488
  this.unknownOption(unknown[0]);
488
489
  }
489
490
  }
@@ -565,11 +566,11 @@ Command.prototype.parseOptions = function(argv){
565
566
  }
566
567
  continue;
567
568
  }
568
-
569
+
569
570
  // looks like an option
570
571
  if (arg.length > 1 && '-' == arg[0]) {
571
572
  unknownOptions.push(arg);
572
-
573
+
573
574
  // If the next argument looks like it might be
574
575
  // an argument for this option, we pass it on.
575
576
  // If it isn't, then it'll simply be ignored
@@ -578,11 +579,11 @@ Command.prototype.parseOptions = function(argv){
578
579
  }
579
580
  continue;
580
581
  }
581
-
582
+
582
583
  // arg
583
584
  args.push(arg);
584
585
  }
585
-
586
+
586
587
  return { args: args, unknown: unknownOptions };
587
588
  };
588
589
 
@@ -720,7 +721,7 @@ Command.prototype.largestOptionLength = function(){
720
721
 
721
722
  Command.prototype.optionHelp = function(){
722
723
  var width = this.largestOptionLength();
723
-
724
+
724
725
  // Prepend the help information
725
726
  return [pad('-h, --help', width) + ' ' + 'output usage information']
726
727
  .concat(this.options.map(function(option){
@@ -751,7 +752,7 @@ Command.prototype.commandHelp = function(){
751
752
  }).join(' ');
752
753
 
753
754
  return pad(cmd._name
754
- + (cmd.options.length
755
+ + (cmd.options.length
755
756
  ? ' [options]'
756
757
  : '') + ' ' + args, 22)
757
758
  + (cmd.description()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander"
3
- , "version": "2.1.0"
3
+ , "version": "2.2.0"
4
4
  , "description": "the complete solution for node.js command-line programs"
5
5
  , "keywords": ["command", "option", "parser", "prompt", "stdin"]
6
6
  , "author": "TJ Holowaychuk <tj@vision-media.ca>"