Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added alias feature #210

Merged
merged 4 commits into from
Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ program

program
.command('exec <cmd>')
.alias('ex')
.description('execute the given remote cmd')
.option("-e, --exec_mode <mode>", "Which exec mode to use")
.action(function(cmd, options){
Expand Down
38 changes: 31 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Command.prototype.parseExpectedArgs = function(args){

Command.prototype.action = function(fn){
var self = this;
this.parent.on(this._name, function(args, unknown){
var listener = function(args, unknown){
// Parse any so-far unknown options
args = args || [];
unknown = unknown || [];
Expand Down Expand Up @@ -247,7 +247,9 @@ Command.prototype.action = function(fn){
}

fn.apply(this, args);
});
};
this.parent.on(this._name, listener);
if (this._alias) this.parent.on(this._alias, listener);
return this;
};

Expand Down Expand Up @@ -673,6 +675,20 @@ Command.prototype.description = function(str){
return this;
};

/**
* Set an alias for the command
*
* @param {String} alias
* @return {String|Command}
* @api public
*/

Command.prototype.alias = function(alias){
if (0 == arguments.length) return this._alias;
this._alias = alias;
return this;
};

/**
* Set / get the command usage `str`.
*
Expand Down Expand Up @@ -751,13 +767,17 @@ Command.prototype.commandHelp = function(){
: '[' + arg.name + ']';
}).join(' ');

return pad(cmd._name
return cmd._name
+ (cmd._alias
? '|' + cmd._alias
: '')
+ (cmd.options.length
? ' [options]'
: '') + ' ' + args, 22)
: '') + ' ' + args
+ (cmd.description()
? ' ' + cmd.description()
: '');
? '\n ' + cmd.description()
: '')
+ '\n';
}).join('\n').replace(/^/gm, ' ')
, ''
].join('\n');
Expand All @@ -773,7 +793,11 @@ Command.prototype.commandHelp = function(){
Command.prototype.helpInformation = function(){
return [
''
, ' Usage: ' + this._name + ' ' + this.usage()
, ' Usage: ' + this._name
+ (this._alias
? '|' + this._alias
: '')
+ ' ' + this.usage()
, '' + this.commandHelp()
, ' Options:'
, ''
Expand Down
7 changes: 7 additions & 0 deletions test/test.options.commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ program

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")
Expand Down Expand Up @@ -85,6 +86,12 @@ program.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 options
var exceptionOccurred = false;
var oldProcessExit = process.exit;
Expand Down