commander 2.11.0 → 2.13.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.
@@ -1,4 +1,40 @@
1
1
 
2
+ 2.13.0 / 2018-01-09
3
+ ==================
4
+
5
+ * Do not print default for --no-
6
+ * remove trailing spaces in command help
7
+ * Update CI's Node.js to LTS and latest version
8
+ * typedefs: Command and Option types added to commander namespace
9
+
10
+ 2.12.2 / 2017-11-28
11
+ ==================
12
+
13
+ * fix: typings are not shipped
14
+
15
+ 2.12.1 / 2017-11-23
16
+ ==================
17
+
18
+ * Move @types/node to dev dependency
19
+
20
+ 2.12.0 / 2017-11-22
21
+ ==================
22
+
23
+ * add attributeName() method to Option objects
24
+ * Documentation updated for options with --no prefix
25
+ * typings: `outputHelp` takes a string as the first parameter
26
+ * typings: use overloads
27
+ * feat(typings): update to match js api
28
+ * Print default value in option help
29
+ * Fix translation error
30
+ * Fail when using same command and alias (#491)
31
+ * feat(typings): add help callback
32
+ * fix bug when description is add after command with options (#662)
33
+ * Format js code
34
+ * Rename History.md to CHANGELOG.md (#668)
35
+ * feat(typings): add typings to support TypeScript (#646)
36
+ * use current node
37
+
2
38
  2.11.0 / 2017-07-03
3
39
  ==================
4
40
 
package/Readme.md CHANGED
@@ -44,6 +44,25 @@ console.log(' - %s cheese', program.cheese);
44
44
 
45
45
  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.
46
46
 
47
+ 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
+
49
+ ```js
50
+ #!/usr/bin/env node
51
+
52
+ /**
53
+ * Module dependencies.
54
+ */
55
+
56
+ var program = require('commander');
57
+
58
+ program
59
+ .option('--no-sauce', 'Remove sauce')
60
+ .parse(process.argv);
61
+
62
+ console.log('you ordered a pizza');
63
+ if (program.sauce) console.log(' with sauce');
64
+ else console.log(' without sauce');
65
+ ```
47
66
 
48
67
  ## Coercion
49
68
 
@@ -285,9 +304,9 @@ program
285
304
  .command('getstream [url]', 'get stream URL')
286
305
  .parse(process.argv);
287
306
 
288
- if (!process.argv.slice(2).length) {
289
- program.outputHelp(make_red);
290
- }
307
+ if (!process.argv.slice(2).length) {
308
+ program.outputHelp(make_red);
309
+ }
291
310
 
292
311
  function make_red(txt) {
293
312
  return colors.red(txt); //display the help text in red on the console
package/index.js CHANGED
@@ -59,6 +59,18 @@ Option.prototype.name = function() {
59
59
  .replace('no-', '');
60
60
  };
61
61
 
62
+ /**
63
+ * Return option name, in a camelcase format that can be used
64
+ * as a object attribute key.
65
+ *
66
+ * @return {String}
67
+ * @api private
68
+ */
69
+
70
+ Option.prototype.attributeName = function() {
71
+ return camelcase( this.name() );
72
+ };
73
+
62
74
  /**
63
75
  * Check if `arg` matches the short or long flag.
64
76
  *
@@ -155,6 +167,10 @@ Command.prototype.__proto__ = EventEmitter.prototype;
155
167
  */
156
168
 
157
169
  Command.prototype.command = function(name, desc, opts) {
170
+ if(typeof desc === 'object' && desc !== null){
171
+ opts = desc;
172
+ desc = null;
173
+ }
158
174
  opts = opts || {};
159
175
  var args = name.split(/ +/);
160
176
  var cmd = new Command(args.shift());
@@ -165,7 +181,6 @@ Command.prototype.command = function(name, desc, opts) {
165
181
  this._execs[cmd._name] = true;
166
182
  if (opts.isDefault) this.defaultExecutable = cmd._name;
167
183
  }
168
-
169
184
  cmd._noHelp = !!opts.noHelp;
170
185
  this.commands.push(cmd);
171
186
  cmd.parseExpectedArgs(args);
@@ -359,7 +374,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
359
374
  var self = this
360
375
  , option = new Option(flags, description)
361
376
  , oname = option.name()
362
- , name = camelcase(oname);
377
+ , name = option.attributeName();
363
378
 
364
379
  // default as 3rd arg
365
380
  if (typeof fn != 'function') {
@@ -381,7 +396,10 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
381
396
  // when --no-* we make sure default is true
382
397
  if (false == option.bool) defaultValue = true;
383
398
  // preassign only if we have a default
384
- if (undefined !== defaultValue) self[name] = defaultValue;
399
+ if (undefined !== defaultValue) {
400
+ self[name] = defaultValue;
401
+ option.defaultValue = defaultValue;
402
+ }
385
403
  }
386
404
 
387
405
  // register the option
@@ -541,7 +559,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) {
541
559
  // add executable arguments to spawn
542
560
  args = (process.execArgv || []).concat(args);
543
561
 
544
- proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
562
+ proc = spawn(process.argv[0], args, { stdio: 'inherit', customFds: [0, 1, 2] });
545
563
  } else {
546
564
  proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
547
565
  }
@@ -755,7 +773,7 @@ Command.prototype.opts = function() {
755
773
  , len = this.options.length;
756
774
 
757
775
  for (var i = 0 ; i < len; i++) {
758
- var key = camelcase(this.options[i].name());
776
+ var key = this.options[i].attributeName();
759
777
  result[key] = key === 'version' ? this._version : this[key];
760
778
  }
761
779
  return result;
@@ -877,6 +895,8 @@ Command.prototype.alias = function(alias) {
877
895
 
878
896
  if (arguments.length === 0) return command._alias;
879
897
 
898
+ if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
899
+
880
900
  command._alias = alias;
881
901
  return this;
882
902
  };
@@ -943,8 +963,9 @@ Command.prototype.optionHelp = function() {
943
963
 
944
964
  // Append the help information
945
965
  return this.options.map(function(option) {
946
- return pad(option.flags, width) + ' ' + option.description;
947
- }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
966
+ return pad(option.flags, width) + ' ' + option.description
967
+ + ((option.bool != false && option.defaultValue !== undefined) ? ' (default: ' + option.defaultValue + ')' : '');
968
+ }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
948
969
  .join('\n');
949
970
  };
950
971
 
@@ -969,7 +990,7 @@ Command.prototype.commandHelp = function() {
969
990
  cmd._name
970
991
  + (cmd._alias ? '|' + cmd._alias : '')
971
992
  + (cmd.options.length ? ' [options]' : '')
972
- + ' ' + args
993
+ + (args ? ' ' + args : '')
973
994
  , cmd._description
974
995
  ];
975
996
  });
@@ -984,7 +1005,7 @@ Command.prototype.commandHelp = function() {
984
1005
  , ''
985
1006
  , commands.map(function(cmd) {
986
1007
  var desc = cmd[1] ? ' ' + cmd[1] : '';
987
- return pad(cmd[0], width) + desc;
1008
+ return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
988
1009
  }).join('\n').replace(/^/gm, ' ')
989
1010
  , ''
990
1011
  ].join('\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander",
3
- "version": "2.11.0",
3
+ "version": "2.13.0",
4
4
  "description": "the complete solution for node.js command-line programs",
5
5
  "keywords": [
6
6
  "commander",
@@ -14,16 +14,21 @@
14
14
  "type": "git",
15
15
  "url": "https://github.com/tj/commander.js.git"
16
16
  },
17
- "devDependencies": {
18
- "should": "^11.2.1",
19
- "sinon": "^2.3.5"
20
- },
21
17
  "scripts": {
22
- "test": "make test"
18
+ "test": "make test && npm run test-typings",
19
+ "test-typings": "node_modules/typescript/bin/tsc -p tsconfig.json"
23
20
  },
24
21
  "main": "index",
25
22
  "files": [
26
- "index.js"
23
+ "index.js",
24
+ "typings/index.d.ts"
27
25
  ],
28
- "dependencies": {}
26
+ "dependencies": {},
27
+ "devDependencies": {
28
+ "@types/node": "^7.0.48",
29
+ "should": "^11.2.1",
30
+ "sinon": "^2.4.1",
31
+ "typescript": "^2.6.2"
32
+ },
33
+ "typings": "typings/index.d.ts"
29
34
  }
@@ -0,0 +1,306 @@
1
+ // Type definitions for commander 2.11
2
+ // Project: https://github.com/visionmedia/commander.js
3
+ // Definitions by: Alan Agius <https://github.com/alan-agius4>, Marcelo Dezem <https://github.com/mdezem>, vvakame <https://github.com/vvakame>, Jules Randolph <https://github.com/sveinburne>
4
+ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
+
6
+ declare namespace local {
7
+
8
+ class Option {
9
+ flags: string;
10
+ required: boolean;
11
+ optional: boolean;
12
+ bool: boolean;
13
+ short?: string;
14
+ long: string;
15
+ description: string;
16
+
17
+ /**
18
+ * Initialize a new `Option` with the given `flags` and `description`.
19
+ *
20
+ * @param {string} flags
21
+ * @param {string} [description]
22
+ */
23
+ constructor(flags: string, description?: string);
24
+ }
25
+
26
+ class Command extends NodeJS.EventEmitter {
27
+ [key: string]: any;
28
+
29
+ args: string[];
30
+
31
+ /**
32
+ * Initialize a new `Command`.
33
+ *
34
+ * @param {string} [name]
35
+ */
36
+ constructor(name?: string);
37
+
38
+ /**
39
+ * Set the program version to `str`.
40
+ *
41
+ * This method auto-registers the "-V, --version" flag
42
+ * which will print the version number when passed.
43
+ *
44
+ * @param {string} str
45
+ * @param {string} [flags]
46
+ * @returns {Command} for chaining
47
+ */
48
+ version(str: string, flags?: string): Command;
49
+
50
+ /**
51
+ * Add command `name`.
52
+ *
53
+ * The `.action()` callback is invoked when the
54
+ * command `name` is specified via __ARGV__,
55
+ * and the remaining arguments are applied to the
56
+ * function for access.
57
+ *
58
+ * When the `name` is "*" an un-matched command
59
+ * will be passed as the first arg, followed by
60
+ * the rest of __ARGV__ remaining.
61
+ *
62
+ * @example
63
+ * program
64
+ * .version('0.0.1')
65
+ * .option('-C, --chdir <path>', 'change the working directory')
66
+ * .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
67
+ * .option('-T, --no-tests', 'ignore test hook')
68
+ *
69
+ * program
70
+ * .command('setup')
71
+ * .description('run remote setup commands')
72
+ * .action(function() {
73
+ * console.log('setup');
74
+ * });
75
+ *
76
+ * program
77
+ * .command('exec <cmd>')
78
+ * .description('run the given remote command')
79
+ * .action(function(cmd) {
80
+ * console.log('exec "%s"', cmd);
81
+ * });
82
+ *
83
+ * program
84
+ * .command('teardown <dir> [otherDirs...]')
85
+ * .description('run teardown commands')
86
+ * .action(function(dir, otherDirs) {
87
+ * console.log('dir "%s"', dir);
88
+ * if (otherDirs) {
89
+ * otherDirs.forEach(function (oDir) {
90
+ * console.log('dir "%s"', oDir);
91
+ * });
92
+ * }
93
+ * });
94
+ *
95
+ * program
96
+ * .command('*')
97
+ * .description('deploy the given env')
98
+ * .action(function(env) {
99
+ * console.log('deploying "%s"', env);
100
+ * });
101
+ *
102
+ * program.parse(process.argv);
103
+ *
104
+ * @param {string} name
105
+ * @param {string} [desc] for git-style sub-commands
106
+ * @param {CommandOptions} [opts] command options
107
+ * @returns {Command} the new command
108
+ */
109
+ command(name: string, desc?: string, opts?: commander.CommandOptions): Command;
110
+
111
+ /**
112
+ * Define argument syntax for the top-level command.
113
+ *
114
+ * @param {string} desc
115
+ * @returns {Command} for chaining
116
+ */
117
+ arguments(desc: string): Command;
118
+
119
+ /**
120
+ * Parse expected `args`.
121
+ *
122
+ * For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
123
+ *
124
+ * @param {string[]} args
125
+ * @returns {Command} for chaining
126
+ */
127
+ parseExpectedArgs(args: string[]): Command;
128
+
129
+ /**
130
+ * Register callback `fn` for the command.
131
+ *
132
+ * @example
133
+ * program
134
+ * .command('help')
135
+ * .description('display verbose help')
136
+ * .action(function() {
137
+ * // output help here
138
+ * });
139
+ *
140
+ * @param {(...args: any[]) => void} fn
141
+ * @returns {Command} for chaining
142
+ */
143
+ action(fn: (...args: any[]) => void): Command;
144
+
145
+ /**
146
+ * Define option with `flags`, `description` and optional
147
+ * coercion `fn`.
148
+ *
149
+ * The `flags` string should contain both the short and long flags,
150
+ * separated by comma, a pipe or space. The following are all valid
151
+ * all will output this way when `--help` is used.
152
+ *
153
+ * "-p, --pepper"
154
+ * "-p|--pepper"
155
+ * "-p --pepper"
156
+ *
157
+ * @example
158
+ * // simple boolean defaulting to false
159
+ * program.option('-p, --pepper', 'add pepper');
160
+ *
161
+ * --pepper
162
+ * program.pepper
163
+ * // => Boolean
164
+ *
165
+ * // simple boolean defaulting to true
166
+ * program.option('-C, --no-cheese', 'remove cheese');
167
+ *
168
+ * program.cheese
169
+ * // => true
170
+ *
171
+ * --no-cheese
172
+ * program.cheese
173
+ * // => false
174
+ *
175
+ * // required argument
176
+ * program.option('-C, --chdir <path>', 'change the working directory');
177
+ *
178
+ * --chdir /tmp
179
+ * program.chdir
180
+ * // => "/tmp"
181
+ *
182
+ * // optional argument
183
+ * program.option('-c, --cheese [type]', 'add cheese [marble]');
184
+ *
185
+ * @param {string} flags
186
+ * @param {string} [description]
187
+ * @param {((arg1: any, arg2: any) => void) | RegExp} [fn] function or default
188
+ * @param {*} [defaultValue]
189
+ * @returns {Command} for chaining
190
+ */
191
+ option(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
192
+ option(flags: string, description?: string, defaultValue?: any): Command;
193
+
194
+ /**
195
+ * Allow unknown options on the command line.
196
+ *
197
+ * @param {boolean} [arg] if `true` or omitted, no error will be thrown for unknown options.
198
+ * @returns {Command} for chaining
199
+ */
200
+ allowUnknownOption(arg?: boolean): Command;
201
+
202
+ /**
203
+ * Parse `argv`, settings options and invoking commands when defined.
204
+ *
205
+ * @param {string[]} argv
206
+ * @returns {Command} for chaining
207
+ */
208
+ parse(argv: string[]): Command;
209
+
210
+ /**
211
+ * Parse options from `argv` returning `argv` void of these options.
212
+ *
213
+ * @param {string[]} argv
214
+ * @returns {ParseOptionsResult}
215
+ */
216
+ parseOptions(argv: string[]): commander.ParseOptionsResult;
217
+
218
+ /**
219
+ * Return an object containing options as key-value pairs
220
+ *
221
+ * @returns {{[key: string]: string}}
222
+ */
223
+ opts(): { [key: string]: string };
224
+
225
+ /**
226
+ * Set the description to `str`.
227
+ *
228
+ * @param {string} str
229
+ * @return {(Command | string)}
230
+ */
231
+ description(str: string): Command;
232
+ description(): string;
233
+
234
+ /**
235
+ * Set an alias for the command.
236
+ *
237
+ * @param {string} alias
238
+ * @return {(Command | string)}
239
+ */
240
+ alias(alias: string): Command;
241
+ alias(): string;
242
+
243
+ /**
244
+ * Set or get the command usage.
245
+ *
246
+ * @param {string} str
247
+ * @return {(Command | string)}
248
+ */
249
+ usage(str: string): Command;
250
+ usage(): string;
251
+
252
+ /**
253
+ * Set the name of the command.
254
+ *
255
+ * @param {string} str
256
+ * @return {Command}
257
+ */
258
+ name(str: string): Command;
259
+
260
+ /**
261
+ * Get the name of the command.
262
+ *
263
+ * @return {string}
264
+ */
265
+ name(): string;
266
+
267
+ /**
268
+ * Output help information for this command.
269
+ *
270
+ * @param {(str: string) => string} [cb]
271
+ */
272
+ outputHelp(cb?: (str: string) => string): void;
273
+
274
+ /** Output help information and exit. */
275
+ help(): void;
276
+ }
277
+
278
+ }
279
+
280
+ declare namespace commander {
281
+
282
+ type Command = local.Command
283
+
284
+ type Option = local.Option
285
+
286
+ interface CommandOptions {
287
+ noHelp?: boolean;
288
+ isDefault?: boolean;
289
+ }
290
+
291
+ interface ParseOptionsResult {
292
+ args: string[];
293
+ unknown: string[];
294
+ }
295
+
296
+ interface CommanderStatic extends Command {
297
+ Command: typeof local.Command;
298
+ Option: typeof local.Option;
299
+ CommandOptions: CommandOptions;
300
+ ParseOptionsResult: ParseOptionsResult;
301
+ }
302
+
303
+ }
304
+
305
+ declare const commander: commander.CommanderStatic;
306
+ export = commander;