Skip to content
Myr

Add support for multiple aliases (#1236)

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

* Allow for multiple aliases #531 * Add tests for multiple alias behaviours * Add aliases() mainly for getter * Fix typo

authored by John Gee · committed by GitHub · parent b59adfc · Browse files · 4 files+104 added−12 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+104 added−12 removed

  • tests:+54 added−1 removed
  • other:+50 added−11 removed

4 files+104 added−12 removed

modified:tests/command.alias.test.js+54 added-1 removed
modified:typings/commander-tests.ts+4 added
167 unmodified lines
168// alias169const aliasThis: commander.Command = program.alias('my alias');170const aliasValue: string = program.alias();171172// aliases173const aliasesThis: commander.Command = program.aliases(['first-alias', 'second-alias']);174const aliasesValue: string[] = program.aliases();175176// usage177const usageThis: commander.Command = program.usage('my usage');178const usageValue: string = program.usage();179
43 unmodified lines
modified:typings/index.d.ts+15 added
273 unmodified lines
274275 /**276 * Set an alias for the command.277 *278 * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.279 *280 * @returns `this` command for chaining281 */282 alias(alias: string): this;283 /**
1 unmodified line
285 */286 alias(): string;287288 /**289 * Set aliases for the command.290 *291 * Only the first alias is shown in the auto-generated help.292 *293 * @returns `this` command for chaining294 */295 aliases(aliases: string[]): this;296 /**297 * Get aliases for the command.298 */299 aliases(): string[];300301 /**302 * Set the command usage.303 *304 * @returns `this` command for chaining305 */
81 unmodified lines
modified:index.js+31 added-11 removed
115 unmodified lines
116 this._executableHandler = false;117 this._executableFile = null; // custom name for executable118 this._defaultCommandName = null;119 this._exitCallback = null;120 this._alias = null;120 this._aliases = [];121122 this._hidden = false;123 this._helpFlags = '-h, --help';124 this._helpDescription = 'display help for command';
811 unmodified lines
936 * @api private937 */938 _findCommand(name) {939 if (!name) return undefined;940 return this.commands.find(cmd => cmd._name === name || cmd._alias === name);940 return this.commands.find(cmd => cmd._name === name || cmd._aliases.includes(name));941 };942943 /**944 * Return an option matching `arg` if any.
255 unmodified lines
1200 * Set the description to `str`.1201 *1202 * @param {string} str1203 * @param {Object} [argsDescription]1204 * @return {String|Command}1204 * @return {string|Command}1205 * @api public1206 */12071208 description(str, argsDescription) {
3 unmodified lines
1212 return this;1213 };12141215 /**1216 * Set an alias for the command1216 * Set an alias for the command.1217 *1218 * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.1219 *1218 * @param {string} alias1219 * @return {String|Command}1220 * @param {string} [alias]1221 * @return {string|Command}1222 * @api public1223 */12241225 alias(alias) {1224 if (alias === undefined) return this._alias;1226 if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility12271228 let command = this;1229 if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {1230 // assume adding alias for last added executable subcommand, rather than this
1 unmodified line
1232 }12331234 if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');12351234 command._alias = alias;1236 command._aliases.push(alias);1237 return this;1238 };12391240 /**1241 * Set aliases for the command.1242 *1243 * Only the first alias is shown in the auto-generated help.1244 *1245 * @param {string[]} [aliases]1246 * @return {string[]|Command}1247 * @api public1248 */12491250 aliases(aliases) {1251 // Getter for the array of aliases is the main reason for having aliases() in addition to alias().1252 if (aliases === undefined) return this._aliases;12531254 aliases.forEach((alias) => this.alias(alias));1255 return this;1256 };12571258 /**1259 * Set / get the command usage `str`.1260 *1261 * @param {string} [str]1262 * @return {String|Command}
46 unmodified lines
1309 }).join(' ');13101311 return [1312 cmd._name +1293 (cmd._alias ? '|' + cmd._alias : '') +1313 (cmd._aliases[0] ? '|' + cmd._aliases[0] : '') +1314 (cmd.options.length ? ' [options]' : '') +1315 (args ? ' ' + args : ''),1316 cmd._description1317 ];
151 unmodified lines
1469 }1470 }14711472 let cmdName = this._name;1453 if (this._alias) {1454 cmdName = cmdName + '|' + this._alias;1473 if (this._aliases[0]) {1474 cmdName = cmdName + '|' + this._aliases[0];1475 }1476 let parentCmdNames = '';1477 for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {1478 parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
261 unmodified lines