From 5cbdf30501c56c43aeee34d76ddf5ffeea40fdfe Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 30 Sep 2024 11:35:52 +0530 Subject: [PATCH] create getOptionName and getOptionAliases also fixed a logical error where the default help option would accidentally clear a few fields --- tribes-cli/utils/parsing.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tribes-cli/utils/parsing.ts b/tribes-cli/utils/parsing.ts index 3a1c6bea..0f96a2a8 100644 --- a/tribes-cli/utils/parsing.ts +++ b/tribes-cli/utils/parsing.ts @@ -8,21 +8,31 @@ export interface ParsedSubcommand { default: Record; } -export const defaultParsedSubcommand = (): ParsedSubcommand => { +export const defaultParsedCommand = (): ParsedSubcommand => { return { string: [], - boolean: [], - alias: {}, + boolean: ['help'], + alias: { help: ['h'] }, default: {}, }; }; -export const parseSubcommand = (command: Command, existing?: Partial): ParsedSubcommand => { - const res = Object.assign(defaultParsedSubcommand(), existing); +const getOptionAliases = (fmt: string) => { + const split = fmt.split(' ').toSorted((a, b) => b.length - a.length).map(cleanArg); + return split; +}; + +export const getOptionName = (fmt: string) => { + const split = getOptionAliases(fmt); + return split[0]; +}; + +export const parseCommand = (command: Command, existing?: Partial): ParsedSubcommand => { + const res = Object.assign(defaultParsedCommand(), existing); if (command.options) { for (const option in command.options) { - const split = option.split(' ').toSorted((a, b) => b.length - a.length).map(cleanArg); + const split = getOptionAliases(option); const name = split[0]; const body = command.options[option]; if ('bool' in body) { @@ -36,7 +46,7 @@ export const parseSubcommand = (command: Command, existing?: Partial