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
46
index.js
46
index.js
@ -112,6 +112,8 @@ function validateAndFillDefaults(opts) {
|
|||||||
|
|
||||||
/** Category: @default false */
|
/** Category: @default false */
|
||||||
validateOrDefaultIfUnset(opts, 'allowSingularDashLongFlags', 'boolean', false);
|
validateOrDefaultIfUnset(opts, 'allowSingularDashLongFlags', 'boolean', false);
|
||||||
|
validateOrDefaultIfUnset(opts, 'lowerCaseFlagValues', 'boolean', false);
|
||||||
|
validateOrDefaultIfUnset(opts, 'lowerCaseInputValues', 'boolean', false);
|
||||||
|
|
||||||
/** Category: @default true */
|
/** Category: @default true */
|
||||||
validateOrDefaultIfUnset(opts, 'allowUnknownFlags', 'boolean', true);
|
validateOrDefaultIfUnset(opts, 'allowUnknownFlags', 'boolean', true);
|
||||||
@ -412,8 +414,6 @@ function getAllShorthandFlags(opts) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @modifies flag
|
* @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)
|
* ### To be run before any flag value assignments (flag.value always expected to be string)
|
||||||
* @param {DefaultParserOptsType} opts
|
* @param {DefaultParserOptsType} opts
|
||||||
* @param {InternalFlagsFlagObj} flag
|
* @param {InternalFlagsFlagObj} flag
|
||||||
@ -751,13 +751,20 @@ function parser(argv, opts) {
|
|||||||
|
|
||||||
if(['string', 'any', castableType].includes(flagType) || (castableType === 'number' && flagType === 'bigint'))
|
if(['string', 'any', castableType].includes(flagType) || (castableType === 'number' && flagType === 'bigint'))
|
||||||
flag.value = arg;
|
flag.value = arg;
|
||||||
else
|
else {
|
||||||
input.push(arg);
|
if(opts.lowerCaseInputValues)
|
||||||
|
input.push(arg.toLowerCase());
|
||||||
|
else
|
||||||
|
input.push(arg);
|
||||||
|
}
|
||||||
lookingForFlagVal = false;
|
lookingForFlagVal = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
input.push(arg);
|
if(opts.lowerCaseInputValues)
|
||||||
|
input.push(arg.toLowerCase());
|
||||||
|
else
|
||||||
|
input.push(arg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -768,9 +775,6 @@ function parser(argv, opts) {
|
|||||||
/** Only on type flagVal */
|
/** Only on type flagVal */
|
||||||
runAutomaticBooleanFlagNegation(opts, flag);
|
runAutomaticBooleanFlagNegation(opts, flag);
|
||||||
|
|
||||||
/** Process some flag values */
|
|
||||||
let { key: flagKey, value: flagVal } = flag;
|
|
||||||
|
|
||||||
/** Building return OBJ */
|
/** Building return OBJ */
|
||||||
/** @readonly */
|
/** @readonly */
|
||||||
let flagConfig = flag.flagConfig
|
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');
|
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
|
let flagType = opts.defaultFlagType
|
||||||
if(flagConfig !== undefined) {
|
if(flagConfig !== undefined) {
|
||||||
flagType = flagConfig.type;
|
flagType = flagConfig.type;
|
||||||
if(flagConfig.requiredValue === true) {
|
if(flagConfig.requiredValue === true) {
|
||||||
const message = `Flag "${flagConfig.name}" is missing a required value of type "${flagConfig.type}"!`;
|
const message = `Flag "${flagConfig.name}" is missing a required value of type "${flagConfig.type}"!`;
|
||||||
const error = new InputError(message);
|
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, {
|
return giveError(opts, {
|
||||||
error,
|
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
|
exitCode: 1
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/** Set default unless bool type */
|
/** Set default unless bool type */
|
||||||
if(typeof flagConfig.default !== 'undefined' && flagConfig.type !== 'boolean')
|
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) {
|
switch(flagType) {
|
||||||
case 'any':
|
case 'any':
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
flagVal = true;
|
flag.value = true;
|
||||||
break;
|
break;
|
||||||
case "bigint":
|
case "bigint":
|
||||||
case "number":
|
case "number":
|
||||||
case "string":
|
case "string":
|
||||||
flagVal = null;
|
flag.value = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof flagVal === 'string')
|
if(typeof flag.value === 'string')
|
||||||
flagVal = runFlagValueTypeResolving(opts, flagVal, flagConfig);
|
flag.value = runFlagValueTypeResolving(opts, flag.value, flagConfig);
|
||||||
|
if(typeof flag.value === 'string' && opts.lowerCaseFlagValues)
|
||||||
|
flag.value = flag.value.toLowerCase()
|
||||||
flag.key = flagKey;
|
|
||||||
flag.value = flagVal;
|
|
||||||
|
|
||||||
let realFlagKey = flag.key;
|
let realFlagKey = flag.key;
|
||||||
if(flagConfig !== undefined) {
|
if(flagConfig !== undefined) {
|
||||||
realFlagKey = flagConfig.name;
|
realFlagKey = flagConfig.name;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
unknownFlag(opts, flagKey, flagVal, argv);
|
unknownFlag(opts, flag.key, flag.value, argv);
|
||||||
|
|
||||||
flagReturnObj[realFlagKey] = flag.value;
|
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';
|
behaviorOnInputError?: 'throw' | 'log' | 'log&exit' | 'ignore';
|
||||||
/** Wether to allow default flag values set to null being valid @default true */
|
/** Wether to allow default flag values set to null being valid @default true */
|
||||||
allowNullDefaultFlagValues?: boolean;
|
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 };
|
function __type__getType(e: any) { return typeof e };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user