Skip to content

Commit

Permalink
Make Option.env() behave as getter when no name supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
aweebit committed Aug 15, 2023
1 parent 880b028 commit 8a050cc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ class Option {
}

/**
* Set environment variable to check for option value.
* Get or set environment variable to check for option value.
*
* 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}
* @param {string} [name]
* @return {Option | string | undefined}
*/

env(name) {
if (!name) return this.envVar;
this.envVar = name;
return this;
}
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ export class Option {
implies(optionValues: OptionValues): this;

/**
* Set environment variable to check for option value.
* Get or set environment variable to check for option value.
*
* 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;
env(): string | undefined;

/**
* Calculate the full description, including defaultValue etc.
Expand Down
1 change: 1 addition & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ expectType<commander.Option>(baseOption.preset('abc'));

// env
expectType<commander.Option>(baseOption.env('PORT'));
expectType<string | undefined>(baseOption.env());

// fullDescription
expectType<string>(baseOption.fullDescription());
Expand Down

0 comments on commit 8a050cc

Please sign in to comment.