commander 2.5.0 → 2.5.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 (3) hide show
  1. package/Readme.md +21 -4
  2. package/index.js +11 -7
  3. package/package.json +1 -1
package/Readme.md CHANGED
@@ -4,7 +4,8 @@
4
4
  [![NPM Version](http://img.shields.io/npm/v/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
5
5
  [![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
6
6
 
7
- The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander).
7
+ The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander).
8
+ API documentation: [http://tj.github.com/commander.js/](http://tj.github.com/commander.js/)
8
9
 
9
10
 
10
11
  ## Installation
@@ -73,6 +74,23 @@ program.parse(process.argv);
73
74
  An `Array` is used for the value of a variadic argument. This applies to `program.args` as well as the argument passed
74
75
  to your action as demonstrated above.
75
76
 
77
+ ## Git-style sub-commands
78
+
79
+ ```js
80
+ // file: ./examples/pm
81
+ var program = require('..');
82
+
83
+ program
84
+ .version('0.0.1')
85
+ .command('install [name]', 'install one or more packages')
86
+ .command('search [query]', 'search with optional query')
87
+ .command('list', 'list packages installed')
88
+ .parse(process.argv);
89
+ ```
90
+
91
+ 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.
92
+ The commander will try to find the executable script in __current directory__ with the name `scriptBasename-subcommand`, like `pm-install`, `pm-search`.
93
+
76
94
  ## Automated --help
77
95
 
78
96
  The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
@@ -152,7 +170,7 @@ console.log(' args: %j', program.args);
152
170
  * Module dependencies.
153
171
  */
154
172
 
155
- var program = require('../');
173
+ var program = require('commander');
156
174
 
157
175
  program
158
176
  .version('0.0.1')
@@ -176,7 +194,7 @@ program.parse(process.argv);
176
194
  console.log('stuff');
177
195
  ```
178
196
 
179
- yielding the following help output:
197
+ Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:
180
198
 
181
199
  ```
182
200
 
@@ -207,7 +225,6 @@ Examples:
207
225
 
208
226
  ## Links
209
227
 
210
- - [API documentation](http://tj.github.com/commander.js/)
211
228
  - [ascii tables](https://github.com/LearnBoost/cli-table)
212
229
  - [progress bars](https://github.com/tj/node-progress)
213
230
  - [more progress bars](https://github.com/substack/node-multimeter)
package/index.js CHANGED
@@ -148,7 +148,7 @@ Command.prototype.__proto__ = EventEmitter.prototype;
148
148
  * program.parse(process.argv);
149
149
  *
150
150
  * @param {String} name
151
- * @param {String} [desc]
151
+ * @param {String} [desc] for git-style sub-commands
152
152
  * @return {Command} the new command
153
153
  * @api public
154
154
  */
@@ -156,12 +156,17 @@ Command.prototype.__proto__ = EventEmitter.prototype;
156
156
  Command.prototype.command = function(name, desc) {
157
157
  var args = name.split(/ +/);
158
158
  var cmd = new Command(args.shift());
159
- if (desc) cmd.description(desc);
160
- if (desc) this.executables = true;
161
- if (desc) this._execs[cmd._name] = true;
159
+
160
+ if (desc) {
161
+ cmd.description(desc);
162
+ this.executables = true;
163
+ this._execs[cmd._name] = true;
164
+ }
165
+
162
166
  this.commands.push(cmd);
163
167
  cmd.parseExpectedArgs(args);
164
168
  cmd.parent = this;
169
+
165
170
  if (desc) return this;
166
171
  return cmd;
167
172
  };
@@ -207,11 +212,10 @@ Command.prototype.parseExpectedArgs = function(args) {
207
212
  break;
208
213
  }
209
214
 
210
- if (argDetails.name.indexOf('...') === argDetails.name.length - 3) {
215
+ if (argDetails.name.length > 3 && argDetails.name.slice(-3) === '...') {
211
216
  argDetails.variadic = true;
212
217
  argDetails.name = argDetails.name.slice(0, -3);
213
218
  }
214
-
215
219
  if (argDetails.name) {
216
220
  self._args.push(argDetails);
217
221
  }
@@ -266,7 +270,7 @@ Command.prototype.action = function(fn) {
266
270
  self.variadicArgNotLast(arg.name);
267
271
  }
268
272
 
269
- args[i] = args.slice(i);
273
+ args[i] = args.splice(i);
270
274
  }
271
275
  });
272
276
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander"
3
- , "version": "2.5.0"
3
+ , "version": "2.5.1"
4
4
  , "description": "the complete solution for node.js command-line programs"
5
5
  , "keywords": ["command", "option", "parser", "prompt"]
6
6
  , "author": "TJ Holowaychuk <tj@vision-media.ca>"