From 3c0b112ac234e51ae79e952047f9804a56afd546 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 16 Aug 2023 13:12:53 +0300 Subject: [PATCH] Minor tweaks to the ParserError class --- index.ts | 3 ++- src/Parser.ts | 21 ++++++++++----------- src/util/Errors/ParserError.ts | 6 ++++++ 3 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 src/util/Errors/ParserError.ts diff --git a/index.ts b/index.ts index bfc6cc8..20e3c4d 100644 --- a/index.ts +++ b/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 { CommandOption } from './src/classes/CommandOption.js'; export { SubcommandGroupOption } from './src/classes/SubcommandGroupOption.js'; diff --git a/src/Parser.ts b/src/Parser.ts index 1c35e99..6bd07e7 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -7,6 +7,7 @@ import IResolver from './interfaces/Resolver.js'; import ExtendedMap from "./util/Map.js"; import Util from "./util/Util.js"; import { ICommand } from '../index.js'; +import { ParserError } from './util/Errors/ParserError.js'; type ArgsResult = { [key: string]: CommandOption @@ -33,8 +34,6 @@ type ParseOptions = { guild?: unknown } -class ParseError extends Error {} - const flagReg = /(?:^| )(?(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu; class Parser extends EventEmitter { @@ -103,7 +102,7 @@ class Parser extends EventEmitter { if (commandFilter && typeof commandFilter === 'function') { const result = await commandFilter(command); - if (result) { + if (!result) { this.debug(`Ignoring command ${command.name}`); return null; } @@ -119,7 +118,7 @@ class Parser extends EventEmitter { if (subcommandGroups.length || subcommands.length) { this.debug(`Expecting subcommand`); 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; group = command.subcommandGroup(first); @@ -138,7 +137,7 @@ class Parser extends EventEmitter { parseResult.subcommandGroup = group?.name || null; 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}`); } @@ -189,7 +188,7 @@ class Parser extends EventEmitter { return f.name === _flag || aliased; }); if (!flag) - throw new ParseError(`Unrecognised flag: ${_flag}`); + throw new ParserError(`Unrecognised flag: ${_flag}`); this.debug(`Matched flag: ${flag.name} with ${_flag}`); params.splice(index, 1, ''); @@ -211,9 +210,9 @@ class Parser extends EventEmitter { const result = await flag.parse(guild); if (result.error) { 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}`); for (const r of result.removed) @@ -314,7 +313,7 @@ class Parser extends EventEmitter { for (const req of options.filter((opt) => opt.required)) { if (!args[req.name]) { 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(); opt.value = req.defaultValue; @@ -323,7 +322,7 @@ class Parser extends EventEmitter { } if (strings.length) - throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`); + throw new ParserError(`Unrecognised option(s): "${strings.join('", "')}"`); return args; @@ -336,4 +335,4 @@ class Parser extends EventEmitter { } -export { Parser, ParseError, ArgsResult }; \ No newline at end of file +export { Parser, ArgsResult }; \ No newline at end of file diff --git a/src/util/Errors/ParserError.ts b/src/util/Errors/ParserError.ts new file mode 100644 index 0000000..4ccaf15 --- /dev/null +++ b/src/util/Errors/ParserError.ts @@ -0,0 +1,6 @@ +export class ParserError extends Error { + constructor (message: string, options?: ErrorOptions) { + super(message, options); + this.name = 'ParserError'; + } +} \ No newline at end of file