Skip to content
Myr

Add check for overlapping command names or aliases (#2059)

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

authored by John Gee · committed by GitHub · parent b96af40 · Browse files · 2 files+83 added−4 removed
This commit

State

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

Trailers

  • Co-authored-by: aweebit <aweebit64@gmail.com>

Diff stats

2 files+83 added−4 removed

  • tests:+49 added−0 removed
  • other:+34 added−4 removed

2 files+83 added−4 removed

modified:lib/command.js+34 added-4 removed
163 unmodified lines
164 if (opts.isDefault) this._defaultCommandName = cmd._name;165 cmd._hidden = !!(opts.noHelp || opts.hidden); // noHelp is deprecated old name for hidden166 cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor167 if (args) cmd.arguments(args);168 this.commands.push(cmd);168 this._registerCommand(cmd);169 cmd.parent = this;170 cmd.copyInheritedSettings(this);171172 if (desc) return this;
108 unmodified lines
281 opts = opts || {};282 if (opts.isDefault) this._defaultCommandName = cmd._name;283 if (opts.noHelp || opts.hidden) cmd._hidden = true; // modifying passed command due to existing implementation284285 this.commands.push(cmd);285 this._registerCommand(cmd);286 cmd.parent = this;287 cmd._checkForBrokenPassThrough();288289 return this;
244 unmodified lines
534 }535 throw err;536 }537 }538539 /**540 * Check for option flag conflicts.540 * Register option if no conflicts found.541 * Throw otherwise.541 * Register option if no conflicts found, or throw on conflict.542 *543 * @param {Option} option544 * @api private545 */
5 unmodified lines
551 const matchingFlag = (option.long && this._findOption(option.long)) ? option.long : option.short;552 throw new Error(`Cannot add option '${option.flags}'${this._name && ` to command '${this._name}'`} due to conflicting flag '${matchingFlag}'553- already used by option '${matchingOption.flags}'`);554 }555556 this.options.push(option);557 }558559 /**560 * Check for command name and alias conflicts with existing commands.561 * Register command if no conflicts found, or throw on conflict.562 *563 * @param {Command} command564 * @api private565 */566567 _registerCommand(command) {568 const knownBy = (cmd) => {569 return [cmd.name()].concat(cmd.aliases());570 };571572 const alreadyUsed = knownBy(command).find((name) => this._findCommand(name));573 if (alreadyUsed) {574 const existingCmd = knownBy(this._findCommand(alreadyUsed)).join('|');575 const newCmd = knownBy(command).join('|');576 throw new Error(`cannot add command '${newCmd}' as already have command '${existingCmd}'`);577 }578579 this.commands.push(command);580 }581582 /**583 * Add an option.584 *585 * @param {Option} option586 * @return {Command} `this` command for chaining
1336 unmodified lines
1923 command = this.commands[this.commands.length - 1];1924 }19251926 if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');1927 const matchingCommand = this.parent?._findCommand(alias);1928 if (matchingCommand) {1929 // c.f. _registerCommand1930 const existingCmd = [matchingCommand.name()].concat(matchingCommand.aliases()).join('|');1931 throw new Error(`cannot add alias '${alias}' to command '${this.name()}' as already have command '${existingCmd}'`);1932 }19331934 command._aliases.push(alias);1935 return this;1936 }
301 unmodified lines
added:tests/command.registerClash.test.js+49 added