Skip to content
Myr

Merge pull request #277 from whitlockjc/fix-issue-53

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add support for variadic arguments

authored by Zhiye Li · committed by Zhiye Li · parents 558169f, a150a21 · Browse files · 3 files+160 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+160 added−9 removed

  • tests:+61 added−0 removed
  • other:+99 added−9 removed

3 files+160 added−9 removed

added:test/test.variadic.args.js+61 added
modified:index.js+67 added-8 removed
125 unmodified lines
126 * console.log('exec "%s"', cmd);127 * });128 *129 * program130 * .command('teardown <dir> [otherDirs...]')131 * .description('run teardown commands')132 * .action(function(dir, otherDirs) {133 * console.log('dir "%s"', dir);134 * if (otherDirs) {135 * otherDirs.forEach(function (oDir) {136 * console.log('dir "%s"', oDir);137 * });138 * }139 * });140 *141 * program142 * .command('*')143 * .description('deploy the given env')144 * .action(function(env) {145 * console.log('deploying "%s"', env);
44 unmodified lines
190Command.prototype.parseExpectedArgs = function(args) {191 if (!args.length) return;192 var self = this;193 args.forEach(function(arg) {194 var argDetails = {195 required: false,196 name: '',197 variadic: false198 };199200 switch (arg[0]) {201 case '<':184 self._args.push({ required: true, name: arg.slice(1, -1) });202 argDetails.required = true;203 argDetails.name = arg.slice(1, -1);204 break;205 case '[':187 self._args.push({ required: false, name: arg.slice(1, -1) });206 argDetails.name = arg.slice(1, -1);207 break;208 }209210 if (argDetails.name.indexOf('...') === argDetails.name.length - 3) {211 argDetails.variadic = true;212 argDetails.name = argDetails.name.slice(0, -3);213 }214215 if (argDetails.name) {216 self._args.push(argDetails);217 }218 });219 return this;220};221
38 unmodified lines
260261 self._args.forEach(function(arg, i) {262 if (arg.required && null == args[i]) {263 self.missingArgument(arg.name);264 } else if (arg.variadic) {265 if (i !== self._args.length - 1) {266 self.variadicArgNotLast(arg.name);267 }268269 args[i] = args.slice(i);270 }271 });272273 // Always append ourselves to the end of the arguments,
422 unmodified lines
696 console.error();697 process.exit(1);698};699700/**701 * Variadic argument with `name` is not the last argument as required.702 *703 * @param {String} name704 * @api private705 */706707Command.prototype.variadicArgNotLast = function(name) {708 console.error();709 console.error(" error: variadic arguments must be last `%s'", name);710 console.error();711 process.exit(1);712};713714/**715 * Set the program version to `str`.716 *717 * This method auto-registers the "-V, --version" flag
54 unmodified lines
772 */773774Command.prototype.usage = function(str) {775 var args = this._args.map(function(arg) {729 return arg.required730 ? '<' + arg.name + '>'731 : '[' + arg.name + ']';776 return humanReadableArgName(arg);777 });778779 var usage = '[options]'780 + (this.commands.length ? ' [command]' : '')
59 unmodified lines
840 if (!this.commands.length) return '';841842 var commands = this.commands.map(function(cmd) {843 var args = cmd._args.map(function(arg) {799 return arg.required800 ? '<' + arg.name + '>'801 : '[' + arg.name + ']';844 return humanReadableArgName(arg);845 }).join(' ');846847 return [848 cmd._name
113 unmodified lines
962 process.exit(0);963 }964 }965}966967/**968 * Takes an argument an returns its human readable equivalent for help usage.969 *970 * @param {Object} arg971 * @return {String}972 * @api private973 */974975function humanReadableArgName(arg) {976 var nameOutput = arg.name + (arg.variadic === true ? '...' : '');977978 return arg.required979 ? '<' + nameOutput + '>'980 : '[' + nameOutput + ']'981}
modified:Readme.md+32 added-1 removed
39 unmodified lines
40```4142 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.4344## Variadic arguments4546 The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to47 append `...` to the argument name. Here is an example:4849```js50#!/usr/bin/env node5152/**53 * Module dependencies.54 */5556var program = require('commander');5758program59 .version('0.0.1')60 .command('rmdir <dir> [otherDirs...]')61 .action(function (dir, otherDirs) {62 console.log('rmdir %s', dir);63 if (otherDirs) {64 otherDirs.forEach(function (oDir) {65 console.log('rmdir %s', oDir);66 });67 }68 })69 .parse(process.argv);70```7172 An `Array` is used for the value of a variadic argument. This applies to `program.args` as well as the argument passed73 to your action as demonstrated above.7475## Automated --help7677 The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:78
132 unmodified lines
211 - [progress bars](https://github.com/tj/node-progress)212 - [more progress bars](https://github.com/substack/node-multimeter)213 - [examples](https://github.com/tj/commander.js/tree/master/examples)214184## License 215## License216217(The MIT License)218219Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
19 unmodified lines