Skip to content

Commit

Permalink
Assume a string parameter to implies is name of boolean option. (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Mar 15, 2023
1 parent 869c3c0 commit f4c7349
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 0 additions & 1 deletion examples/options-implies.js
Expand Up @@ -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 <level>').choices(['info', 'warning', 'error', 'off']).default('info'))
Expand Down
7 changes: 6 additions & 1 deletion lib/option.js
Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/options.implies.test.js
Expand Up @@ -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);
});

0 comments on commit f4c7349

Please sign in to comment.