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

Allow bypassing negation #2238

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ class Option {
*/

attributeName() {
return camelcase(this.name().replace(/^no-/, ''));
if (this.negate) {
return camelcase(this.name().replace(/^no-/, ''));
}
return camelcase(this.name());
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/options.bool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ describe('boolean flag on program', () => {
expect(program.opts().pepper).toBe(true);
});

test('when boolean flag specified then option should be camelcase', () => {
const program = new commander.Command();
program.option('--pepper-and-salt', 'add pepper and salt');
program.parse(['node', 'test', '--pepper-and-salt']);
expect(program.opts().pepperAndSalt).toBe(true);
});

test('when negatable boolean flag not specified then value is true', () => {
const program = new commander.Command();
program.option('--no-cheese', 'remove cheese');
Expand All @@ -31,6 +38,20 @@ describe('boolean flag on program', () => {
program.parse(['node', 'test', '--no-cheese']);
expect(program.opts().cheese).toBe(false);
});

test('when negatable boolean flag specified then negative value is undefined', () => {
const program = new commander.Command();
program.option('--no-cheese', 'remove cheese');
program.parse(['node', 'test', '--no-cheese']);
expect(program.opts().noCheese).toBeUndefined();
});

test('when negatable boolean flag specified then positive option is camelcase', () => {
const program = new commander.Command();
program.option('--no-cheese-or-wine', 'remove cheese or wine');
program.parse(['node', 'test']);
expect(program.opts().cheeseOrWine).toBe(true);
});
});

// boolean flag on command
Expand Down Expand Up @@ -88,6 +109,35 @@ describe('boolean flag on command', () => {
});
});

describe('overridden negatable boolean flag', () => {
test('when negatable boolean flag is specified and negate is overridden then negative value is true', () => {
const program = new commander.Command();
var option = program.createOption('--no-cheese', 'remove cheese');
option.negate = false;
program.addOption(option);
program.parse(['node', 'test', '--no-cheese']);
expect(program.opts().noCheese).toBe(true);
});

test('when negatable boolean flag is specified and negate is overridden then positive value is undefined', () => {
const program = new commander.Command();
var option = program.createOption('--no-cheese', 'remove cheese');
option.negate = false;
program.addOption(option);
program.parse(['node', 'test', '--no-cheese']);
expect(program.opts().cheese).toBeUndefined();
});

test('when negatable boolean flag is specified and negate is overridden then negative option should be camelcase', () => {
const program = new commander.Command();
var option = program.createOption('--no-cheese-or-wine', 'remove cheese');
option.negate = false;
program.addOption(option);
program.parse(['node', 'test', '--no-cheese-or-wine']);
expect(program.opts().noCheeseOrWine).toBe(true);
});
});

// boolean flag with non-boolean default
// NB: behaviour changed in Commander v9 to have default be default.
// These tests no longer match likely uses, but retained and updated to match current behaviour.
Expand Down