Skip to content
Myr

merged / tweaked

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

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parents ba74d56, 4788f70 · Browse files · 3 files+70 added−23 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+70 added−23 removed

  • tests:+13 added−2 removed
  • other:+57 added−21 removed

3 files+70 added−23 removed

modified:examples/deploy+5 added
26 unmodified lines
27 .description('execute the given remote cmd')28 .option("-e, --exec_mode <mode>", "Which exec mode to use")29 .action(function(cmd, options){30 console.log('exec "%s" using %s mode', cmd, options.exec_mode);31 }).on('--help', function() {32 console.log(' Examples:');33 console.log('');34 console.log(' $ deploy exec sequential');35 console.log(' $ deploy exec async');36 });3738program39 .command('*')
5 unmodified lines
modified:lib/commander.js+52 added-21 removed
199 unmodified lines
200Command.prototype.action = function(fn){201 var self = this;202 this.parent.on(this.name, function(args, unknown){ 203 // Parse any so-far unknown options204 unknown = unknown || [];205 var parsed = self.parseOptions(unknown);206 207 // Output help if necessary208 outputHelpIfNecessary(self, parsed.unknown);209 210 // If there are still any unknown options, then we simply 211 // die, unless someone asked for help, in which case we give it207 // die.208 if (parsed.unknown.length > 0) {212 // to them, and then we die.213 if (parsed.unknown.length > 0) { 214 self.unknownOption(parsed.unknown[0]);215 }216 217 self.args.forEach(function(arg, i){
4 unmodified lines
222 223 // Always append ourselves to the end of the arguments,224 // to make sure we match the number of arguments the user225 // expects226 if (self.args.length) {221 args[self.args.length] = self;227 args[self.args.length] = self;228 } else {229 args.push(self);230 }231 232 fn.apply(this, args);233 });234 return this;
161 unmodified lines
396 if (args.length) {397 name = args[0];398 if (this.listeners(name).length) {399 this.emit(args.shift(), args, unknown);391 } else { 400 } else {401 this.emit('*', args);402 }394 }395 else {403 } else {404 outputHelpIfNecessary(this, unknown);405 406 // If there were no args and we have unknown options,407 // then they are extraneous and we need to error.398 if (unknown.length > 0) {408 if (unknown.length > 0) { 409 this.unknownOption(unknown[0]);410 }411 }412
82 unmodified lines
495 496 // If the next argument looks like it might be497 // an argument for this option, we pass it on.498 // If it isn't, then it'll simply be ignored489 if (argv[i+1] && !(argv[i+1][0] === '-')) {499 if (argv[i+1] && '-' != argv[i+1][0]) {500 unknownOptions.push(argv[++i]);501 }502 continue;503 }
98 unmodified lines
602 * @api public603 */604605Command.prototype.usage = function(str){606 var args = this.args.map(function(arg){607 return arg.required608 ? '<' + arg.name + '>'609 : '[' + arg.name + ']';610 });611 var usage = '[options'612 + (this.commands.length ? '] [command' : '')598 + ']';613 + ']'614 + (this.args.length ? ' ' + args : '');615 if (0 == arguments.length) return this._usage || usage;616 this._usage = str;617 return this;618};
19 unmodified lines
638 */639640Command.prototype.optionHelp = function(){641 var width = this.largestOptionLength();642 643 // Prepend the help information644 return [pad('-h, --help', width) + ' ' + 'output usage information']626 return this.options.map(function(option){627 return pad(option.flags, width)628 + ' ' + option.description;629 }).join('\n');645 .concat(this.options.map(function(option){646 return pad(option.flags, width)647 + ' ' + option.description;648 }))649 .join('\n');650};651652/**653 * Return command help documentation.
13 unmodified lines
667 return arg.required668 ? '<' + arg.name + '>'669 : '[' + arg.name + ']';670 }).join(' ');651 return cmd.name + ' ' + args671 return cmd.name 672 + (cmd.options.length 673 ? ' [options]'674 : '') + ' ' + args675 + (cmd.description()676 ? '\n' + cmd.description()677 : '');678 }).join('\n\n').replace(/^/gm, ' ')
288 unmodified lines
967 return str + Array(len + 1).join(' ');968}969970/**948 * Default -h, --help option.971 * Output help information if necessary972 *973 * @param {Command} command to output help for974 * @param {Array} array of options to search for -h or --help975 * @api private976 */977951exports.option('-h, --help', 'output usage information');952exports.on('help', function(){953 process.stdout.write(this.helpInformation());954 exports.emit('--help');955 process.exit(0);956});978function outputHelpIfNecessary(cmd, options) {979 options = options || [];980 for (var i = 0; i < options.length; i++) {981 if (options[i] == '--help' || options[i] == '-h') {982 cmd.emit('--help');983 process.stdout.write(cmd.helpInformation());984 process.exit(0);985 }986 }987}
modified:test/test.options.commands.js+13 added-2 removed