Skip to content
Myr

Merge pull request #1 from rike422/support_subcommand_alias

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Support the alias of Git style sub-command

authored by James Robertson · committed by GitHub · parents ad2e539, 81577a6 · Browse files · 4 files+59 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+59 added−10 removed

  • tests:+41 added−5 removed
  • other:+18 added−5 removed

4 files+59 added−10 removed

modified:examples/pm+3 added-3 removed
2 unmodified lines
3var program = require('..');45program6 .version('0.0.1')7 .command('install [name]', 'install one or more packages')8 .command('search [query]', 'search with optional query')7 .command('install [name]', 'install one or more packages').alias('i')8 .command('search [query]', 'search with optional query').alias('s')9 .command('list', 'list packages installed')10 .command('publish', 'publish the package')10 .command('publish', 'publish the package').alias('p')11 .parse(process.argv);1213// here .command() is invoked with a description,14// and no .action(callback) calls to handle sub-commands.
13 unmodified lines
modified:test/fixtures/pm+5 added-5 removed
2 unmodified lines
3var program = require('../../');45program6 .version('0.0.1')7 .command('install [name]', 'install one or more packages')8 .command('search [query]', 'search with optional query')9 .command('cache', 'actions dealing with the cache')10 .command('list', 'list packages installed')11 .command('publish', 'publish or update package')7 .command('install [name]', 'install one or more packages').alias('i')8 .command('search [query]', 'search with optional query').alias('s')9 .command('cache', 'actions dealing with the cache').alias('c')10 .command('list', 'list packages installed').alias('l')11 .command('publish', 'publish or update package').alias('p')12 .command('default', 'default command', {noHelp: true, isDefault: true})13 .parse(process.argv);
added:test/test.command.executableSubcommandAlias.js+36 added
modified:index.js+15 added-2 removed
457 unmodified lines
458 var result = this.parseArgs(this.args, parsed.unknown);459460 // executable sub-commands461 var name = result.args[0];462 var aliasCommand = this.commands.filter(function(command) {463 var _alias = command.alias();464 return _alias !== undefined && _alias === name;465 })[0];466 if (this._execs[name] && typeof this._execs[name] != "function") {467 return this.executeSubCommand(argv, args, parsed.unknown);468 } else if (aliasCommand !== undefined) {469 args[0] = aliasCommand._name;470 return this.executeSubCommand(argv, args, parsed.unknown);471 } else if (this.defaultExecutable) {472 // use the default subcommand473 args.unshift(name = this.defaultExecutable);474 return this.executeSubCommand(argv, args, parsed.unknown);
381 unmodified lines
856 * @api public857 */858859Command.prototype.alias = function(alias) {860 var command = undefined;861 if(this.commands.length == 0) {862 command = this;863 } else {864 command = this.commands[this.commands.length - 1]865 }853 if (0 == arguments.length) return this._alias;854 this._alias = alias;866 if (0 == arguments.length) return command._alias;867 command._alias = alias;868 return this;869};870871/**
252 unmodified lines