diff --git a/lib/option.js b/lib/option.js index bded840ae..40767e9cf 100644 --- a/lib/option.js +++ b/lib/option.js @@ -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()); } /** diff --git a/tests/options.bool.test.js b/tests/options.bool.test.js index 138debfc4..ad5ca65da 100644 --- a/tests/options.bool.test.js +++ b/tests/options.bool.test.js @@ -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'); @@ -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 @@ -88,6 +109,44 @@ describe('boolean flag on command', () => { }); }); +describe('overridden negatable boolean flag', () => { + test('when negatable boolean flag is unspecified 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']); + expect(program.opts().cheese).toBeUndefined(); + }); + + 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.