Added opts.lowerCaseInputValues and opts.lowerCaseFlagValues options & refactored flag processing to no longer use temporary flagVal and flagKey vars
This commit is contained in:
parent
63e0e6c06a
commit
49d74d9de5
40
index.js
40
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,12 +751,19 @@ function parser(argv, opts) {
|
||||
|
||||
if(['string', 'any', castableType].includes(flagType) || (castableType === 'number' && flagType === 'bigint'))
|
||||
flag.value = arg;
|
||||
else {
|
||||
if(opts.lowerCaseInputValues)
|
||||
input.push(arg.toLowerCase());
|
||||
else
|
||||
input.push(arg);
|
||||
}
|
||||
lookingForFlagVal = false;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
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;
|
||||
})
|
||||
|
||||
4
types.d.ts
vendored
4
types.d.ts
vendored
@ -59,6 +59,10 @@ export interface ParserOpts<Flags extends FlagsI> {
|
||||
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 };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user