Addition: otps.addFlagShorthandsAsAliases -> Automatically adds shorthands into aliases, so --f resolves to --flag like -f would (with the shorthand configured as -f)

This commit is contained in:
Oxtaly 2025-05-11 22:32:00 +02:00
parent 815bb52510
commit 4876804e6c
2 changed files with 11 additions and 0 deletions

View File

@ -121,6 +121,7 @@ function validateAndFillDefaults(opts) {
validateOrDefaultIfUnset(opts, 'resolveFlagValueTypes', 'boolean', true); validateOrDefaultIfUnset(opts, 'resolveFlagValueTypes', 'boolean', true);
validateOrDefaultIfUnset(opts, 'allowNullDefaultFlagValues', 'boolean', true); validateOrDefaultIfUnset(opts, 'allowNullDefaultFlagValues', 'boolean', true);
validateOrDefaultIfUnset(opts, 'handleBackslashesForSpecialFlagChars', 'boolean', true); validateOrDefaultIfUnset(opts, 'handleBackslashesForSpecialFlagChars', 'boolean', true);
validateOrDefaultIfUnset(opts, 'addFlagShorthandsAsAliases', 'boolean', true);
validateOrDefaultIfUnset(opts, 'automaticBooleanFlagNegation', 'object', {}); validateOrDefaultIfUnset(opts, 'automaticBooleanFlagNegation', 'object', {});
@ -699,6 +700,14 @@ function parser(argv, opts) {
expectType('opts', 'object', opts, false, false); expectType('opts', 'object', opts, false, false);
/** @throwable Will throw if anything is wrong */ /** @throwable Will throw if anything is wrong */
validateAndFillDefaults(opts); validateAndFillDefaults(opts);
if(opts.addFlagShorthandsAsAliases) {
Object.values(opts.flags).forEach((flagConfig) => {
if(flagConfig.shorthands.length >= 1)
/// @ts-ignore
flagConfig.aliases.push(...flagConfig.shorthands)
})
}
const allShorthandFlags = getAllShorthandFlags(opts); const allShorthandFlags = getAllShorthandFlags(opts);

2
types.d.ts vendored
View File

@ -82,6 +82,8 @@ export interface ParserOpts<Flags extends FlagsI> {
* @default true * @default true
*/ */
handleBackslashesForSpecialFlagChars?: boolean handleBackslashesForSpecialFlagChars?: boolean
/** If enabled, automatically adds shorthands as valid flag aliases (eg: -f &-> --f) @default true */
addFlagShorthandsAsAliases?: boolean
} }
function __type__getType(e: any) { return typeof e }; function __type__getType(e: any) { return typeof e };