From eacaee86881f4cdf1c29fa2b15340ccba83f1379 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Fri, 4 Aug 2023 01:19:32 +0300 Subject: [PATCH] Warn about help option obscured by version option _registerOption() has been borrowed from deb3bdf in #1923. Watch out for merge conflicts! --- lib/command.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/command.js b/lib/command.js index b6938a483..b9dad2b3e 100644 --- a/lib/command.js +++ b/lib/command.js @@ -500,6 +500,24 @@ 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. * @@ -507,6 +525,8 @@ Expecting one of '${allowedValues.join("', '")}'`); * @return {Command} `this` command for chaining */ addOption(option) { + this._registerOption(option); + const oname = option.name(); const name = option.attributeName(); @@ -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. @@ -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);