Skip to content
Myr

Make order not matter for adding combo positive/negative options (#2405)

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 2e96cd3 · Browse files · 4 files+65 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+65 added−16 removed

  • tests:+40 added−0 removed
  • other:+25 added−16 removed

4 files+65 added−16 removed

modified:examples/options-negatable.js+1 added-1 removed
modified:lib/command.js+23 added-11 removed
673 unmodified lines
674 const oname = option.name();675 const name = option.attributeName();676677 // store default value678 if (option.negate) {679 // --no-foo is special and defaults foo to true, unless a --foo option is already defined680 const positiveLongFlag = option.long.replace(/^--no-/, '--');681 if (!this._findOption(positiveLongFlag)) {682 this.setOptionValueWithSource(683 name,684 option.defaultValue === undefined ? true : option.defaultValue,685 'default',686 );687 }688 } else if (option.defaultValue !== undefined) {678 if (option.defaultValue !== undefined) {679 this.setOptionValueWithSource(name, option.defaultValue, 'default');680 }681682 // handler for cli and env supplied values
432 unmodified lines
1115 return this;1116 }11171118 _prepareForParse() {1119 // Save the state the first time, then restore the state before each subsequent parse.1120 if (this._savedState === null) {1121 // Do the special default of lone negated option to true, now that we have all the options.1122 // Filter for negated options that have not been processed already.1123 this.options1124 .filter(1125 (option) =>1126 option.negate &&1127 option.defaultValue === undefined &&1128 this.getOptionValue(option.attributeName()) === undefined,1129 )1130 .forEach((option) => {1131 // check for lone negated option: --no-foo without a --foo option1132 const positiveLongFlag = option.long.replace(/^--no-/, '--');1133 if (!this._findOption(positiveLongFlag)) {1134 this.setOptionValueWithSource(1135 option.attributeName(),1136 true,1137 'default',1138 );1139 }1140 });11411142 this.saveStateBeforeParse();1143 } else {1144 this.restoreStateBeforeParse();1145 }
1647 unmodified lines
modified:tests/options.bool.combo.test.js+40 added
modified:Readme.md+1 added-4 removed
275 unmodified lines
276277### Other option types, negatable boolean and boolean|value278279You can define a boolean option long name with a leading `no-` to set the option value to false when used.280Defined alone this also makes the option true by default.280Defined alone without a matching positive option, this also makes the option true by default.281282If you define `--foo` first, adding `--no-foo` does not change the default value from what it would283otherwise be.284282Example file: [options-negatable.js](./examples/options-negatable.js)283284```js285program
871 unmodified lines