Skip to content

Commit

Permalink
Warn about help option obscured by version option
Browse files Browse the repository at this point in the history
_registerOption() has been borrowed from deb3bdf in tj#1923.
Watch out for merge conflicts!
  • Loading branch information
aweebit committed Aug 3, 2023
1 parent 371751e commit eacaee8
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,33 @@ Expecting one of '${allowedValues.join("', '")}'`);
return new Option(flags, description);
}

/**
* Check for option flag conflicts.
* Register option if no conflicts found.
* Throw otherwise.
*
* @param {Option} option
* @api private
*/

_registerOption(option) {
this.options.push(option);
this._checkForObscuredHelpOption((matchingOptionFlags) => (
`Help option '${this._helpOption.flags}' is obscured after adding option '${option.flags}'${matchingOptionFlags.length > 1
? `
- conflicts with options '${matchingOptionFlags.join("' and '")}'`
: ''}`));
}

/**
* Add an option.
*
* @param {Option} option
* @return {Command} `this` command for chaining
*/
addOption(option) {
this._registerOption(option);

const oname = option.name();
const name = option.attributeName();

Expand All @@ -521,14 +541,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
this.setOptionValueWithSource(name, option.defaultValue, 'default');
}

// register the option
this.options.push(option);
this._checkForObscuredHelpOption((matchingOptionFlags) => (
`Help option '${this._helpOption.flags}' is obscured after adding option '${option.flags}'${matchingOptionFlags.length > 1
? `
- conflicts with options '${matchingOptionFlags.join("' and '")}'`
: ''}`));

// handler for cli and env supplied values
const handleOptionValue = (val, invalidValueMessage, valueSource) => {
// val is null for optional option used without an optional-argument.
Expand Down Expand Up @@ -1825,7 +1837,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
description = description || 'output the version number';
const versionOption = this.createOption(flags, description);
this._versionOptionName = versionOption.attributeName();
this.options.push(versionOption);
this._registerOption(versionOption);
this.on('option:' + versionOption.name(), () => {
this._outputConfiguration.writeOut(`${str}\n`);
this._exit(0, 'commander.version', str);
Expand Down

0 comments on commit eacaee8

Please sign in to comment.