Skip to content
Myr

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

3 files+136 added−16 removed

modified:examples/deploy+7 added-6 removed
13 unmodified lines
1415program16 .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 });2425program26 .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 });3233program34 .command('*')
1 unmodified line
36 console.log('deploying "%s"', env);37 });3839program.parse(process.argv);3738
modified:lib/commander.js+42 added-10 removed
197 unmodified lines
198 */199200Command.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 lines
334 // guess name335 if (!this.name) this.name = basename(argv[1]);336337 // 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};342343/**344 * Normalize `args`, splitting joined short flags. For example
33 unmodified lines
378 * @return {Command} for chaining379 * @api private380 */381366Command.prototype.parseArgs = function(args){382Command.prototype.parseArgs = function(args, unknown){383 var cmds = this.commands384 , len = cmds.length385 , name;386387 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 }402403 return this;404};405
23 unmodified lines
429 */430431Command.prototype.parseOptions = function(argv){432 var args = []410 , argv = argv.slice(2)433 , len = argv.length434 , literal435 , option436 , arg;437438 var unknownOptions = [];439440 // parse options441 for (var i = 0; i < len; ++i) {442 arg = argv[i];443
36 unmodified lines
480 }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 }466467 return args;498 499 return {args: args, unknown: unknownOptions};500};501502/**503 * Argument `name` is missing.
452 unmodified lines
added:test/test.options.commands.js+87 added
1/**2 * Module dependencies.3 */45var program = require('../')6 , should = require('should');78program9 .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')1314program15 .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 });2526program27 .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 });3435program36 .command('*')37 .action(function(env){38 console.log('deploying "%s"', env);39 });40 41program.parse(['node', 'test', '--config', 'conf']);42program.config.should.equal("conf");43program.commands[0].should.not.have.property.setup_mode;44program.commands[1].should.not.have.property.exec_mode;4546program.parse(['node', 'test', '--config', 'conf', 'setup', '--setup_mode', 'mode3', 'env1']);47program.config.should.equal("conf");48program.commands[0].setup_mode.should.equal("mode3");49program.commands[0].should.not.have.property.host;5051program.parse(['node', 'test', '--config', 'conf', 'setup', '--setup_mode', 'mode3', '-o', 'host1', 'env1']);52program.config.should.equal("conf");53program.commands[0].setup_mode.should.equal("mode3");54program.commands[0].host.should.equal("host1");5556program.parse(['node', 'test', '--config', 'conf', 'setup', '-s', 'mode4', 'env1']);57program.config.should.equal("conf");58program.commands[0].setup_mode.should.equal("mode4");5960program.parse(['node', 'test', '--config', 'conf', 'exec', '--exec_mode', 'mode1', 'exec1']);61program.config.should.equal("conf");62program.commands[1].exec_mode.should.equal("mode1");63program.commands[1].should.not.have.property.target;6465program.parse(['node', 'test', '--config', 'conf', 'exec', '-e', 'mode2', 'exec1']);66program.config.should.equal("conf");67program.commands[1].exec_mode.should.equal("mode2");6869program.parse(['node', 'test', '--config', 'conf', 'exec', '--target', 'target1', '-e', 'mode2', 'exec1']);70program.config.should.equal("conf");71program.commands[1].exec_mode.should.equal("mode2");72program.commands[1].target.should.equal("target1");7374// Make sure we still catch errors with required values for options75var exceptionOccurred = false;76var oldProcessExit = process.exit;77process.exit = function() { exceptionOccurred = true; throw new Error(); };7879try {80 program.parse(['node', 'test', '--config', 'conf', 'exec', '-t', 'target1', 'exec1', '-e']);81}82catch(ex) {83}8485exceptionOccurred.should.be.true;8687process.exit = oldProcessExit;