Minor tweaks to the ParserError class
This commit is contained in:
parent
25fcb88885
commit
3c0b112ac2
3
index.ts
3
index.ts
@ -1,5 +1,6 @@
|
|||||||
export { Parser, ParseError, ArgsResult } from './src/Parser.js';
|
export { Parser, ArgsResult } from './src/Parser.js';
|
||||||
|
|
||||||
|
export { ParserError } from './src/util/Errors/ParserError.js';
|
||||||
export { Command } from './src/classes/Command.js';
|
export { Command } from './src/classes/Command.js';
|
||||||
export { CommandOption } from './src/classes/CommandOption.js';
|
export { CommandOption } from './src/classes/CommandOption.js';
|
||||||
export { SubcommandGroupOption } from './src/classes/SubcommandGroupOption.js';
|
export { SubcommandGroupOption } from './src/classes/SubcommandGroupOption.js';
|
||||||
|
@ -7,6 +7,7 @@ import IResolver from './interfaces/Resolver.js';
|
|||||||
import ExtendedMap from "./util/Map.js";
|
import ExtendedMap from "./util/Map.js";
|
||||||
import Util from "./util/Util.js";
|
import Util from "./util/Util.js";
|
||||||
import { ICommand } from '../index.js';
|
import { ICommand } from '../index.js';
|
||||||
|
import { ParserError } from './util/Errors/ParserError.js';
|
||||||
|
|
||||||
type ArgsResult = {
|
type ArgsResult = {
|
||||||
[key: string]: CommandOption
|
[key: string]: CommandOption
|
||||||
@ -33,8 +34,6 @@ type ParseOptions = {
|
|||||||
guild?: unknown
|
guild?: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParseError extends Error {}
|
|
||||||
|
|
||||||
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
|
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
|
||||||
|
|
||||||
class Parser extends EventEmitter {
|
class Parser extends EventEmitter {
|
||||||
@ -103,7 +102,7 @@ class Parser extends EventEmitter {
|
|||||||
|
|
||||||
if (commandFilter && typeof commandFilter === 'function') {
|
if (commandFilter && typeof commandFilter === 'function') {
|
||||||
const result = await commandFilter(command);
|
const result = await commandFilter(command);
|
||||||
if (result) {
|
if (!result) {
|
||||||
this.debug(`Ignoring command ${command.name}`);
|
this.debug(`Ignoring command ${command.name}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -119,7 +118,7 @@ class Parser extends EventEmitter {
|
|||||||
if (subcommandGroups.length || subcommands.length) {
|
if (subcommandGroups.length || subcommands.length) {
|
||||||
this.debug(`Expecting subcommand`);
|
this.debug(`Expecting subcommand`);
|
||||||
if (!params.length)
|
if (!params.length)
|
||||||
throw new ParseError('Expected a subcommand(group), got nothing');
|
throw new ParserError('Expected a subcommand(group), got nothing');
|
||||||
const [ first, second, ...rest ] = params;
|
const [ first, second, ...rest ] = params;
|
||||||
group = command.subcommandGroup(first);
|
group = command.subcommandGroup(first);
|
||||||
|
|
||||||
@ -138,7 +137,7 @@ class Parser extends EventEmitter {
|
|||||||
parseResult.subcommandGroup = group?.name || null;
|
parseResult.subcommandGroup = group?.name || null;
|
||||||
|
|
||||||
if (!subcommand)
|
if (!subcommand)
|
||||||
throw new ParseError(`Expecting subcommand, one of ${subcommands.map((s) => s.name).join(', ')}`); // return { showUsage: true, verbose: true };
|
throw new ParserError(`Expecting subcommand, one of ${subcommands.map((s) => s.name).join(', ')}`); // return { showUsage: true, verbose: true };
|
||||||
this.debug(`Got ${subcommand.name}`);
|
this.debug(`Got ${subcommand.name}`);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -189,7 +188,7 @@ class Parser extends EventEmitter {
|
|||||||
return f.name === _flag || aliased;
|
return f.name === _flag || aliased;
|
||||||
});
|
});
|
||||||
if (!flag)
|
if (!flag)
|
||||||
throw new ParseError(`Unrecognised flag: ${_flag}`);
|
throw new ParserError(`Unrecognised flag: ${_flag}`);
|
||||||
this.debug(`Matched flag: ${flag.name} with ${_flag}`);
|
this.debug(`Matched flag: ${flag.name} with ${_flag}`);
|
||||||
|
|
||||||
params.splice(index, 1, '');
|
params.splice(index, 1, '');
|
||||||
@ -211,9 +210,9 @@ class Parser extends EventEmitter {
|
|||||||
const result = await flag.parse(guild);
|
const result = await flag.parse(guild);
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
if (flag.choices.length) {
|
if (flag.choices.length) {
|
||||||
throw new ParseError(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
|
throw new ParserError(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`);
|
||||||
}
|
}
|
||||||
throw new ParseError(`Failed to parse value for ${flag.name}, expected value type: ${OptionType[flag.type]}`); // return { option: flag, ...result.removed };
|
throw new ParserError(`Failed to parse value for ${flag.name}, expected value type: ${OptionType[flag.type]}`); // return { option: flag, ...result.removed };
|
||||||
}
|
}
|
||||||
this.debug(`Cleaning up params after ${flag.name}`);
|
this.debug(`Cleaning up params after ${flag.name}`);
|
||||||
for (const r of result.removed)
|
for (const r of result.removed)
|
||||||
@ -314,7 +313,7 @@ class Parser extends EventEmitter {
|
|||||||
for (const req of options.filter((opt) => opt.required)) {
|
for (const req of options.filter((opt) => opt.required)) {
|
||||||
if (!args[req.name]) {
|
if (!args[req.name]) {
|
||||||
if (req.defaultValue === null || !this.allowDefaultOnRequired)
|
if (req.defaultValue === null || !this.allowDefaultOnRequired)
|
||||||
throw new ParseError(`${req.name} is a required option`);
|
throw new ParserError(`${req.name} is a required option`);
|
||||||
|
|
||||||
const opt = req.clone();
|
const opt = req.clone();
|
||||||
opt.value = req.defaultValue;
|
opt.value = req.defaultValue;
|
||||||
@ -323,7 +322,7 @@ class Parser extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strings.length)
|
if (strings.length)
|
||||||
throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);
|
throw new ParserError(`Unrecognised option(s): "${strings.join('", "')}"`);
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
|
|
||||||
@ -336,4 +335,4 @@ class Parser extends EventEmitter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Parser, ParseError, ArgsResult };
|
export { Parser, ArgsResult };
|
6
src/util/Errors/ParserError.ts
Normal file
6
src/util/Errors/ParserError.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export class ParserError extends Error {
|
||||||
|
constructor (message: string, options?: ErrorOptions) {
|
||||||
|
super(message, options);
|
||||||
|
this.name = 'ParserError';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user