Merge branch 'fix---no-logic' of https://github.com/usmonster/commander.js into usmonster-fix---no-logic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by John Gee · parents c13988c, 26594fb · 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
Provenance
- Commit
19f52c2- Tree
3b509cf- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
c13988c- Parent 2
26594fb
6 files changed+60 added−19 removed
modified:examples/pizza+2 added-4 removed
10 unmodified lines11 .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);18 19 console.log('you ordered a pizza with:');20 if (program.peppers) console.log(' - peppers');21 if (program.pineapple) console.log(' - pineapple');22 if (program.bbq) console.log(' - bbq');23 24-var cheese = true === program.cheese25- ? 'marble'26- : program.cheese || 'no';24+var cheese = !program.cheese ? 'no' : program.cheese;25 26 console.log(' - %s cheese', cheese);27 console.log(program.args);modified:test/test.options.bool.no.js+18 added-2 removed
5 unmodified lines67891011121314131415161718192021222324252627282930315 unmodified lines , should = require('should');
program .version('0.0.1') .option('-e, --everything', 'add all of the toppings') .option('-p, --pepper', 'add pepper') .option('-P, --no-pepper', 'remove pepper') .option('-c|--no-cheese', 'remove cheese');
program.parse(['node', 'test', '--no-cheese']);should.equal(undefined, program.pepper);program.parse(['node', 'test']);program.should.not.have.property('everything');program.should.not.have.property('pepper');program.cheese.should.be.true();
program.parse(['node', 'test', '--everything']);program.everything.should.be.true();program.should.not.have.property('pepper');program.cheese.should.be.true();
program.parse(['node', 'test', '--pepper']);program.pepper.should.be.true();program.cheese.should.be.true();
program.parse(['node', 'test', '--everything', '--no-pepper', '--no-cheese']);program.pepper.should.be.false();program.cheese.should.be.false();added:test/test.options.defaults.no.js+23 added
1234567891011121314151617181920212223/** * Module dependencies. */
var program = require('../') , should = require('should');
program .version('0.0.1') .option('-p, --pepper [type]', 'add pepper', 'red') .option('-P, --no-pepper', 'remove pepper');
program.parse(['node', 'test']);program.pepper.should.equal('red');
program.parse(['node', 'test', '--pepper']);program.pepper.should.equal('red');
program.parse(['node', 'test', '--pepper', 'jalapeño']);program.pepper.should.equal('jalapeño');
program.parse(['node', 'test', '--no-pepper']);program.pepper.should.be.false();modified:index.js+14 added-10 removed
58 unmodified lines59 * @api private60 */61 62 Option.prototype.name = function() {63- return this.long64- .replace('--', '')65- .replace('no-', '');63+ return this.long.replace(/^--/, '');64 };65 66 /**67 * Return option name, in a camelcase format that can be used3 unmodified lines71 * @api private72 */73 74 Option.prototype.attributeName = function() {77- return camelcase(this.name());75+ return camelcase(this.name().replace(/^no-/, ''));76 };77 78 /**79 * Check if `arg` matches the short or long flag.261 unmodified lines341 * "-p --pepper"342 *343 * Examples:344 *347- * // simple boolean defaulting to false345+ * // simple boolean defaulting to undefined346 * program.option('-p, --pepper', 'add pepper');347 *348+ * program.pepper349+ * // => undefined350+ *351 * --pepper352 * program.pepper352- * // => Boolean353+ * // => true354 *354- * // simple boolean defaulting to true355+ * // simple boolean defaulting to true (unless non-negated option is also defined)356 * program.option('-C, --no-cheese', 'remove cheese');357 *358 * program.cheese359 * // => true43 unmodified lines403 }404 405 // preassign default value only for --no-*, [optional], or <required>406 if (!option.bool || option.optional || option.required) {406- // when --no-* we make sure default is true407- if (!option.bool) defaultValue = true;407+ // when --no-foo we make sure default is true, unless a --foo option is already defined408+ if (!option.bool) {409+ var opts = self.opts();410+ defaultValue = Object.prototype.hasOwnProperty.call(opts, name) ? opts[name] : true;411+ }412 // preassign only if we have a default413 if (defaultValue !== undefined) {414 self[name] = defaultValue;415 option.defaultValue = defaultValue;22 unmodified lines438 self[name] = val;439 }440 } else if (val !== null) {441 // reassign438- self[name] = val;442+ self[name] = option.bool ? val : false;443 }444 });445 446 return this;849 unmodified linesmodified:package-lock.json+1 added-1 removed
2274 unmodified lines2275 },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 linesmodified:Readme.md+2 added-2 removed
340 unmodified lines341 342 An application for pizzas ordering343 344 Options:345- -h, --help output usage information345 -V, --version output the version number346 -p, --peppers Add peppers347 -P, --pineapple Add pineapple348 -b, --bbq Add bbq sauce350- -c, --cheese <type> Add the specified type of cheese [marble]349+ -c, --cheese <type> Add the specified type of cheese (default: "marble")350 -C, --no-cheese You do not want any cheese351+ -h, --help output usage information352 ```353 354 ## Custom help355 176 unmodified lines