Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes behavior of --no-* options #795

Merged
merged 1 commit into from Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Readme.md
Expand Up @@ -343,13 +343,13 @@ Usage: pizza [options]
An application for pizzas ordering

Options:
-h, --help output usage information
-V, --version output the version number
-p, --peppers Add peppers
-P, --pineapple Add pineapple
-b, --bbq Add bbq sauce
-c, --cheese <type> Add the specified type of cheese [marble]
-c, --cheese <type> Add the specified type of cheese (default: "marble")
-C, --no-cheese You do not want any cheese
-h, --help output usage information
```

## Custom help
Expand Down
6 changes: 2 additions & 4 deletions examples/pizza
Expand Up @@ -12,7 +12,7 @@ program
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq', 'Add bbq sauce')
.option('-c, --cheese <type>', 'Add the specified type of cheese [marble]')
.option('-c, --cheese <type>', 'Add the specified type of cheese', 'marble')
.option('-C, --no-cheese', 'You do not want any cheese')
.parse(process.argv);

Expand All @@ -21,9 +21,7 @@ if (program.peppers) console.log(' - peppers');
if (program.pineapple) console.log(' - pineapple');
if (program.bbq) console.log(' - bbq');

var cheese = true === program.cheese
? 'marble'
: program.cheese || 'no';
var cheese = !program.cheese ? 'no' : program.cheese;

console.log(' - %s cheese', cheese);
console.log(program.args);
24 changes: 14 additions & 10 deletions index.js
Expand Up @@ -60,9 +60,7 @@ function Option(flags, description) {
*/

Option.prototype.name = function() {
return this.long
.replace('--', '')
.replace('no-', '');
return this.long.replace(/^--/, '');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This may be a breaking change for people who use the (only recently-documented) Custom Event Listeners, though I can also clarify this by adding another illustrative example to the Readme in that section, if required.

};

/**
Expand All @@ -74,7 +72,7 @@ Option.prototype.name = function() {
*/

Option.prototype.attributeName = function() {
return camelcase(this.name());
return camelcase(this.name().replace(/^no-/, ''));
};

/**
Expand Down Expand Up @@ -335,14 +333,17 @@ Command.prototype.action = function(fn) {
*
* Examples:
*
* // simple boolean defaulting to false
* // simple boolean defaulting to undefined
* program.option('-p, --pepper', 'add pepper');
*
* program.pepper
* // => undefined
*
* --pepper
* program.pepper
* // => Boolean
* // => true
*
* // simple boolean defaulting to true
* // simple boolean defaulting to true (unless non-negated option is also defined)
* program.option('-C, --no-cheese', 'remove cheese');
*
* program.cheese
Expand Down Expand Up @@ -394,8 +395,11 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {

// preassign default value only for --no-*, [optional], or <required>
if (!option.bool || option.optional || option.required) {
// when --no-* we make sure default is true
if (!option.bool) defaultValue = true;
// when --no-foo we make sure default is true, unless a --foo option is already defined
if (!option.bool) {
var opts = self.opts();
defaultValue = Object.prototype.hasOwnProperty.call(opts, name) ? opts[name] : true;
}
// preassign only if we have a default
if (defaultValue !== undefined) {
self[name] = defaultValue;
Expand Down Expand Up @@ -426,7 +430,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
}
} else if (val !== null) {
// reassign
self[name] = val;
self[name] = option.bool ? val : false;
}
});

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions test/test.options.bool.no.js
Expand Up @@ -7,9 +7,25 @@ var program = require('../')

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();
23 changes: 23 additions & 0 deletions test/test.options.defaults.no.js
@@ -0,0 +1,23 @@
/**
* 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();