argparse-ts
Version:
Modern CLI arguments parser for node.js
234 lines • 7.67 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createValueCaster = createValueCaster;
/**
* 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
*/
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
*/
var BaseValueCaster = /** @class */ (function () {
/**
* Constructs a base value caster with the provided argument configuration.
*
* @param config - The extended configuration for the argument to cast.
*/
function BaseValueCaster(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.
*/
BaseValueCaster.prototype.cast = function (value, isset) {
if (!isset) {
return this.argConfig.default;
}
if (value.length === 0) {
return this.argConfig.const;
}
return undefined;
};
return BaseValueCaster;
}());
/**
* An abstract class that provides a single value caster implementation.
*
* @template T - The expected type of the argument value.
*
* @category Cast
*/
var SingleValueCaster = /** @class */ (function (_super) {
__extends(SingleValueCaster, _super);
function SingleValueCaster() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 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.
*/
SingleValueCaster.prototype.cast = function (value, isset) {
var baseCasted = _super.prototype.cast.call(this, 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.
*/
SingleValueCaster.prototype.getSingleValue = function (value) {
var _a;
return (_a = value[0]) !== null && _a !== void 0 ? _a : undefined;
};
return SingleValueCaster;
}(BaseValueCaster));
/**
* A value caster for a single string argument value.
*
* @category Cast
*/
var StringValueCaster = /** @class */ (function (_super) {
__extends(StringValueCaster, _super);
function StringValueCaster() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 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.
*/
StringValueCaster.prototype.castInternal = function (value) {
return value !== undefined ? String(value) : undefined;
};
return StringValueCaster;
}(SingleValueCaster));
/**
* A value caster for a single number argument value.
*
* @category Cast
*/
var NumberValueCaster = /** @class */ (function (_super) {
__extends(NumberValueCaster, _super);
function NumberValueCaster() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 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.
*/
NumberValueCaster.prototype.castInternal = function (value) {
return value !== undefined ? Number(value) : undefined;
};
return NumberValueCaster;
}(SingleValueCaster));
/**
* A value caster for a single boolean argument value.
*
* @category Cast
*/
var BooleanValueCaster = /** @class */ (function (_super) {
__extends(BooleanValueCaster, _super);
function BooleanValueCaster() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 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.
*/
BooleanValueCaster.prototype.castInternal = function (value) {
return value !== undefined ? !['false', '0'].includes(value) : undefined;
};
return BooleanValueCaster;
}(SingleValueCaster));
/**
* A value caster for an array of argument values.
*
* @template T - The expected type of the argument item value.
*
* @category Cast
*/
var ArrayValueCaster = /** @class */ (function (_super) {
__extends(ArrayValueCaster, _super);
/**
* Constructs a new ArrayValueCaster with the provided argument configuration.
*
* @param config - The extended configuration for the argument to cast.
*/
function ArrayValueCaster(config) {
var _this = _super.call(this, config) || this;
_this.itemCaster = createSingleValueCaster(config);
return _this;
}
/**
* 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.
*/
ArrayValueCaster.prototype.cast = function (value, isset) {
var _this = this;
var baseCasted = _super.prototype.cast.call(this, value, isset);
if (baseCasted !== undefined && baseCasted.length > 0) {
return baseCasted;
}
if (value.length === 0 && !isset && !this.argConfig.positional) {
return undefined;
}
return value.map(function (v) { return _this.itemCaster.cast([v], isset); });
};
return ArrayValueCaster;
}(BaseValueCaster));
//# sourceMappingURL=cast.js.map