From bf4206f181b525af9af100fdc03426c626e952ab Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Thu, 30 Nov 2023 18:49:52 -0300 Subject: [PATCH] fix: improve flag capture --- src/commandExecution.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/commandExecution.ts b/src/commandExecution.ts index dc262524..1edc3fe6 100644 --- a/src/commandExecution.ts +++ b/src/commandExecution.ts @@ -185,12 +185,33 @@ export class CommandExecution extends AsyncCreatable { } else if (argv.find((arg) => new RegExp(`^--${flagName}(=.*)?$`).test(arg))) { this.specifiedFlags.push(flagName); this.specifiedFlagFullNames.push(flagName); - } else if (flagDefinitions[flagName].deprecateAliases) { - // we can't find the flag as the key (long name) or short char, so it must be a deprecated flag + } else { + // we can't find the flag as the key (long name) or short char, so it must be an alias + + // get present flags in argv, that is everything starting with `-`, then strip dashes from it. + // e.g. ['-u', 'test', '--json'] -> [ 'u', undefined, 'json' ] const argvFlags = this.argv.map((a) => a.match(/-([a-zA-Z]+)/g)).map((a) => a?.[0].replace('-', '')); - this.deprecatedFlagsUsed.push(flagDefinitions[flagName].aliases?.find((a) => argvFlags.includes(a)) ?? ''); - this.specifiedFlagFullNames.push(flagName); - this.specifiedFlags.push(flagName); + + let possibleAliases = flagDefinitions[flagName].aliases ?? []; + + if (flagDefinitions[flagName].charAliases) { + possibleAliases = possibleAliases?.concat(flagDefinitions[flagName].charAliases as string[]); + } + + // if you have a flag that sets a default value and has aliases + // this check will ensure it only gets captured if the user specified using aliases + const specifiedFlag = possibleAliases.find((a) => argvFlags.includes(a)); + + if (specifiedFlag) { + // save flag with their original name instead of the typed alias + this.specifiedFlagFullNames.push(flagName); + this.specifiedFlags.push(flagName); + + // user typed a deprecated alias + if (flagDefinitions[flagName].deprecateAliases) { + this.deprecatedFlagsUsed.push(specifiedFlag); + } + } } }); }