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
Provenance
- Commit
6ccb639- Tree
3706747- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent 1
ba74d56- Parent 2
4788f70
3 files changed+70 added−23 removed
modified:examples/deploy+5 added
26 unmodified lines27 .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 });37 38 program39 .command('*')5 unmodified linesmodified:lib/commander.js+52 added-21 removed
199 unmodified lines200 Command.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 lines222 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 lines396 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 lines495 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 lines602 * @api public603 */604 605 Command.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 lines638 */639 640 Command.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 };651 652 /**653 * Return command help documentation.13 unmodified lines667 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 lines967 return str + Array(len + 1).join(' ');968 }969 970 /**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 */977 951-exports.option('-h, --help', 'output usage information');952-exports.on('help', function(){953- process.stdout.write(this.helpInformation());954- exports.emit('--help');955- process.exit(0);956-});978+function 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
11 unmodified lines12131415161718192012 unmodified lines3334353637383940414244 unmodified lines87888990919293949596979899100101102103104105106107108979810911 unmodified lines .option('-T, --no-tests', 'ignore test hook')
var envValue = "";var cmdValue = "";var customHelp = false;
program .command('setup [env]') .description('run setup commands for all envs')12 unmodified lines .option("-e, --exec_mode <mode>", "Which exec mode to use") .option("-t, --target [target]", "Target to use") .action(function(cmd, options){ cmdValue = cmd; }).on("--help", function(){ customHelp = true; });
program .command('*')44 unmodified lines
// Make sure we still catch errors with required values for optionsvar exceptionOccurred = false;var oldProcessExit = process.exit;var oldConsoleError = console.error;process.exit = function() { exceptionOccurred = true; throw new Error(); };console.error = function() {};
try { program.parse(['node', 'test', '--config', 'conf6', 'exec', '--help']);} catch(ex) { program.config.should.equal("conf6");}
try { program.parse(['node', 'test', '--config', 'conf', 'exec', '-t', 'target1', 'exec1', '-e']);}catch(ex) {}
process.exit = oldProcessExit;exceptionOccurred.should.be.true;
process.exit = oldProcessExit;No newline at end of filecustomHelp.should.be.true;