Merge pull request #210 from tandrewnichols/add-alias
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Added alias feature
authored by Randall Koutnik · committed by Randall Koutnik · parents ebdd789, e00c4ca · Browse files · 3 files·+39 added−7 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+39 added−7 removed
- tests:+7 added−0 removed
- other:+32 added−7 removed
Provenance
- Commit
33db110- Tree
839b146- Author
Randall Koutnik <rkoutnik@gmail.com>- Committer
Randall Koutnik <rkoutnik@gmail.com>- Parent 1
ebdd789- Parent 2
e00c4ca
3 files changed+39 added−7 removed
modified:examples/deploy+1 added
22 unmodified lines23 });24 25 program26 .command('exec <cmd>')27+ .alias('ex')28 .description('execute the given remote cmd')29 .option("-e, --exec_mode <mode>", "Which exec mode to use")30 .action(function(cmd, options){31 console.log('exec "%s" using %s mode', cmd, options.exec_mode);15 unmodified linesmodified:test/test.options.commands.js+7 added
27 unmodified lines28293031323334353648 unmodified lines858687888990919293949596979818 unmodified lines27 unmodified lines });
program .command('exec <cmd>') .alias('ex') .description('execute the given remote cmd') .option("-e, --exec_mode <mode>", "Which exec mode to use") .option("-t, --target [target]", "Target to use") .action(function(cmd, options){48 unmodified linesprogram.commands[1].exec_mode.should.equal("mode2");program.commands[1].target.should.equal("target1");cmdValue.should.equal("exec3");
program.parse(['node', 'test', '--config', 'conf7', 'ex', '-e', 'mode3', 'exec4']);program.config.should.equal("conf7");program.commands[1].exec_mode.should.equal("mode3");program.commands[1].should.not.have.property.target;cmdValue.should.equal("exec4");
// Make sure we still catch errors with required values for optionsvar exceptionOccurred = false;var oldProcessExit = process.exit;var oldConsoleError = console.error;18 unmodified linesmodified:index.js+31 added-7 removed
209 unmodified lines210 */211 212 Command.prototype.action = function(fn){213 var self = this;214- this.parent.on(this._name, function(args, unknown){214+ var listener = function(args, unknown){215 // Parse any so-far unknown options216 args = args || [];217 unknown = unknown || [];218 27 unmodified lines246 args.push(self);247 }248 249 fn.apply(this, args);250- });250+ };251+ this.parent.on(this._name, listener);252+ if (this._alias) this.parent.on(this._alias, listener);253 return this;254 };255 256 /**418 unmodified lines675 return this;676 };677 678 /**679+ * Set an alias for the command680+ *681+ * @param {String} alias682+ * @return {String|Command}683+ * @api public684+ */685+686+Command.prototype.alias = function(alias){687+ if (0 == arguments.length) return this._alias;688+ this._alias = alias;689+ return this;690+};691+692+/**693 * Set / get the command usage `str`.694 *695 * @param {String} str696 * @return {String|Command}69 unmodified lines766 ? '<' + arg.name + '>'767 : '[' + arg.name + ']';768 }).join(' ');769 754- return pad(cmd._name770+ return cmd._name771+ + (cmd._alias772+ ? '|' + cmd._alias773+ : '')774 + (cmd.options.length775 ? ' [options]'757- : '') + ' ' + args, 22)776+ : '') + ' ' + args777 + (cmd.description()759- ? ' ' + cmd.description()760- : '');778+ ? '\n ' + cmd.description()779+ : '')780+ + '\n';781 }).join('\n').replace(/^/gm, ' ')782 , ''783 ].join('\n');784 };7 unmodified lines792 793 Command.prototype.helpInformation = function(){794 return [795 ''776- , ' Usage: ' + this._name + ' ' + this.usage()796+ , ' Usage: ' + this._name797+ + (this._alias798+ ? '|' + this._alias799+ : '')800+ + ' ' + this.usage()801 , '' + this.commandHelp()802 , ' Options:'803 , ''804 , '' + this.optionHelp().replace(/^/gm, ' ')72 unmodified lines