diff --git a/examples/options-implies.js b/examples/options-implies.js index af1912817..1633ff1cf 100644 --- a/examples/options-implies.js +++ b/examples/options-implies.js @@ -3,7 +3,6 @@ const { Command, Option } = require('../'); // include commander in git clone of commander repo const program = new Command(); -// You can use .conflicts() with a single string, which is the camel-case name of the conflicting option. program .addOption(new Option('--quiet').implies({ logLevel: 'off' })) .addOption(new Option('--log-level ').choices(['info', 'warning', 'error', 'off']).default('info')) diff --git a/lib/option.js b/lib/option.js index d8a9a150d..d61fc5f2f 100644 --- a/lib/option.js +++ b/lib/option.js @@ -99,7 +99,12 @@ class Option { * @return {Option} */ implies(impliedOptionValues) { - this.implied = Object.assign(this.implied || {}, impliedOptionValues); + let newImplied = impliedOptionValues; + if (typeof impliedOptionValues === 'string') { + // string is not documented, but easy mistake and we can do what user probably intended. + newImplied = { [impliedOptionValues]: true }; + } + this.implied = Object.assign(this.implied || {}, newImplied); return this; } diff --git a/tests/options.implies.test.js b/tests/options.implies.test.js index 005c648a0..9fe18e2d0 100644 --- a/tests/options.implies.test.js +++ b/tests/options.implies.test.js @@ -201,3 +201,14 @@ test('when implied option has custom processing then custom processing not calle expect(program.opts().bar).toEqual(true); expect(called).toEqual(false); }); + +test('when passed string then treat as boolean', () => { + // Do something sensible instead of something silly if user passes just name of option. + // https://github.com/tj/commander.js/issues/1852 + const program = new Command(); + program + .addOption(new Option('--foo').implies('bar')) + .option('-b, --bar', 'description'); + program.parse(['--foo'], { from: 'user' }); + expect(program.opts().bar).toEqual(true); +});