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
Provenance
- Commit
28e8d3f- Tree
7984c23- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
b59adfc
4 files changed+104 added−12 removed
modified:tests/command.alias.test.js+54 added-1 removed
10 unmodified lines11121314151516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747510 unmodified lines const helpInformation = program.helpInformation(); expect(helpInformation).toMatch('info|i');});
test('when command = alias then error', () => {test('when command has aliases added separately then only first appears in help', () => { const program = new commander.Command(); program .command('list [thing]') .alias('ls') .alias('dir'); const helpInformation = program.helpInformation(); expect(helpInformation).toMatch('list|ls ');});
test('when command has aliases then only first appears in help', () => { const program = new commander.Command(); program .command('list [thing]') .aliases(['ls', 'dir']); const helpInformation = program.helpInformation(); expect(helpInformation).toMatch('list|ls ');});
test('when command name = alias then error', () => { const program = new commander.Command(); expect(() => { program .command('fail') .alias('fail'); }).toThrow("Command alias can't be the same as its name");});
test('when use alias then action handler called', () => { const program = new commander.Command(); const actionMock = jest.fn(); program .command('list') .alias('ls') .action(actionMock); program.parse(['ls'], { from: 'user' }); expect(actionMock).toHaveBeenCalled();});
test('when use second alias added separately then action handler called', () => { const program = new commander.Command(); const actionMock = jest.fn(); program .command('list') .alias('ls') .alias('dir') .action(actionMock); program.parse(['dir'], { from: 'user' }); expect(actionMock).toHaveBeenCalled();});
test('when use second of aliases then action handler called', () => { const program = new commander.Command(); const actionMock = jest.fn(); program .command('list') .aliases(['ls', 'dir']) .action(actionMock); program.parse(['dir'], { from: 'user' }); expect(actionMock).toHaveBeenCalled();});modified:typings/commander-tests.ts+4 added
167 unmodified lines168 // alias169 const aliasThis: commander.Command = program.alias('my alias');170 const aliasValue: string = program.alias();171 172+// aliases173+const aliasesThis: commander.Command = program.aliases(['first-alias', 'second-alias']);174+const aliasesValue: string[] = program.aliases();175+176 // usage177 const usageThis: commander.Command = program.usage('my usage');178 const usageValue: string = program.usage();179 43 unmodified linesmodified:typings/index.d.ts+15 added
273 unmodified lines274 275 /**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 line285 */286 alias(): string;287 288 /**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[];300+301+ /**302 * Set the command usage.303 *304 * @returns `this` command for chaining305 */81 unmodified linesmodified:index.js+31 added-11 removed
115 unmodified lines116 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 = [];121 122 this._hidden = false;123 this._helpFlags = '-h, --help';124 this._helpDescription = 'display help for command';811 unmodified lines936 * @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 };942 943 /**944 * Return an option matching `arg` if any.255 unmodified lines1200 * Set the description to `str`.1201 *1202 * @param {string} str1203 * @param {Object} [argsDescription]1204- * @return {String|Command}1204+ * @return {string|Command}1205 * @api public1206 */1207 1208 description(str, argsDescription) {3 unmodified lines1212 return this;1213 };1214 1215 /**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 */1224 1225 alias(alias) {1224- if (alias === undefined) return this._alias;1226+ if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility1227 1228 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 this1 unmodified line1232 }1233 1234 if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');1235 1234- command._alias = alias;1236+ command._aliases.push(alias);1237 return this;1238 };1239 1240 /**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+ */1249+1250+ 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;1253+1254+ aliases.forEach((alias) => this.alias(alias));1255+ return this;1256+ };1257+1258+ /**1259 * Set / get the command usage `str`.1260 *1261 * @param {string} [str]1262 * @return {String|Command}46 unmodified lines1309 }).join(' ');1310 1311 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 lines1469 }1470 }1471 1472 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