Skip to content
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
13 changes: 6 additions & 7 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,12 +780,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

setOptionValue(key, value) {
if (this._storeOptionsAsProperties) {
this[key] = value;
} else {
this._optionValues[key] = value;
}
return this;
return this.setOptionValueWithSource(key, value, undefined);
}

/**
Expand All @@ -798,7 +793,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

setOptionValueWithSource(key, value, source) {
this.setOptionValue(key, value);
if (this._storeOptionsAsProperties) {
this[key] = value;
} else {
this._optionValues[key] = value;
}
this._optionValueSources[key] = source;
return this;
}
Expand Down
11 changes: 10 additions & 1 deletion tests/options.getset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('when setOptionValueWithSource then value returned by opts', () => {
const cheeseValue = 'blue';
program
.option('--cheese [type]', 'cheese type')
.setOptionValue('cheese', cheeseValue);
.setOptionValueWithSource('cheese', cheeseValue, 'cli');
expect(program.opts().cheese).toBe(cheeseValue);
});

Expand Down Expand Up @@ -57,3 +57,12 @@ test('when option value parsed from cli then option source is cli', () => {
program.parse(['--foo'], { from: 'user' });
expect(program.getOptionValueSource('foo')).toBe('cli');
});

test('when setOptionValue then clears previous source', () => {
const program = new commander.Command();
program
.option('--foo', 'description', 'default value');
expect(program.getOptionValueSource('foo')).toBe('default');
program.setOptionValue('foo', 'bar');
expect(program.getOptionValueSource('foo')).toBeUndefined();
});