Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Made all flags case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaKulkarni committed Oct 20, 2023
1 parent 8027296 commit a43f6f6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions core/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function parseFlags<T extends Flags>(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) {
Expand All @@ -106,8 +106,12 @@ export async function parseFlags<T extends Flags>(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
Expand Down Expand Up @@ -183,3 +187,14 @@ async function validateFlagInputs<T extends Flags>(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();
}, '');
}

0 comments on commit a43f6f6

Please sign in to comment.