diff --git a/examples/pm b/examples/pm index 534aac650..baac7816f 100755 --- a/examples/pm +++ b/examples/pm @@ -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, diff --git a/index.js b/index.js index eb412e3d8..2f16a8489 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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; }; diff --git a/test/fixtures/pm b/test/fixtures/pm index 28517e535..6f4345da4 100755 --- a/test/fixtures/pm +++ b/test/fixtures/pm @@ -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); diff --git a/test/test.command.executableSubcommandAlias.js b/test/test.command.executableSubcommandAlias.js new file mode 100644 index 000000000..5e423ff0f --- /dev/null +++ b/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