Skip to content

Commit

Permalink
Add implied to typedef and precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 31, 2022
1 parent 82fcb98 commit 8bea61c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
39 changes: 30 additions & 9 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class Command extends EventEmitter {
this._showHelpAfterError = false;
this._showSuggestionAfterError = true;

// 'unknown' value is left out on purpose, should be treated as such
this._precedence = [
'default',
'implied', // implied by others configs
'config', // placeholder for configuration files
'env',
'cli'
];

// see .configureOutput() for docs
this._outputConfiguration = {
writeOut: (str) => process.stdout.write(str),
Expand Down Expand Up @@ -780,11 +789,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

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

Expand All @@ -797,8 +802,12 @@ Expecting one of '${allowedValues.join("', '")}'`);
* @return {Command} `this` command for chaining
*/

setOptionValueWithSource(key, value, source) {
this.setOptionValue(key, value);
setOptionValueWithSource(key, value, source = 'unknown') {
if (this._storeOptionsAsProperties) {
this[key] = value;
} else {
this._optionValues[key] = value;
}
this._optionValueSources[key] = source;
return this;
}
Expand Down Expand Up @@ -1584,7 +1593,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (option.envVar && option.envVar in process.env) {
const optionKey = option.attributeName();
// Priority check. Do not overwrite cli or options from unknown source (client-code).
if (this.getOptionValue(optionKey) === undefined || ['default', 'config', 'env'].includes(this.getOptionValueSource(optionKey))) {
if (this.getOptionValue(optionKey) === undefined || this._isNewSourcePreferred('env', this.getOptionValueSource(optionKey))) {
if (option.required || option.optional) { // option can take a value
// keep very simple, optional always takes value
this.emit(`optionEnv:${option.name()}`, process.env[option.envVar]);
Expand All @@ -1597,6 +1606,18 @@ Expecting one of '${allowedValues.join("', '")}'`);
});
}

/**
* Check if newSource is preferred.
*
* @api private
*/
_isNewSourcePreferred(newSource, existingSource) {
// unknown source should always take precedence. undefined existingSource means option not set.
if (newSource == null || !this._precedence.includes(newSource) || existingSource === undefined) return true;
if (existingSource === null || !this._precedence.includes(existingSource)) return false;
return this._precedence.indexOf(newSource) >= this._precedence.indexOf(existingSource);
}

/**
* Apply any implied option values, if option is undefined or default value.
*
Expand All @@ -1605,7 +1626,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
_parseOptionsImplied() {
const dualHelper = new DualOptions(this.options);
const hasCustomOptionValue = (optionKey) => {
return this.getOptionValue(optionKey) !== undefined && !['default', 'implied'].includes(this.getOptionValueSource(optionKey));
return this.getOptionValue(optionKey) !== undefined && !this._isNewSourcePreferred('implied', this.getOptionValueSource(optionKey));
};
this.options
.filter(option => (option.implied !== undefined) &&
Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export interface OutputConfiguration {

export type AddHelpTextPosition = 'beforeAll' | 'before' | 'after' | 'afterAll';
export type HookEvent = 'preSubcommand' | 'preAction' | 'postAction';
export type OptionValueSource = 'default' | 'env' | 'config' | 'cli';
export type OptionValueSource = 'default' | 'implied' | 'config' | 'env' | 'cli' | 'unknown';

export interface OptionValues {
[key: string]: any;
Expand Down

0 comments on commit 8bea61c

Please sign in to comment.