Skip to content

Commit

Permalink
setOptionValue should clear option source (#1795)
Browse files Browse the repository at this point in the history
* Have setOptionValue call setOptionValueWithSource instead of the other way around

* Add explicit undefined to pass  tests
  • Loading branch information
shadowspawn committed Sep 6, 2022
1 parent 82fcb98 commit 84d9201
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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();
});

0 comments on commit 84d9201

Please sign in to comment.