Skip to content

Commit

Permalink
Merge pull request #1 from rike422/support_subcommand_alias
Browse files Browse the repository at this point in the history
Support the alias of Git style sub-command
  • Loading branch information
jamesr73 committed Jun 24, 2016
2 parents ad2e539 + 81577a6 commit 47f678f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
6 changes: 3 additions & 3 deletions examples/pm
Expand Up @@ -4,10 +4,10 @@ var program = require('..');

program
.version('0.0.1')
.command('install [name]', 'install one or more packages')
.command('search [query]', 'search with optional query')
.command('install [name]', 'install one or more packages').alias('i')
.command('search [query]', 'search with optional query').alias('s')
.command('list', 'list packages installed')
.command('publish', 'publish the package')
.command('publish', 'publish the package').alias('p')
.parse(process.argv);

// here .command() is invoked with a description,
Expand Down
17 changes: 15 additions & 2 deletions index.js
Expand Up @@ -459,8 +459,15 @@ Command.prototype.parse = function(argv) {

// executable sub-commands
var name = result.args[0];
var aliasCommand = this.commands.filter(function(command) {
var _alias = command.alias();
return _alias !== undefined && _alias === name;
})[0];
if (this._execs[name] && typeof this._execs[name] != "function") {
return this.executeSubCommand(argv, args, parsed.unknown);
} else if (aliasCommand !== undefined) {
args[0] = aliasCommand._name;
return this.executeSubCommand(argv, args, parsed.unknown);
} else if (this.defaultExecutable) {
// use the default subcommand
args.unshift(name = this.defaultExecutable);
Expand Down Expand Up @@ -850,8 +857,14 @@ Command.prototype.description = function(str) {
*/

Command.prototype.alias = function(alias) {
if (0 == arguments.length) return this._alias;
this._alias = alias;
var command = undefined;
if(this.commands.length == 0) {
command = this;
} else {
command = this.commands[this.commands.length - 1]
}
if (0 == arguments.length) return command._alias;
command._alias = alias;
return this;
};

Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/pm
Expand Up @@ -4,10 +4,10 @@ var program = require('../../');

program
.version('0.0.1')
.command('install [name]', 'install one or more packages')
.command('search [query]', 'search with optional query')
.command('cache', 'actions dealing with the cache')
.command('list', 'list packages installed')
.command('publish', 'publish or update package')
.command('install [name]', 'install one or more packages').alias('i')
.command('search [query]', 'search with optional query').alias('s')
.command('cache', 'actions dealing with the cache').alias('c')
.command('list', 'list packages installed').alias('l')
.command('publish', 'publish or update package').alias('p')
.command('default', 'default command', {noHelp: true, isDefault: true})
.parse(process.argv);
36 changes: 36 additions & 0 deletions test/test.command.executableSubcommandAlias.js
@@ -0,0 +1,36 @@
var exec = require('child_process').exec
, path = require('path')
, should = require('should');



var bin = path.join(__dirname, './fixtures/pm')
// not exist
exec(bin + ' l', function (error, stdout, stderr) {
//stderr.should.equal('\n pm-list(1) does not exist, try --help\n\n');
// TODO error info are not the same in between <=v0.8 and later version
should.notEqual(0, stderr.length);
});

// success case
exec(bin + ' i', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

// subcommand bin file with explicit extension
exec(bin + ' p', function (error, stdout, stderr) {
stdout.should.equal('publish\n');
});

// spawn EACCES
exec(bin + ' s', function (error, stdout, stderr) {
// TODO error info are not the same in between <v0.10 and v0.12
should.notEqual(0, stderr.length);
});

// when `bin` is a symbol link for mocking global install
var bin = path.join(__dirname, './fixtures/pmlink')
// success case
exec(bin + ' i', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

0 comments on commit 47f678f

Please sign in to comment.