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
Provenance
- Commit
63eed4a- Tree
61cafba- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
2e96cd3
4 files changed+65 added−16 removed
modified:examples/options-negatable.js+1 added-1 removed
20 unmodified lines212223242525262720 unmodified linesconsole.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);
// Try the following:// node options-negatable.js// node options-negatable.js --sauce// node options-negatable.js --no-sauce// node options-negatable.js --cheese=blue// node options-negatable.js --no-sauce --no-cheesemodified:lib/command.js+23 added-11 removed
673 unmodified lines674 const oname = option.name();675 const name = option.attributeName();676 677 // 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 }681 682 // handler for cli and env supplied values432 unmodified lines1115 return this;1116 }1117 1118 _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+ });1141+1142 this.saveStateBeforeParse();1143 } else {1144 this.restoreStateBeforeParse();1145 }1647 unmodified linesmodified:tests/options.bool.combo.test.js+40 added
147 unmodified linesExpand all148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191147 unmodified linesExpand all program.parse(['node', 'test', '--no-pepper']); expect(program.opts().pepper).toBe(false); });});
describe('only lone negative causes implicit default of true', () => { test('when lone negative then default value is true', () => { const program = new commander.Command(); program.option('--no-pepper', 'remove pepper'); program.parse([], { from: 'user' }); expect(program.opts().pepper).toBe(true); });
test('when boolean combo and negative first then no implicit default', () => { // New behaviour in Commander 15, now only lone negative gets implicit default of true. const program = new commander.Command(); program .option('--no-pepper', 'remove pepper') .option('--pepper', 'pepper only'); program.parse([], { from: 'user' }); expect(program.opts().pepper).toBeUndefined(); });
test('when boolean combo and negative second then no implicit default', () => { const program = new commander.Command(); program .option('--pepper', 'pepper only') .option('--no-pepper', 'remove pepper'); program.parse([], { from: 'user' }); expect(program.opts().pepper).toBeUndefined(); });
test('when boolean combo and negative second with explicit default then get explicit default', () => { // Prior to Commander 15, negative second would ignore implicit and explicit default value. // Simpler rule now and the special default behaviour is for lone negative only. Add test since // intentional change of behaviour for unlikely edge case. const program = new commander.Command(); program .option('--pepper', 'pepper only') .option('--no-pepper', 'remove pepper', 'plain'); program.parse([], { from: 'user' }); expect(program.opts().pepper).toBe('plain'); });});modified:Readme.md+1 added-4 removed
275 unmodified lines276 277 ### Other option types, negatable boolean and boolean|value278 279 You can define a boolean option long name with a leading `no-` to set the option value to false when used.280-Defined alone this also makes the option true by default.280+Defined alone without a matching positive option, this also makes the option true by default.281 282-If you define `--foo` first, adding `--no-foo` does not change the default value from what it would283-otherwise be.284-282 Example file: [options-negatable.js](./examples/options-negatable.js)283 284 ```js285 program871 unmodified lines