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

setOptionValue should clear option source #1795

Merged
merged 2 commits into from Sep 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/command.js
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
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();
});