diff --git a/README.md b/README.md index 9982bf1..5a768ff 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - Fast and lightweight argument parser based on [mri](https://github.com/lukeed/mri) - Smart value parsing with typecast, boolean shortcuts and unknown flag handling - Nested sub-commands -- Lazy and Async commands +- Lazy and Async commands, caching async results - Plugable and composable API - Auto generated usage and help diff --git a/src/_utils.ts b/src/_utils.ts index f92a648..757f377 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -25,8 +25,18 @@ export function formatLineColumns(lines: string[][], linePrefix = "") { .join("\n"); } -export function resolveValue(input: Resolvable): T | Promise { - return typeof input === "function" ? (input as any)() : input; +const cache = new WeakMap(); + +export async function resolveValue(input: Resolvable): Promise { + if (typeof input === 'function') { + let result: T = cache.get(input); + if (!result) { + result = await (input as any)(); + cache.set(input, result); + } + return result; + } + return input; } export class CLIError extends Error {