From d1a1b395e31362b6315669d1bab484fba69856e9 Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Sun, 13 Aug 2023 18:19:07 +0200 Subject: [PATCH] Refactor addOption() part storing default value Eliminate excessive ._findOption() calls and undefined checks. Has the tiny side effect of overwriting the default value of a negatable option when its negating component prefixed with "--no-" is added if that component has an explicitly specified default value. --- lib/command.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/command.js b/lib/command.js index 590a271dd..bd0bd7cea 100644 --- a/lib/command.js +++ b/lib/command.js @@ -511,14 +511,14 @@ Expecting one of '${allowedValues.join("', '")}'`); const name = option.attributeName(); // store default value - if (option.negate) { + if (option.defaultValue !== undefined || !option.negate) { + this.setOptionValueWithSource(name, option.defaultValue, 'default'); + } else { // --no-foo is special and defaults foo to true, unless a --foo option is already defined const positiveLongFlag = option.long.replace(/^--no-/, '--'); if (!this._findOption(positiveLongFlag)) { - this.setOptionValueWithSource(name, option.defaultValue === undefined ? true : option.defaultValue, 'default'); + this.setOptionValueWithSource(name, true, 'default'); } - } else if (option.defaultValue !== undefined) { - this.setOptionValueWithSource(name, option.defaultValue, 'default'); } // register the option