argparse-ts
Version:
Modern CLI arguments parser for node.js
84 lines • 2.15 kB
JavaScript
/**
* Base error class for all exceptions thrown by `ArgsParser`.
*
* @category Exceptions
*/
export class ArgsParserException extends Error {
constructor(message) {
super(message);
this.names = [];
this.names.push('ArgsParserException');
}
}
/**
* Base error class for all errors thrown by `ArgsParser`.
*
* @category Exceptions
*/
export class ArgsParserError extends ArgsParserException {
constructor(message) {
super(message);
this.names.push('ArgsParserError');
}
}
/**
* Thrown when trying to add an argument with a name that already exists.
*
* @category Exceptions
*/
export class ArgumentConfigError extends ArgsParserError {
constructor(message) {
super(message);
this.names.push('ArgumentConfigError');
}
}
/**
* Thrown when the argument name is invalid.
*
* @category Exceptions
*/
export class ArgumentNameError extends ArgsParserError {
constructor(message) {
super(message);
this.names.push('ArgumentNameError');
}
}
/**
* Thrown when the argument value is invalid.
*
* @category Exceptions
*/
export class ArgumentValueError extends ArgsParserError {
constructor(message) {
super(message);
this.names.push('ArgumentValueError');
}
}
/**
* Thrown when the parser should stop processing arguments.
*
* @category Exceptions
*/
export class StopException extends ArgsParserException {
constructor(message) {
super(message);
this.names.push('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.
*/
export function isExceptionInstanceOf(e, instanceOf) {
if (!('names' in e) || !Array.isArray(e.names)) {
return false;
}
const currentNames = e.names;
const instanceNames = (new instanceOf()).names;
return instanceNames.every((name) => currentNames.includes(name));
}
//# sourceMappingURL=exceptions.js.map