From 8a050cca28dc504b1806b2938d6ada1e9cb7597e Mon Sep 17 00:00:00 2001 From: Wee Bit Date: Tue, 15 Aug 2023 03:16:54 +0200 Subject: [PATCH] Make Option.env() behave as getter when no name supplied --- lib/option.js | 7 ++++--- typings/index.d.ts | 3 ++- typings/index.test-d.ts | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/option.js b/lib/option.js index d61fc5f2f..c1e2ac83b 100644 --- a/lib/option.js +++ b/lib/option.js @@ -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; } diff --git a/typings/index.d.ts b/typings/index.d.ts index 695c3bd25..ea766a9a7 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -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. diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index 734036fad..de19d2685 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -410,6 +410,7 @@ expectType(baseOption.preset('abc')); // env expectType(baseOption.env('PORT')); +expectType(baseOption.env()); // fullDescription expectType(baseOption.fullDescription());