Merge branch 'release/12.x' of github.com:tj/commander.js into release/12.x
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by John Gee · parents b521b7a, 1d3dd47 · 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.
Diff stats
2 files+83 added−4 removed
- tests:+49 added−0 removed
- other:+34 added−4 removed
Provenance
- Commit
ce4d5f7- Tree
f342970- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
b521b7a- Parent 2
1d3dd47
2 files changed+83 added−4 removed
modified:lib/command.js+34 added-4 removed
163 unmodified lines164 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);171 172 if (desc) return this;108 unmodified lines281 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 implementation284 285- this.commands.push(cmd);285+ this._registerCommand(cmd);286 cmd.parent = this;287 cmd._checkForBrokenPassThrough();288 289 return this;244 unmodified lines534 }535 throw err;536 }537 }538+539 /**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 lines551 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 }555+556 this.options.push(option);557 }558 559 /**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+ */566+567+ _registerCommand(command) {568+ const knownBy = (cmd) => {569+ return [cmd.name()].concat(cmd.aliases());570+ };571+572+ 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+ }578+579+ this.commands.push(command);580+ }581+582+ /**583 * Add an option.584 *585 * @param {Option} option586 * @return {Command} `this` command for chaining1336 unmodified lines1923 command = this.commands[this.commands.length - 1];1924 }1925 1926 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+ }1933 1934 command._aliases.push(alias);1935 return this;1936 }301 unmodified linesadded:tests/command.registerClash.test.js+49 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849const { Command } = require('../');
test('when command name conflicts with existing name then throw', () => { expect(() => { const program = new Command(); program.command('one'); program.command('one'); }).toThrow('cannot add command');});
test('when command name conflicts with existing alias then throw', () => { expect(() => { const program = new Command(); program.command('one').alias('1'); program.command('1'); }).toThrow('cannot add command');});
test('when command alias conflicts with existing name then throw', () => { expect(() => { const program = new Command(); program.command('one'); program.command('1').alias('one'); }).toThrow('cannot add alias');});
test('when command alias conflicts with existing alias then throw', () => { expect(() => { const program = new Command(); program.command('one').alias('1'); program.command('unity').alias('1'); }).toThrow('cannot add alias');});
test('when .addCommand name conflicts with existing name then throw', () => { expect(() => { const program = new Command(); program.command('one'); program.addCommand(new Command('one')); }).toThrow('cannot add command');});
test('when .addCommand alias conflicts with existing name then throw', () => { expect(() => { const program = new Command(); program.command('one'); program.addCommand(new Command('unity').alias('one')); }).toThrow('cannot add command');});