Throw error when add option with clashing flags (#2055)
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 d90e81e · Browse files · 2 files·+83 added−5 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−5 removed
- tests:+59 added−0 removed
- other:+24 added−5 removed
Provenance
- Commit
b96af40- Tree
4fe5a63- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
d90e81e
2 files changed+83 added−5 removed
modified:lib/command.js+24 added-5 removed
533 unmodified lines534 }535 throw err;536 }537 }538+ /**539+ * Check for option flag conflicts.540+ * Register option if no conflicts found.541+ * Throw otherwise.542+ *543+ * @param {Option} option544+ * @api private545+ */546 547+ _registerOption(option) {548+ const matchingOption = (option.short && this._findOption(option.short)) ||549+ (option.long && this._findOption(option.long));550+ if (matchingOption) {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+ }555+ this.options.push(option);556+ }557+558 /**559 * Add an option.560 *561 * @param {Option} option562 * @return {Command} `this` command for chaining563 */564 addOption(option) {565+ this._registerOption(option);566+567 const oname = option.name();568 const name = option.attributeName();569 570 // store default value6 unmodified lines577 } else if (option.defaultValue !== undefined) {578 this.setOptionValueWithSource(name, option.defaultValue, 'default');579 }580 560- // register the option561- this.options.push(option);562-581 // handler for cli and env supplied values582 const handleOptionValue = (val, invalidValueMessage, valueSource) => {583 // val is null for optional option used without an optional-argument.584 // val is undefined for boolean and negated option.1256 unmodified lines1841 this._version = str;1842 flags = flags || '-V, --version';1843 description = description || 'output the version number';1844 const versionOption = this.createOption(flags, description);1827- this._versionOptionName = versionOption.attributeName(); // [sic] not defined in constructor, partly legacy, partly only needed at root1828- this.options.push(versionOption);1845+ this._versionOptionName = versionOption.attributeName();1846+ this._registerOption(versionOption);1847+1848 this.on('option:' + versionOption.name(), () => {1849 this._outputConfiguration.writeOut(`${str}\n`);1850 this._exit(0, 'commander.version', str);1851 });356 unmodified linesadded:tests/options.registerClash.test.js+59 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859const { Command, Option } = require('../');
describe('.option()', () => { test('when short option flag conflicts then throws', () => { expect(() => { const program = new Command(); program .option('-c, --cheese <type>', 'cheese type') .option('-c, --conflict'); }).toThrow('Cannot add option'); });
test('when long option flag conflicts then throws', () => { expect(() => { const program = new Command(); program .option('-c, --cheese <type>', 'cheese type') .option('-H, --cheese'); }).toThrow('Cannot add option'); });
test('when use help options separately then does not throw', () => { expect(() => { const program = new Command(); program .option('-h, --help', 'display help'); }).not.toThrow(); });
test('when reuse flags in subcommand then does not throw', () => { expect(() => { const program = new Command(); program .option('e, --example'); program.command('sub') .option('e, --example'); }).not.toThrow(); });});
describe('.addOption()', () => { test('when short option flags conflicts then throws', () => { expect(() => { const program = new Command(); program .option('-c, --cheese <type>', 'cheese type') .addOption(new Option('-c, --conflict')); }).toThrow('Cannot add option'); });
test('when long option flags conflicts then throws', () => { expect(() => { const program = new Command(); program .option('-c, --cheese <type>', 'cheese type') .addOption(new Option('-H, --cheese')); }).toThrow('Cannot add option'); });});