Skip to content
Myr

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

2 files+83 added−5 removed

modified:lib/command.js+24 added-5 removed
533 unmodified lines
534 }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 */546547 _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 }557558 /**559 * Add an option.560 *561 * @param {Option} option562 * @return {Command} `this` command for chaining563 */564 addOption(option) {565 this._registerOption(option);566567 const oname = option.name();568 const name = option.attributeName();569570 // store default value
6 unmodified lines
577 } else if (option.defaultValue !== undefined) {578 this.setOptionValueWithSource(name, option.defaultValue, 'default');579 }580560 // register the option561 this.options.push(option);562581 // 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 lines
1841 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);18471848 this.on('option:' + versionOption.name(), () => {1849 this._outputConfiguration.writeOut(`${str}\n`);1850 this._exit(0, 'commander.version', str);1851 });
356 unmodified lines
added:tests/options.registerClash.test.js+59 added