Add options for subcommands
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Itay Neeman · committed by Itay Neeman · parent 41fdf89 · Browse files · 3 files·+136 added−16 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+136 added−16 removed
- tests:+87 added−0 removed
- other:+49 added−16 removed
Provenance
- Commit
b32fcca- Tree
7fbac16- Author
Itay Neeman <itay@neeman.net>- Committer
Itay Neeman <itay@neeman.net>- Parent
41fdf89
3 files changed+136 added−16 removed
modified:examples/deploy+7 added-6 removed
13 unmodified lines14 15 program16 .command('setup [env]')17 .description('run setup commands for all envs')18+ .option("-s, --setup_mode [mode]", "Which setup mode to use")18- .action(function(env){19+ .action(function(env, options){20+ var mode = options.setup_mode || "normal";21 env = env || 'all';20- console.log('setup for %s env(s)', env);22+ console.log('setup for %s env(s) with %s mode', env, mode);23 });24 25 program26 .command('exec <cmd>')27 .description('execute the given remote cmd')28+ .option("-e, --exec_mode <mode>", "Which exec mode to use")26- .action(function(cmd){27- console.log('exec "%s"', cmd);29+ .action(function(cmd, options){30+ console.log('exec "%s" using %s mode', cmd, options.exec_mode);31 });32 33 program34 .command('*')1 unmodified line36 console.log('deploying "%s"', env);37 });38 39 program.parse(process.argv);37-38-modified:lib/commander.js+42 added-10 removed
197 unmodified lines198 */199 200 Command.prototype.action = function(fn){201 var self = this;202- this.parent.on(this.name, function(args){202+ this.parent.on(this.name, function(args, unknown){ 203+ // Parse any so-far unknown options204+ var parsed = self.parseOptions(unknown);205+ 206+ // If there are still any unknown options, then we simply 207+ // die.208+ if (parsed.unknown.length > 0) {209+ self.unknownOption(parsed.unknown[0]);210+ }211+ 212 self.args.forEach(function(arg, i){213 if (arg.required && null == args[i]) {214 self.missingArgument(arg.name);215 }216 });217+ 218+ // Always append ourselves to the end of the arguments,219+ // to make sure we match the number of arguments the user220+ // expects221+ args[self.args.length] = self;222+ 223 fn.apply(this, args);224 });225 return this;226 };107 unmodified lines334 // guess name335 if (!this.name) this.name = basename(argv[1]);336 337 // process argv323- this.args = this.parseOptions(this.normalize(argv));324- return this.parseArgs(this.args);338+ var parsed = this.parseOptions(this.normalize(argv.slice(2)));339+ this.args = parsed.args;340+ return this.parseArgs(this.args, parsed.unknown);341 };342 343 /**344 * Normalize `args`, splitting joined short flags. For example33 unmodified lines378 * @return {Command} for chaining379 * @api private380 */381 366-Command.prototype.parseArgs = function(args){382+Command.prototype.parseArgs = function(args, unknown){383 var cmds = this.commands384 , len = cmds.length385 , name;386 387 if (args.length) {388 name = args[0];389 if (this.listeners(name).length) {374- this.emit(args.shift(), args);375- } else {390+ this.emit(args.shift(), args, unknown);391+ } else { 392 this.emit('*', args);393 }394 }395+ else {396+ // If there were no args and we have unknown options,397+ // then they are extraneous and we need to error.398+ if (unknown.length > 0) {399+ this.unknownOption(unknown[0]);400+ }401+ }402 403 return this;404 };405 23 unmodified lines429 */430 431 Command.prototype.parseOptions = function(argv){432 var args = []410- , argv = argv.slice(2)433 , len = argv.length434 , literal435 , option436 , arg;437 438+ var unknownOptions = [];439+440 // parse options441 for (var i = 0; i < len; ++i) {442 arg = argv[i];443 36 unmodified lines480 }481 482 // looks like an option483 if (arg.length > 1 && '-' == arg[0]) {460- this.unknownOption(arg);484+ unknownOptions.push(arg);485+ 486+ // If the next argument looks like it might be487+ // an argument for this option, we pass it on.488+ // If it isn't, then it'll simply be ignored489+ if (argv[i+1] && !(argv[i+1][0] === '-')) {490+ unknownOptions.push(argv[++i]);491+ }492+ continue;493 }494 495 // arg496 args.push(arg);497 }466-467- return args;498+ 499+ return {args: args, unknown: unknownOptions};500 };501 502 /**503 * Argument `name` is missing.452 unmodified linesadded:test/test.options.commands.js+87 added
1+/**2+ * Module dependencies.3+ */4+5+var program = require('../')6+ , should = require('should');7+8+program9+ .version('0.0.1')10+ .option('-C, --chdir <path>', 'change the working directory')11+ .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')12+ .option('-T, --no-tests', 'ignore test hook')13+14+program15+ .command('setup [env]')16+ .description('run setup commands for all envs')17+ .option("-s, --setup_mode [mode]", "Which setup mode to use")18+ .option("-o, --host [host]", "Host to use")19+ .action(function(env, options){20+ var mode = options.setup_mode || "normal";21+ env = env || 'all';22+ 23+ env.should.equal('env1');24+ });25+26+program27+ .command('exec <cmd>')28+ .description('execute the given remote cmd')29+ .option("-e, --exec_mode <mode>", "Which exec mode to use")30+ .option("-t, --target [target]", "Target to use")31+ .action(function(cmd, options){32+ cmd.should.equal('exec1');33+ });34+35+program36+ .command('*')37+ .action(function(env){38+ console.log('deploying "%s"', env);39+ });40+ 41+program.parse(['node', 'test', '--config', 'conf']);42+program.config.should.equal("conf");43+program.commands[0].should.not.have.property.setup_mode;44+program.commands[1].should.not.have.property.exec_mode;45+46+program.parse(['node', 'test', '--config', 'conf', 'setup', '--setup_mode', 'mode3', 'env1']);47+program.config.should.equal("conf");48+program.commands[0].setup_mode.should.equal("mode3");49+program.commands[0].should.not.have.property.host;50+51+program.parse(['node', 'test', '--config', 'conf', 'setup', '--setup_mode', 'mode3', '-o', 'host1', 'env1']);52+program.config.should.equal("conf");53+program.commands[0].setup_mode.should.equal("mode3");54+program.commands[0].host.should.equal("host1");55+56+program.parse(['node', 'test', '--config', 'conf', 'setup', '-s', 'mode4', 'env1']);57+program.config.should.equal("conf");58+program.commands[0].setup_mode.should.equal("mode4");59+60+program.parse(['node', 'test', '--config', 'conf', 'exec', '--exec_mode', 'mode1', 'exec1']);61+program.config.should.equal("conf");62+program.commands[1].exec_mode.should.equal("mode1");63+program.commands[1].should.not.have.property.target;64+65+program.parse(['node', 'test', '--config', 'conf', 'exec', '-e', 'mode2', 'exec1']);66+program.config.should.equal("conf");67+program.commands[1].exec_mode.should.equal("mode2");68+69+program.parse(['node', 'test', '--config', 'conf', 'exec', '--target', 'target1', '-e', 'mode2', 'exec1']);70+program.config.should.equal("conf");71+program.commands[1].exec_mode.should.equal("mode2");72+program.commands[1].target.should.equal("target1");73+74+// Make sure we still catch errors with required values for options75+var exceptionOccurred = false;76+var oldProcessExit = process.exit;77+process.exit = function() { exceptionOccurred = true; throw new Error(); };78+79+try {80+ program.parse(['node', 'test', '--config', 'conf', 'exec', '-t', 'target1', 'exec1', '-e']);81+}82+catch(ex) {83+}84+85+exceptionOccurred.should.be.true;86+87+process.exit = oldProcessExit;