Skip to content

Commit f4c7349

Browse files
authored
Assume a string parameter to implies is name of boolean option. (#1854)
1 parent 869c3c0 commit f4c7349

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

examples/options-implies.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const { Command, Option } = require('../'); // include commander in git clone of commander repo
44
const program = new Command();
55

6-
// You can use .conflicts() with a single string, which is the camel-case name of the conflicting option.
76
program
87
.addOption(new Option('--quiet').implies({ logLevel: 'off' }))
98
.addOption(new Option('--log-level <level>').choices(['info', 'warning', 'error', 'off']).default('info'))

lib/option.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ class Option {
9999
* @return {Option}
100100
*/
101101
implies(impliedOptionValues) {
102-
this.implied = Object.assign(this.implied || {}, impliedOptionValues);
102+
let newImplied = impliedOptionValues;
103+
if (typeof impliedOptionValues === 'string') {
104+
// string is not documented, but easy mistake and we can do what user probably intended.
105+
newImplied = { [impliedOptionValues]: true };
106+
}
107+
this.implied = Object.assign(this.implied || {}, newImplied);
103108
return this;
104109
}
105110

tests/options.implies.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,14 @@ test('when implied option has custom processing then custom processing not calle
201201
expect(program.opts().bar).toEqual(true);
202202
expect(called).toEqual(false);
203203
});
204+
205+
test('when passed string then treat as boolean', () => {
206+
// Do something sensible instead of something silly if user passes just name of option.
207+
// https://github.com/tj/commander.js/issues/1852
208+
const program = new Command();
209+
program
210+
.addOption(new Option('--foo').implies('bar'))
211+
.option('-b, --bar', 'description');
212+
program.parse(['--foo'], { from: 'user' });
213+
expect(program.opts().bar).toEqual(true);
214+
});

0 commit comments

Comments
 (0)