Skip to content

Commit

Permalink
fix: improve flag capture
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiand391 committed Nov 30, 2023
1 parent 4c6d3cf commit bf4206f
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/commandExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
});
}
Expand Down

0 comments on commit bf4206f

Please sign in to comment.