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

Assume a string parameter to implies is name of boolean option. #1854

Merged
merged 1 commit into from
Mar 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/options-implies.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
});