Skip to content
Myr

fixes behavior of --no-* options

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

A bug existed whenever a "--no"-prefixed option was defined along with its counterpart (e.g. "--status" and "--no-status"), which caused the corresponding boolean attribute to always be `false` when either option was specified on the command line. This was because the definition of a negating option would overwrite the default value of the attribute, combined with the fact that specifying either option would emit two events with conflicting logic. This has been corrected in two ways: 1. During the setup of a negating option, we now check to see if the non-negated counterpart was already defined, in which case we don't set/overwrite the default value (which would be "true"); 2. An option's name no longer omits its negation prefix; that is, "--no-cheese" is internally "no-cheese", which will distinguish it from "--cheese"—this will allow unique events to be registered and emitted, depending on which option is passed to the command, thus avoiding any attribute assignment collision. Additionally, tests for negated options were added/updated to more explicitly demonstrate the expected behavior, and a couple of relevant examples were fixed to match their intended behavior.

authored by usmonster · committed by usmonster · parent d060897 · Browse files · 6 files+60 added−19 removed
This commit

State

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

Diff stats

6 files+60 added−19 removed

  • tests:+41 added−2 removed
  • other:+19 added−17 removed

6 files+60 added−19 removed

modified:examples/pizza+2 added-4 removed
10 unmodified lines
11 .description('An application for pizzas ordering')12 .option('-p, --peppers', 'Add peppers')13 .option('-P, --pineapple', 'Add pineapple')14 .option('-b, --bbq', 'Add bbq sauce')15 .option('-c, --cheese <type>', 'Add the specified type of cheese [marble]')15 .option('-c, --cheese <type>', 'Add the specified type of cheese', 'marble')16 .option('-C, --no-cheese', 'You do not want any cheese')17 .parse(process.argv);1819console.log('you ordered a pizza with:');20if (program.peppers) console.log(' - peppers');21if (program.pineapple) console.log(' - pineapple');22if (program.bbq) console.log(' - bbq');2324var cheese = true === program.cheese25 ? 'marble'26 : program.cheese || 'no';24var cheese = !program.cheese ? 'no' : program.cheese;2526console.log(' - %s cheese', cheese);27console.log(program.args);
modified:test/test.options.bool.no.js+18 added-2 removed
added:test/test.options.defaults.no.js+23 added
modified:index.js+14 added-10 removed
58 unmodified lines
59 * @api private60 */6162Option.prototype.name = function() {63 return this.long64 .replace('--', '')65 .replace('no-', '');63 return this.long.replace(/^--/, '');64};6566/**67 * Return option name, in a camelcase format that can be used
3 unmodified lines
71 * @api private72 */7374Option.prototype.attributeName = function() {77 return camelcase(this.name());75 return camelcase(this.name().replace(/^no-/, ''));76};7778/**79 * Check if `arg` matches the short or long flag.
252 unmodified lines
332 * "-p --pepper"333 *334 * Examples:335 *338 * // simple boolean defaulting to false336 * // simple boolean defaulting to undefined337 * program.option('-p, --pepper', 'add pepper');338 *339 * program.pepper340 * // => undefined341 *342 * --pepper343 * program.pepper343 * // => Boolean344 * // => true345 *345 * // simple boolean defaulting to true346 * // simple boolean defaulting to true (unless non-negated option is also defined)347 * program.option('-C, --no-cheese', 'remove cheese');348 *349 * program.cheese350 * // => true
43 unmodified lines
394 }395396 // preassign default value only for --no-*, [optional], or <required>397 if (!option.bool || option.optional || option.required) {397 // when --no-* we make sure default is true398 if (!option.bool) defaultValue = true;398 // when --no-foo we make sure default is true, unless a --foo option is already defined399 if (!option.bool) {400 var opts = self.opts();401 defaultValue = Object.prototype.hasOwnProperty.call(opts, name) ? opts[name] : true;402 }403 // preassign only if we have a default404 if (defaultValue !== undefined) {405 self[name] = defaultValue;406 option.defaultValue = defaultValue;
22 unmodified lines
429 self[name] = val;430 }431 } else if (val !== null) {432 // reassign429 self[name] = val;433 self[name] = option.bool ? val : false;434 }435 });436437 return this;
816 unmodified lines
modified:package-lock.json+1 added-1 removed
2274 unmodified lines
2275 },2276 "dependencies": {2277 "minimist": {2278 "version": "1.2.0",2279 "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",2279 "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",2280 "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=",2281 "dev": true2282 }2283 }
207 unmodified lines
modified:Readme.md+2 added-2 removed
341 unmodified lines
342343An application for pizzas ordering344345Options:346 -h, --help output usage information346 -V, --version output the version number347 -p, --peppers Add peppers348 -P, --pineapple Add pineapple349 -b, --bbq Add bbq sauce351 -c, --cheese <type> Add the specified type of cheese [marble]350 -c, --cheese <type> Add the specified type of cheese (default: "marble")351 -C, --no-cheese You do not want any cheese352 -h, --help output usage information353```354355## Custom help356
167 unmodified lines