* Set an alias for the command. * * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help. * * @param {string} [alias] * @return {(string|Command)}
(alias)
| 2261 | */ |
| 2262 | |
| 2263 | alias(alias) { |
| 2264 | if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility |
| 2265 | |
| 2266 | /** @type {Command} */ |
| 2267 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
| 2268 | let command = this; |
| 2269 | if ( |
| 2270 | this.commands.length !== 0 && |
| 2271 | this.commands[this.commands.length - 1]._executableHandler |
| 2272 | ) { |
| 2273 | // assume adding alias for last added executable subcommand, rather than this |
| 2274 | command = this.commands[this.commands.length - 1]; |
| 2275 | } |
| 2276 | |
| 2277 | if (alias === command._name) |
| 2278 | throw new Error("Command alias can't be the same as its name"); |
| 2279 | const matchingCommand = this.parent?._findCommand(alias); |
| 2280 | if (matchingCommand) { |
| 2281 | // c.f. _registerCommand |
| 2282 | const existingCmd = [matchingCommand.name()] |
| 2283 | .concat(matchingCommand.aliases()) |
| 2284 | .join('|'); |
| 2285 | throw new Error( |
| 2286 | `cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`, |
| 2287 | ); |
| 2288 | } |
| 2289 | |
| 2290 | command._aliases.push(alias); |
| 2291 | return this; |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * Set aliases for the command. |
no test coverage detected