Addition: flag.acceptsNaturalValue (default behavior unchanged)

This commit is contained in:
Oxtaly 2025-05-14 02:57:43 +02:00
parent 5533706b30
commit 1b9b9e1312
2 changed files with 12 additions and 3 deletions

View File

@ -277,6 +277,11 @@ function validateAndFillDefaults(opts) {
flagData.requiredValue = false;
if(typeof flagData.requiredValue !== 'boolean')
throw new TypeError(`flag["${flagName}"].requiredValue must be type boolean | undefined!`);
if(typeof flagData.acceptsNaturalValue === 'undefined')
flagData.acceptsNaturalValue = true;
if(typeof flagData.acceptsNaturalValue !== 'boolean')
throw new TypeError(`flag["${flagName}"].acceptsNaturalValue must be type boolean | undefined!`);
})
}
@ -926,7 +931,8 @@ function parser(argv, opts) {
continue;
}
naturalFlagAssignment = { isLookingForValue: true, requiredType: flag.type };
if(flag.config === undefined || flag.config.acceptsNaturalValue)
naturalFlagAssignment = { isLookingForValue: true, requiredType: flag.type };
continue;
}
@ -952,7 +958,8 @@ function parser(argv, opts) {
}
flags.push(flag);
naturalFlagAssignment = { isLookingForValue: true, requiredType: flag.type };
if(flag.config === undefined || flag.config.acceptsNaturalValue)
naturalFlagAssignment = { isLookingForValue: true, requiredType: flag.type };
continue;
}

4
types.d.ts vendored
View File

@ -5,8 +5,10 @@ interface FlagT<T, Z> {
/** Shorhands, AKA only work with singular minus signs like -f; Multiple characters are allowed; Will not count as a full (like --f) flag alias; */
shorthands?: string|string[],
type?: Z
/** Whether to send/throw an error if the flag is present but no value is set, either with an assigned (--flag=value) value, or regular --flag value @default false */
/** If enabled, sends/throws an error if the flag is present but no value is set, either with an assigned value (ie: ["--flag=value"]), or natural value (ie: ["--flag", "value"]) @default false */
requiredValue?: boolean
/** If disabled, only accepts assigned values (ie: ["--flag=value"]) for flag values (ie: the would-have-been value will instead become input) @default true*/
acceptsNaturalValue?: boolean
};
export type FlagAny = FlagT<string, "string"> | FlagT<boolean, "boolean"> | FlagT<number, "number"> | FlagT<bigint, "bigint"> | FlagT<string | number | bigint | boolean, "any">