Skip to content

Commit

Permalink
Add implied to option value sources (#1794)
Browse files Browse the repository at this point in the history
* TypeScript: add implied to OptionValueSource

* Add implied to JSDoc and TSDoc. Mention config in env priority.

* TypeScript: add undefined to return type of getOptionValueSource

* Reword env description so not  priority ordering as such. More accurately describe behaviour.
  • Loading branch information
shadowspawn committed Sep 6, 2022
1 parent 84d9201 commit 72be61c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/command.js
Expand Up @@ -37,7 +37,7 @@ class Command extends EventEmitter {
this._scriptPath = null;
this._name = name || '';
this._optionValues = {};
this._optionValueSources = {}; // default < config < env < cli
this._optionValueSources = {}; // default, env, cli etc
this._storeOptionsAsProperties = false;
this._actionHandler = null;
this._executableHandler = false;
Expand Down Expand Up @@ -784,11 +784,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
}

/**
* Store option value and where the value came from.
* Store option value and where the value came from.
*
* @param {string} key
* @param {Object} value
* @param {string} source - expected values are default/config/env/cli
* @param {string} source - expected values are default/config/env/cli/implied
* @return {Command} `this` command for chaining
*/

Expand All @@ -804,7 +804,7 @@ Expecting one of '${allowedValues.join("', '")}'`);

/**
* Get source of option value.
* Expected values are default | config | env | cli
* Expected values are default | config | env | cli | implied
*
* @param {string} key
* @return {string}
Expand Down
4 changes: 3 additions & 1 deletion lib/option.js
Expand Up @@ -105,7 +105,9 @@ class Option {

/**
* Set environment variable to check for option value.
* Priority order of option values is default < env < cli
*
* An environment variable is only used if when processed the current option value is
* undefined, or the source of the current value is 'default' or 'config' or 'env'.
*
* @param {string} name
* @return {Option}
Expand Down
8 changes: 5 additions & 3 deletions typings/index.d.ts
Expand Up @@ -144,7 +144,9 @@ export class Option {

/**
* Set environment variable to check for option value.
* Priority order of option values is default < env < cli
*
* An environment variables is only used if when processed the current option value is
* undefined, or the source of the current value is 'default' or 'config' or 'env'.
*/
env(name: string): this;

Expand Down Expand Up @@ -266,7 +268,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' | 'config' | 'env' | 'cli' | 'implied';

export interface OptionValues {
[key: string]: any;
Expand Down Expand Up @@ -595,7 +597,7 @@ export class Command {
/**
* Retrieve option value source.
*/
getOptionValueSource(key: string): OptionValueSource;
getOptionValueSource(key: string): OptionValueSource | undefined;

/**
* Alter parsing of short flags with optional values.
Expand Down
2 changes: 1 addition & 1 deletion typings/index.test-d.ts
Expand Up @@ -172,7 +172,7 @@ expectType<commander.Command>(program.setOptionValue('example', true));
expectType<commander.Command>(program.setOptionValueWithSource('example', [], 'cli'));

// getOptionValueSource
expectType<commander.OptionValueSource>(program.getOptionValueSource('example'));
expectType<commander.OptionValueSource | undefined>(program.getOptionValueSource('example'));

// combineFlagAndOptionalValue
expectType<commander.Command>(program.combineFlagAndOptionalValue());
Expand Down

0 comments on commit 72be61c

Please sign in to comment.