( args: Record<string, unknown>, options: CliOptions, )
| 37 | * IMPORTANT: keep getPossibleFlagMetrics() in sync with this function. |
| 38 | */ |
| 39 | export function computeFlagUsage( |
| 40 | args: Record<string, unknown>, |
| 41 | options: CliOptions, |
| 42 | ): FlagUsage { |
| 43 | const usage: FlagUsage = {}; |
| 44 | |
| 45 | for (const [flagName, config] of Object.entries(options)) { |
| 46 | const value = args[flagName]; |
| 47 | const snakeCaseName = stripUnderscoreBeforeNumber(toSnakeCase(flagName)); |
| 48 | |
| 49 | // If there isn't a default value provided for the flag, |
| 50 | // we're going to log whether it's present on the args user |
| 51 | // provided or not. If there is a default value, we only log presence |
| 52 | // if the value differs from the default, implying explicit user intent. |
| 53 | if (!('default' in config) || value !== config.default) { |
| 54 | usage[`${snakeCaseName}_present`] = value !== undefined && value !== null; |
| 55 | } |
| 56 | |
| 57 | if (config.type === 'boolean' && typeof value === 'boolean') { |
| 58 | // For boolean options, we're going to log the value directly. |
| 59 | usage[snakeCaseName] = value; |
| 60 | } else if ( |
| 61 | config.type === 'string' && |
| 62 | typeof value === 'string' && |
| 63 | 'choices' in config && |
| 64 | config.choices |
| 65 | ) { |
| 66 | usage[snakeCaseName] = formatEnumChoice(snakeCaseName, value); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return usage; |
| 71 | } |
| 72 | |
| 73 | export interface FlagMetric { |
| 74 | name: string; |
no test coverage detected