Browse files

Merge branch 'pr/545'

2 parents c86ba2a + ad46a4d commit c6236d9504b60d9a2e6aa7fc3ce17a12f48f4a3e @zhiyelee zhiyelee committed Jul 4, 2016
View
6 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,
View
25 index.js
@@ -459,11 +459,24 @@ Command.prototype.parse = function(argv) {
// executable sub-commands
var name = result.args[0];
+
+ var aliasCommand = null;
+ // check alias of sub commands
+ if (name) {
+ aliasCommand = this.commands.filter(function(command) {
+ return command.alias() === name;
+ })[0];
+ }
+
if (this._execs[name] && typeof this._execs[name] != "function") {
return this.executeSubCommand(argv, args, parsed.unknown);
+ } else if (aliasCommand) {
+ // is alias of a subCommand
+ args[0] = aliasCommand._name;
+ return this.executeSubCommand(argv, args, parsed.unknown);
} else if (this.defaultExecutable) {
// use the default subcommand
- args.unshift(name = this.defaultExecutable);
+ args.unshift(this.defaultExecutable);
return this.executeSubCommand(argv, args, parsed.unknown);
}
@@ -850,8 +863,14 @@ Command.prototype.description = function(str) {
*/
Command.prototype.alias = function(alias) {
- if (0 == arguments.length) return this._alias;
- this._alias = alias;
+ var command = this;
+ if(this.commands.length !== 0) {
+ command = this.commands[this.commands.length - 1]
+ }
+
+ if (arguments.length === 0) return command._alias;
+
+ command._alias = alias;
return this;
};
View
8 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('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')
- .command('publish', 'publish or update package')
+ .command('publish', 'publish or update package').alias('p')
.command('default', 'default command', {noHelp: true, isDefault: true})
.parse(process.argv);
View
20 test/test.command.alias.help.js
@@ -0,0 +1,20 @@
+var program = require('../')
+ , should = require('should');
+
+program
+ .command('info [thing]')
+ .alias('i')
+ .action(function () {
+ });
+
+program
+ .command('save [file]')
+ .alias('s')
+ .action(function() {
+ });
+
+program.parse(['node', 'test']);
+
+program.commandHelp().should.containEql('info|i');
+program.commandHelp().should.containEql('save|s');
+program.commandHelp().should.not.containEql('test|');
View
17 test/test.command.executableSubcommandAlias.help.js
@@ -0,0 +1,17 @@
+var exec = require('child_process').exec
+ , path = require('path')
+ , should = require('should');
+
+
+
+var bin = path.join(__dirname, './fixtures/pm')
+
+// success case
+exec(bin + ' help', function (error, stdout, stderr) {
+ stdout.should.containEql('install|i');
+ stdout.should.containEql('search|s');
+ stdout.should.containEql('cache|c');
+ stdout.should.containEql('list');
+ stdout.should.containEql('publish|p');
+ stdout.should.not.containEql('pm|');
+});
View
28 test/test.command.executableSubcommandAlias.js
@@ -0,0 +1,28 @@
+var exec = require('child_process').exec
+ , path = require('path')
+ , should = require('should');
+
+var bin = path.join(__dirname, './fixtures/pm')
+
+// 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) {
+ // 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 c6236d9

Please sign in to comment.