diff --git a/index.js b/index.js index 68b54ffda..59b995eff 100644 --- a/index.js +++ b/index.js @@ -154,6 +154,16 @@ Command.prototype.command = function(name, desc) { return cmd; }; +/** + * Define argument syntax for the top-level command. + * + * @api public + */ + +Command.prototype.arguments = function (desc) { + return this.parseExpectedArgs(desc.split(/ +/)); +} + /** * Add an implicit `help [cmd]` subcommand * which invokes `--help` for the given command. @@ -247,8 +257,10 @@ Command.prototype.action = function(fn){ fn.apply(this, args); }; - this.parent.on(this._name, listener); - if (this._alias) this.parent.on(this._alias, listener); + var parent = this.parent || this; + var name = parent === this ? '*' : this._name; + parent.on(name, listener); + if (this._alias) parent.on(this._alias, listener); return this; }; diff --git a/test/test.arguments.js b/test/test.arguments.js new file mode 100644 index 000000000..cdf6b92b7 --- /dev/null +++ b/test/test.arguments.js @@ -0,0 +1,30 @@ +/** + * Module dependencies. + */ + +var program = require('../') + , should = require('should'); + +var envValue = ""; +var cmdValue = ""; + +program + .version('0.0.1') + .arguments(' [env]') + .action(function (cmd, env) { + cmdValue = cmd; + envValue = env; + }) + .option('-C, --chdir ', 'change the working directory') + .option('-c, --config ', 'set config path. defaults to ./deploy.conf') + .option('-T, --no-tests', 'ignore test hook') + +program.parse(['node', 'test', '--config', 'conf']); +program.config.should.equal("conf"); +cmdValue.should.equal(""); +envValue.should.equal(""); + +program.parse(['node', 'test', '--config', 'conf1', 'setup', '--setup_mode', 'mode3', 'env1']); +program.config.should.equal("conf1"); +cmdValue.should.equal("setup"); +envValue.should.equal("env1");