argparse-ts
Version:
Modern CLI arguments parser for node.js
191 lines • 5.32 kB
JavaScript
/**
* Creates a value caster based on the provided argument configuration.
*
* @param argConfig - The extended configuration for the argument to cast.
*
* @returns A value caster that can be used to cast the argument value.
*
* @category Utils
* @category Cast
*/
export function createValueCaster(argConfig) {
if (argConfig.multiple) {
return new ArrayValueCaster(argConfig);
}
return createSingleValueCaster(argConfig);
}
/**
* Creates a value caster for a single value argument based on the provided argument configuration.
*
* @param argConfig - The extended configuration for the argument to cast.
*
* @returns A value caster that can be used to cast the argument value.
*
* @category Utils
* @category Cast
*/
function createSingleValueCaster(argConfig) {
switch (argConfig.type) {
case 'string':
return new StringValueCaster(argConfig);
case 'number':
return new NumberValueCaster(argConfig);
case 'boolean':
return new BooleanValueCaster(argConfig);
}
}
/**
* Base value caster.
*
* @category Cast
*/
class BaseValueCaster {
/**
* Constructs a base value caster with the provided argument configuration.
*
* @param config - The extended configuration for the argument to cast.
*/
constructor(config) {
this.argConfig = config;
}
/**
* Casts the argument value to the expected type.
*
* @param value - The value to cast.
* @param isset - Whether the value is set.
*
* @returns The casted value.
*/
cast(value, isset) {
if (!isset) {
return this.argConfig.default;
}
if (value.length === 0) {
return this.argConfig.const;
}
return undefined;
}
}
/**
* An abstract class that provides a single value caster implementation.
*
* @template T - The expected type of the argument value.
*
* @category Cast
*/
class SingleValueCaster extends BaseValueCaster {
/**
* Casts the argument value to the expected type.
*
* @param value - The value to cast.
* @param isset - Whether the value is set.
*
* @returns The casted value.
*/
cast(value, isset) {
const baseCasted = super.cast(value, isset);
if (baseCasted !== undefined) {
return baseCasted;
}
return this.castInternal(this.getSingleValue(value));
}
/**
* Gets the single value from the provided array.
*
* @param value - The array of string values to get the single value from.
*
* @returns The single value from the array or undefined if the array is empty.
*/
getSingleValue(value) {
var _a;
return (_a = value[0]) !== null && _a !== void 0 ? _a : undefined;
}
}
/**
* A value caster for a single string argument value.
*
* @category Cast
*/
class StringValueCaster extends SingleValueCaster {
/**
* Casts the argument value to a string.
*
* @param value - The value to cast.
*
* @returns The casted string value or undefined if the input value is undefined.
*/
castInternal(value) {
return value !== undefined ? String(value) : undefined;
}
}
/**
* A value caster for a single number argument value.
*
* @category Cast
*/
class NumberValueCaster extends SingleValueCaster {
/**
* Casts the argument value to a number.
*
* @param value - The value to cast.
*
* @returns The casted number value or undefined if the input value is undefined.
*/
castInternal(value) {
return value !== undefined ? Number(value) : undefined;
}
}
/**
* A value caster for a single boolean argument value.
*
* @category Cast
*/
class BooleanValueCaster extends SingleValueCaster {
/**
* Casts the argument value to a boolean.
*
* @param value - The value to cast.
*
* @returns The casted boolean value or undefined if the input value is undefined.
*/
castInternal(value) {
return value !== undefined ? !['false', '0'].includes(value) : undefined;
}
}
/**
* A value caster for an array of argument values.
*
* @template T - The expected type of the argument item value.
*
* @category Cast
*/
class ArrayValueCaster extends BaseValueCaster {
/**
* Constructs a new ArrayValueCaster with the provided argument configuration.
*
* @param config - The extended configuration for the argument to cast.
*/
constructor(config) {
super(config);
this.itemCaster = createSingleValueCaster(config);
}
/**
* Casts the argument value to an array of the expected type.
*
* @param value - The value to cast.
* @param isset - Whether the value is set.
*
* @returns The casted value or undefined if the input value is undefined.
*/
cast(value, isset) {
const baseCasted = super.cast(value, isset);
if (baseCasted !== undefined && baseCasted.length > 0) {
return baseCasted;
}
if (value.length === 0 && !isset && !this.argConfig.positional) {
return undefined;
}
return value.map((v) => this.itemCaster.cast([v], isset));
}
}
//# sourceMappingURL=cast.js.map