commander 2.5.1 → 2.6.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 (4) hide show
  1. package/History.md +222 -0
  2. package/Readme.md +94 -50
  3. package/index.js +45 -10
  4. package/package.json +1 -1
package/History.md ADDED
@@ -0,0 +1,222 @@
1
+ 2.6.0 / 2014-12-30
2
+ ==================
3
+
4
+ * added `Command#allowUnknownOption` method. Close #138 #318 @doozr @zhiyelee
5
+ * Add application description to the help msg. Close #112 @dalssoft
6
+
7
+ 2.5.1 / 2014-12-15
8
+ ==================
9
+
10
+ * fixed two bugs incurred by variadic arguments. Close #291 @Quentin01 #302 @zhiyelee
11
+
12
+ 2.5.0 / 2014-10-24
13
+ ==================
14
+
15
+ * add support for variadic arguments. Closes #277 @whitlockjc
16
+
17
+ 2.4.0 / 2014-10-17
18
+ ==================
19
+
20
+ * fixed a bug on executing the coercion function of subcommands option. Closes #270
21
+ * added `Command.prototype.name` to retrieve command name. Closes #264 #266 @tonylukasavage
22
+ * added `Command.prototype.opts` to retrieve all the options as a simple object of key-value pairs. Closes #262 @tonylukasavage
23
+ * fixed a bug on subcommand name. Closes #248 @jonathandelgado
24
+ * fixed function normalize doesn’t honor option terminator. Closes #216 @abbr
25
+
26
+ 2.3.0 / 2014-07-16
27
+ ==================
28
+
29
+ * add command alias'. Closes PR #210
30
+ * fix: Typos. Closes #99
31
+ * fix: Unused fs module. Closes #217
32
+
33
+ 2.2.0 / 2014-03-29
34
+ ==================
35
+
36
+ * add passing of previous option value
37
+ * fix: support subcommands on windows. Closes #142
38
+ * Now the defaultValue passed as the second argument of the coercion function.
39
+
40
+ 2.1.0 / 2013-11-21
41
+ ==================
42
+
43
+ * add: allow cflag style option params, unit test, fixes #174
44
+
45
+ 2.0.0 / 2013-07-18
46
+ ==================
47
+
48
+ * remove input methods (.prompt, .confirm, etc)
49
+
50
+ 1.3.2 / 2013-07-18
51
+ ==================
52
+
53
+ * add support for sub-commands to co-exist with the original command
54
+
55
+ 1.3.1 / 2013-07-18
56
+ ==================
57
+
58
+ * add quick .runningCommand hack so you can opt-out of other logic when running a sub command
59
+
60
+ 1.3.0 / 2013-07-09
61
+ ==================
62
+
63
+ * add EACCES error handling
64
+ * fix sub-command --help
65
+
66
+ 1.2.0 / 2013-06-13
67
+ ==================
68
+
69
+ * allow "-" hyphen as an option argument
70
+ * support for RegExp coercion
71
+
72
+ 1.1.1 / 2012-11-20
73
+ ==================
74
+
75
+ * add more sub-command padding
76
+ * fix .usage() when args are present. Closes #106
77
+
78
+ 1.1.0 / 2012-11-16
79
+ ==================
80
+
81
+ * add git-style executable subcommand support. Closes #94
82
+
83
+ 1.0.5 / 2012-10-09
84
+ ==================
85
+
86
+ * fix `--name` clobbering. Closes #92
87
+ * fix examples/help. Closes #89
88
+
89
+ 1.0.4 / 2012-09-03
90
+ ==================
91
+
92
+ * add `outputHelp()` method.
93
+
94
+ 1.0.3 / 2012-08-30
95
+ ==================
96
+
97
+ * remove invalid .version() defaulting
98
+
99
+ 1.0.2 / 2012-08-24
100
+ ==================
101
+
102
+ * add `--foo=bar` support [arv]
103
+ * fix password on node 0.8.8. Make backward compatible with 0.6 [focusaurus]
104
+
105
+ 1.0.1 / 2012-08-03
106
+ ==================
107
+
108
+ * fix issue #56
109
+ * fix tty.setRawMode(mode) was moved to tty.ReadStream#setRawMode() (i.e. process.stdin.setRawMode())
110
+
111
+ 1.0.0 / 2012-07-05
112
+ ==================
113
+
114
+ * add support for optional option descriptions
115
+ * add defaulting of `.version()` to package.json's version
116
+
117
+ 0.6.1 / 2012-06-01
118
+ ==================
119
+
120
+ * Added: append (yes or no) on confirmation
121
+ * Added: allow node.js v0.7.x
122
+
123
+ 0.6.0 / 2012-04-10
124
+ ==================
125
+
126
+ * Added `.prompt(obj, callback)` support. Closes #49
127
+ * Added default support to .choose(). Closes #41
128
+ * Fixed the choice example
129
+
130
+ 0.5.1 / 2011-12-20
131
+ ==================
132
+
133
+ * Fixed `password()` for recent nodes. Closes #36
134
+
135
+ 0.5.0 / 2011-12-04
136
+ ==================
137
+
138
+ * Added sub-command option support [itay]
139
+
140
+ 0.4.3 / 2011-12-04
141
+ ==================
142
+
143
+ * Fixed custom help ordering. Closes #32
144
+
145
+ 0.4.2 / 2011-11-24
146
+ ==================
147
+
148
+ * Added travis support
149
+ * Fixed: line-buffered input automatically trimmed. Closes #31
150
+
151
+ 0.4.1 / 2011-11-18
152
+ ==================
153
+
154
+ * Removed listening for "close" on --help
155
+
156
+ 0.4.0 / 2011-11-15
157
+ ==================
158
+
159
+ * Added support for `--`. Closes #24
160
+
161
+ 0.3.3 / 2011-11-14
162
+ ==================
163
+
164
+ * Fixed: wait for close event when writing help info [Jerry Hamlet]
165
+
166
+ 0.3.2 / 2011-11-01
167
+ ==================
168
+
169
+ * Fixed long flag definitions with values [felixge]
170
+
171
+ 0.3.1 / 2011-10-31
172
+ ==================
173
+
174
+ * Changed `--version` short flag to `-V` from `-v`
175
+ * Changed `.version()` so it's configurable [felixge]
176
+
177
+ 0.3.0 / 2011-10-31
178
+ ==================
179
+
180
+ * Added support for long flags only. Closes #18
181
+
182
+ 0.2.1 / 2011-10-24
183
+ ==================
184
+
185
+ * "node": ">= 0.4.x < 0.7.0". Closes #20
186
+
187
+ 0.2.0 / 2011-09-26
188
+ ==================
189
+
190
+ * Allow for defaults that are not just boolean. Default peassignment only occurs for --no-*, optional, and required arguments. [Jim Isaacs]
191
+
192
+ 0.1.0 / 2011-08-24
193
+ ==================
194
+
195
+ * Added support for custom `--help` output
196
+
197
+ 0.0.5 / 2011-08-18
198
+ ==================
199
+
200
+ * Changed: when the user enters nothing prompt for password again
201
+ * Fixed issue with passwords beginning with numbers [NuckChorris]
202
+
203
+ 0.0.4 / 2011-08-15
204
+ ==================
205
+
206
+ * Fixed `Commander#args`
207
+
208
+ 0.0.3 / 2011-08-15
209
+ ==================
210
+
211
+ * Added default option value support
212
+
213
+ 0.0.2 / 2011-08-15
214
+ ==================
215
+
216
+ * Added mask support to `Command#password(str[, mask], fn)`
217
+ * Added `Command#password(str, fn)`
218
+
219
+ 0.0.1 / 2010-01-03
220
+ ==================
221
+
222
+ * Initial release
package/Readme.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
6
6
 
7
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
+ [API documentation](http://tj.github.com/commander.js/)
9
9
 
10
10
 
11
11
  ## Installation
@@ -42,6 +42,50 @@ console.log(' - %s cheese', program.cheese);
42
42
 
43
43
  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.
44
44
 
45
+
46
+ ## Coercion
47
+
48
+ ```js
49
+ function range(val) {
50
+ return val.split('..').map(Number);
51
+ }
52
+
53
+ function list(val) {
54
+ return val.split(',');
55
+ }
56
+
57
+ function collect(val, memo) {
58
+ memo.push(val);
59
+ return memo;
60
+ }
61
+
62
+ function increaseVerbosity(v, total) {
63
+ return total + 1;
64
+ }
65
+
66
+ program
67
+ .version('0.0.1')
68
+ .usage('[options] <file ...>')
69
+ .option('-i, --integer <n>', 'An integer argument', parseInt)
70
+ .option('-f, --float <n>', 'A float argument', parseFloat)
71
+ .option('-r, --range <a>..<b>', 'A range', range)
72
+ .option('-l, --list <items>', 'A list', list)
73
+ .option('-o, --optional [value]', 'An optional value')
74
+ .option('-c, --collect [value]', 'A repeatable value', collect, [])
75
+ .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
76
+ .parse(process.argv);
77
+
78
+ console.log(' int: %j', program.integer);
79
+ console.log(' float: %j', program.float);
80
+ console.log(' optional: %j', program.optional);
81
+ program.range = program.range || [];
82
+ console.log(' range: %j..%j', program.range[0], program.range[1]);
83
+ console.log(' list: %j', program.list);
84
+ console.log(' collect: %j', program.collect);
85
+ console.log(' verbosity: %j', program.verbose);
86
+ console.log(' args: %j', program.args);
87
+ ```
88
+
45
89
  ## Variadic arguments
46
90
 
47
91
  The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to
@@ -100,58 +144,18 @@ The commander will try to find the executable script in __current directory__ wi
100
144
 
101
145
  Usage: pizza [options]
102
146
 
147
+ An application for pizzas ordering
148
+
103
149
  Options:
104
150
 
151
+ -h, --help output usage information
105
152
  -V, --version output the version number
106
153
  -p, --peppers Add peppers
107
154
  -P, --pineapple Add pineapple
108
155
  -b, --bbq Add bbq sauce
109
156
  -c, --cheese <type> Add the specified type of cheese [marble]
110
- -h, --help output usage information
111
-
112
- ```
113
-
114
- ## Coercion
115
-
116
- ```js
117
- function range(val) {
118
- return val.split('..').map(Number);
119
- }
120
-
121
- function list(val) {
122
- return val.split(',');
123
- }
124
-
125
- function collect(val, memo) {
126
- memo.push(val);
127
- return memo;
128
- }
129
-
130
- function increaseVerbosity(v, total) {
131
- return total + 1;
132
- }
133
-
134
- program
135
- .version('0.0.1')
136
- .usage('[options] <file ...>')
137
- .option('-i, --integer <n>', 'An integer argument', parseInt)
138
- .option('-f, --float <n>', 'A float argument', parseFloat)
139
- .option('-r, --range <a>..<b>', 'A range', range)
140
- .option('-l, --list <items>', 'A list', list)
141
- .option('-o, --optional [value]', 'An optional value')
142
- .option('-c, --collect [value]', 'A repeatable value', collect, [])
143
- .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
144
- .parse(process.argv);
157
+ -C, --no-cheese You do not want any cheese
145
158
 
146
- console.log(' int: %j', program.integer);
147
- console.log(' float: %j', program.float);
148
- console.log(' optional: %j', program.optional);
149
- program.range = program.range || [];
150
- console.log(' range: %j..%j', program.range[0], program.range[1]);
151
- console.log(' list: %j', program.list);
152
- console.log(' collect: %j', program.collect);
153
- console.log(' verbosity: %j', program.verbose);
154
- console.log(' args: %j', program.args);
155
159
  ```
156
160
 
157
161
  ## Custom help
@@ -223,12 +227,52 @@ Examples:
223
227
 
224
228
  Output help information and exit immediately.
225
229
 
226
- ## Links
230
+ ## Examples
231
+
232
+ ```js
233
+ var program = require('commander');
234
+
235
+ program
236
+ .version('0.0.1')
237
+ .option('-C, --chdir <path>', 'change the working directory')
238
+ .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
239
+ .option('-T, --no-tests', 'ignore test hook')
240
+
241
+ program
242
+ .command('setup [env]')
243
+ .description('run setup commands for all envs')
244
+ .option("-s, --setup_mode [mode]", "Which setup mode to use")
245
+ .action(function(env, options){
246
+ var mode = options.setup_mode || "normal";
247
+ env = env || 'all';
248
+ console.log('setup for %s env(s) with %s mode', env, mode);
249
+ });
250
+
251
+ program
252
+ .command('exec <cmd>')
253
+ .alias('ex')
254
+ .description('execute the given remote cmd')
255
+ .option("-e, --exec_mode <mode>", "Which exec mode to use")
256
+ .action(function(cmd, options){
257
+ console.log('exec "%s" using %s mode', cmd, options.exec_mode);
258
+ }).on('--help', function() {
259
+ console.log(' Examples:');
260
+ console.log();
261
+ console.log(' $ deploy exec sequential');
262
+ console.log(' $ deploy exec async');
263
+ console.log();
264
+ });
265
+
266
+ program
267
+ .command('*')
268
+ .action(function(env){
269
+ console.log('deploying "%s"', env);
270
+ });
271
+
272
+ program.parse(process.argv);
273
+ ```
227
274
 
228
- - [ascii tables](https://github.com/LearnBoost/cli-table)
229
- - [progress bars](https://github.com/tj/node-progress)
230
- - [more progress bars](https://github.com/substack/node-multimeter)
231
- - [examples](https://github.com/tj/commander.js/tree/master/examples)
275
+ You can see more Demos in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.
232
276
 
233
277
  ## License
234
278
 
package/index.js CHANGED
@@ -82,6 +82,7 @@ function Command(name) {
82
82
  this.commands = [];
83
83
  this.options = [];
84
84
  this._execs = [];
85
+ this._allowUnknownOption = false;
85
86
  this._args = [];
86
87
  this._name = name;
87
88
  }
@@ -389,6 +390,18 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
389
390
  return this;
390
391
  };
391
392
 
393
+ /**
394
+ * Allow unknown options on the command line.
395
+ *
396
+ * @param {Boolean} arg if `true` or omitted, no error will be thrown
397
+ * for unknown options.
398
+ * @api public
399
+ */
400
+ Command.prototype.allowUnknownOption = function(arg) {
401
+ this._allowUnknownOption = arguments.length === 0 || arg;
402
+ return this;
403
+ };
404
+
392
405
  /**
393
406
  * Parse `argv`, settings options and invoking commands when defined.
394
407
  *
@@ -695,6 +708,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
695
708
  */
696
709
 
697
710
  Command.prototype.unknownOption = function(flag) {
711
+ if(this._allowUnknownOption) return;
698
712
  console.error();
699
713
  console.error(" error: unknown option `%s'", flag);
700
714
  console.error();
@@ -884,20 +898,41 @@ Command.prototype.commandHelp = function() {
884
898
  */
885
899
 
886
900
  Command.prototype.helpInformation = function() {
887
- return [
888
- ''
889
- , ' Usage: ' + this._name
890
- + (this._alias
891
- ? '|' + this._alias
892
- : '')
893
- + ' ' + this.usage()
894
- , '' + this.commandHelp()
895
- , ' Options:'
901
+ var desc = [];
902
+ if (this._description) {
903
+ desc = [
904
+ ' ' + this._description
905
+ , ''
906
+ ];
907
+ }
908
+
909
+ var cmdName = this._name;
910
+ if(this._alias) {
911
+ cmdName = cmdName + '|' + this._alias;
912
+ }
913
+ var usage = [
914
+ ''
915
+ ,' Usage: ' + cmdName + ' ' + this.usage()
916
+ , ''
917
+ ];
918
+
919
+ var cmds = [];
920
+ var commandHelp = this.commandHelp();
921
+ if (commandHelp) cmds = [commandHelp];
922
+
923
+ var options = [
924
+ ' Options:'
896
925
  , ''
897
926
  , '' + this.optionHelp().replace(/^/gm, ' ')
898
927
  , ''
899
928
  , ''
900
- ].join('\n');
929
+ ];
930
+
931
+ return usage
932
+ .concat(cmds)
933
+ .concat(desc)
934
+ .concat(options)
935
+ .join('\n');
901
936
  };
902
937
 
903
938
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander"
3
- , "version": "2.5.1"
3
+ , "version": "2.6.0"
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>"