argparse-ts
Version:
Modern CLI arguments parser for node.js
127 lines • 4.49 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.StopException = exports.ArgumentValueError = exports.ArgumentNameError = exports.ArgumentConfigError = exports.ArgsParserError = exports.ArgsParserException = void 0;
exports.isExceptionInstanceOf = isExceptionInstanceOf;
/**
* Base error class for all exceptions thrown by `ArgsParser`.
*
* @category Exceptions
*/
var ArgsParserException = /** @class */ (function (_super) {
__extends(ArgsParserException, _super);
function ArgsParserException(message) {
var _this = _super.call(this, message) || this;
_this.names = [];
_this.names.push('ArgsParserException');
return _this;
}
return ArgsParserException;
}(Error));
exports.ArgsParserException = ArgsParserException;
/**
* Base error class for all errors thrown by `ArgsParser`.
*
* @category Exceptions
*/
var ArgsParserError = /** @class */ (function (_super) {
__extends(ArgsParserError, _super);
function ArgsParserError(message) {
var _this = _super.call(this, message) || this;
_this.names.push('ArgsParserError');
return _this;
}
return ArgsParserError;
}(ArgsParserException));
exports.ArgsParserError = ArgsParserError;
/**
* Thrown when trying to add an argument with a name that already exists.
*
* @category Exceptions
*/
var ArgumentConfigError = /** @class */ (function (_super) {
__extends(ArgumentConfigError, _super);
function ArgumentConfigError(message) {
var _this = _super.call(this, message) || this;
_this.names.push('ArgumentConfigError');
return _this;
}
return ArgumentConfigError;
}(ArgsParserError));
exports.ArgumentConfigError = ArgumentConfigError;
/**
* Thrown when the argument name is invalid.
*
* @category Exceptions
*/
var ArgumentNameError = /** @class */ (function (_super) {
__extends(ArgumentNameError, _super);
function ArgumentNameError(message) {
var _this = _super.call(this, message) || this;
_this.names.push('ArgumentNameError');
return _this;
}
return ArgumentNameError;
}(ArgsParserError));
exports.ArgumentNameError = ArgumentNameError;
/**
* Thrown when the argument value is invalid.
*
* @category Exceptions
*/
var ArgumentValueError = /** @class */ (function (_super) {
__extends(ArgumentValueError, _super);
function ArgumentValueError(message) {
var _this = _super.call(this, message) || this;
_this.names.push('ArgumentValueError');
return _this;
}
return ArgumentValueError;
}(ArgsParserError));
exports.ArgumentValueError = ArgumentValueError;
/**
* Thrown when the parser should stop processing arguments.
*
* @category Exceptions
*/
var StopException = /** @class */ (function (_super) {
__extends(StopException, _super);
function StopException(message) {
var _this = _super.call(this, message) || this;
_this.names.push('StopException');
return _this;
}
return StopException;
}(ArgsParserException));
exports.StopException = StopException;
/**
* Checks if the given exception is an instance of the specified class.
*
* @param e - The exception to check.
* @param instanceOf - The class to check against.
*
* @returns `true` if the exception is an instance of the specified class, `false` otherwise.
*/
function isExceptionInstanceOf(e, instanceOf) {
if (!('names' in e) || !Array.isArray(e.names)) {
return false;
}
var currentNames = e.names;
var instanceNames = (new instanceOf()).names;
return instanceNames.every(function (name) { return currentNames.includes(name); });
}
//# sourceMappingURL=exceptions.js.map