From a43f6f6e462e244e08140d4174673ce1cd8c24d8 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Fri, 20 Oct 2023 13:14:28 -0700 Subject: [PATCH] Made all flags case insensitive --- core/flags.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/core/flags.ts b/core/flags.ts index 3311413..8cb4e52 100644 --- a/core/flags.ts +++ b/core/flags.ts @@ -91,7 +91,7 @@ export async function parseFlags(flags: T, data?: {}): Promise< Object.entries(flags).forEach(([flag, options]) => { if (options.alias) { - aliases[flag] = [options.alias]; + aliases[flag] = [options.alias.toLowerCase()]; } if (options.type === Boolean) { @@ -106,8 +106,12 @@ export async function parseFlags(flags: T, data?: {}): Promise< boolean: booleans, }); + const caseInsensitiveResults = Object.fromEntries( + Object.entries(results).map(([key, value]) => [convertArgToCamelCase(key), value]), + ); + const defaultResults = getFlagDefaults(flags); - const validatedResults = await validateFlagInputs(flags, results); + const validatedResults = await validateFlagInputs(flags, caseInsensitiveResults); const finalResults = { ...defaultResults, ...validatedResults }; // If `data` is provided (in other words, if the flow is programmatic rather @@ -183,3 +187,14 @@ async function validateFlagInputs(flags: T, inputs: {} = {}) { ), ); } + +function convertArgToCamelCase(arg: string) { + const tokens = arg.split('-'); + return tokens.reduce((camelCase, token, index) => { + if (index === 0) { + return token.toLowerCase(); + } + + return camelCase + token.charAt(0).toUpperCase() + token.slice(1).toLowerCase(); + }, ''); +}