From 49d74d9de5e8a7e1cd09c80d675ebf19a49a823d Mon Sep 17 00:00:00 2001 From: Oxtaly Date: Sun, 11 May 2025 16:41:12 +0200 Subject: [PATCH] Added opts.lowerCaseInputValues and opts.lowerCaseFlagValues options & refactored flag processing to no longer use temporary flagVal and flagKey vars --- index.js | 46 ++++++++++++++++++++++++---------------------- types.d.ts | 4 ++++ 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 918d2f5..19953b1 100644 --- a/index.js +++ b/index.js @@ -112,6 +112,8 @@ function validateAndFillDefaults(opts) { /** Category: @default false */ validateOrDefaultIfUnset(opts, 'allowSingularDashLongFlags', 'boolean', false); + validateOrDefaultIfUnset(opts, 'lowerCaseFlagValues', 'boolean', false); + validateOrDefaultIfUnset(opts, 'lowerCaseInputValues', 'boolean', false); /** Category: @default true */ validateOrDefaultIfUnset(opts, 'allowUnknownFlags', 'boolean', true); @@ -412,8 +414,6 @@ function getAllShorthandFlags(opts) { /** * @modifies flag - * And uh, yeah bit of a heavy refactor given it's a biggggg chicken and egg situation, where I need the type of the flag value (assigned or given or default), but to get the - * proper flag config, I first need to run the negated boolean flag, so uh yeah, need to run allat twice :D * ### To be run before any flag value assignments (flag.value always expected to be string) * @param {DefaultParserOptsType} opts * @param {InternalFlagsFlagObj} flag @@ -751,13 +751,20 @@ function parser(argv, opts) { if(['string', 'any', castableType].includes(flagType) || (castableType === 'number' && flagType === 'bigint')) flag.value = arg; - else - input.push(arg); + else { + if(opts.lowerCaseInputValues) + input.push(arg.toLowerCase()); + else + input.push(arg); + } lookingForFlagVal = false; return; } else { - input.push(arg); + if(opts.lowerCaseInputValues) + input.push(arg.toLowerCase()); + else + input.push(arg); return; } }) @@ -768,9 +775,6 @@ function parser(argv, opts) { /** Only on type flagVal */ runAutomaticBooleanFlagNegation(opts, flag); - /** Process some flag values */ - let { key: flagKey, value: flagVal } = flag; - /** Building return OBJ */ /** @readonly */ let flagConfig = flag.flagConfig @@ -782,52 +786,50 @@ function parser(argv, opts) { } ok(flagConfig !== null, 'flagConfig should always be undefined if not found, and would only be in the wrong state of null if not reassigned at a later point (which should be impossible'); - if(flagVal === UNSET_FLAG_VALUE) { + if(flag.value === UNSET_FLAG_VALUE) { let flagType = opts.defaultFlagType if(flagConfig !== undefined) { flagType = flagConfig.type; if(flagConfig.requiredValue === true) { const message = `Flag "${flagConfig.name}" is missing a required value of type "${flagConfig.type}"!`; const error = new InputError(message); - error.context = { flag: flagConfig, flagType: flagConfig.type, flagMatch: flagKey, value: UNSET_FLAG_VALUE, argv }; + error.context = { flag: flagConfig, flagType: flagConfig.type, flagMatch: flag.key, value: UNSET_FLAG_VALUE, argv }; return giveError(opts, { error, - message: message + `\n - Flag: "${flagConfig.name}", match: "${flagKey}", value: UNSET_FLAG_VALUE, type: "${flagConfig.type}"`, + message: message + `\n - Flag: "${flagConfig.name}", match: "${flag.key}", value: UNSET_FLAG_VALUE, type: "${flagConfig.type}"`, exitCode: 1 }); } /** Set default unless bool type */ if(typeof flagConfig.default !== 'undefined' && flagConfig.type !== 'boolean') - flagVal = flagConfig.default; + flag.value = flagConfig.default; } - if(flagVal === UNSET_FLAG_VALUE) { + if(flag.value === UNSET_FLAG_VALUE) { switch(flagType) { case 'any': case 'boolean': - flagVal = true; + flag.value = true; break; case "bigint": case "number": case "string": - flagVal = null; + flag.value = null; break; } } } - if(typeof flagVal === 'string') - flagVal = runFlagValueTypeResolving(opts, flagVal, flagConfig); - - - flag.key = flagKey; - flag.value = flagVal; + if(typeof flag.value === 'string') + flag.value = runFlagValueTypeResolving(opts, flag.value, flagConfig); + if(typeof flag.value === 'string' && opts.lowerCaseFlagValues) + flag.value = flag.value.toLowerCase() let realFlagKey = flag.key; if(flagConfig !== undefined) { realFlagKey = flagConfig.name; } else - unknownFlag(opts, flagKey, flagVal, argv); + unknownFlag(opts, flag.key, flag.value, argv); flagReturnObj[realFlagKey] = flag.value; }) diff --git a/types.d.ts b/types.d.ts index 71f34af..330cfa0 100644 --- a/types.d.ts +++ b/types.d.ts @@ -59,6 +59,10 @@ export interface ParserOpts { behaviorOnInputError?: 'throw' | 'log' | 'log&exit' | 'ignore'; /** Wether to allow default flag values set to null being valid @default true */ allowNullDefaultFlagValues?: boolean; + /** If enabled, makes all input values lowercase @default false */ + lowerCaseInputValues?: boolean + /** If enabled, makes all string flag values lowercase after flag processing @default false */ + lowerCaseFlagValues?: boolean } function __type__getType(e: any) { return typeof e };