diff --git a/index.ts b/index.ts index 876f240..8d741a0 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -export { Parser } from './src/Parser'; +export { Parser, ParseError } from './src/Parser'; export { Command } from './src/classes/Command'; export { CommandOption } from './src/classes/CommandOption'; diff --git a/package.json b/package.json index 2c10dcf..71ec53e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commandparser", - "version": "1.0.15", + "version": "1.0.16", "description": "Parser meant to parse commands and their options for discord bots", "main": "build/index.js", "author": "Navy.gif", diff --git a/src/Parser.ts b/src/Parser.ts index d80797f..f3bc88e 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line max-classes-per-file import { EventEmitter } from 'events'; import CommandOption from './classes/CommandOption'; @@ -21,6 +22,8 @@ type ParserOptions = { resolver: IResolver } +class ParseError extends Error {} + const flagReg = /(?:^| )(?(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu; class Parser extends EventEmitter { @@ -89,7 +92,7 @@ class Parser extends EventEmitter { parseResult.subcommand = subcommand?.name || null; parseResult.subcommandGroup = group?.name || null; - if (!subcommand) throw new Error(`Expecting subcommand, one of ${subcommands.map((s) => s.name)}`); //return { showUsage: true, verbose: true }; + if (!subcommand) throw new ParseError(`Expecting subcommand, one of ${subcommands.map((s) => s.name)}`); //return { showUsage: true, verbose: true }; this.debug(`Got ${subcommand.name}`); } @@ -126,7 +129,7 @@ class Parser extends EventEmitter { aliased = f.valueAsAlias && f.choices.some((c) => c === _flag); return f.name === _flag || aliased; }); - if (!flag) throw new Error(`Unrecognised flag: ${_flag}`); + if (!flag) throw new ParseError(`Unrecognised flag: ${_flag}`); this.debug(`Matched flag: ${flag.name} with ${_flag}`); params.splice(index, 1, ''); @@ -147,9 +150,9 @@ class Parser extends EventEmitter { const result = await flag.parse(guild); if (result.error) { if (flag.choices.length) { - throw new Error(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`); + throw new ParseError(`Invalid choice for ${flag.name}, Valid choices are ${flag.choices.join(', ')}.`); } - throw new Error(`Failed to parse value for ${flag.name}, expected value type: ${flag.type}`); //return { option: flag, ...result.removed }; + throw new ParseError(`Failed to parse value for ${flag.name}, expected value type: ${flag.type}`); //return { option: flag, ...result.removed }; } this.debug(`Cleaning up params after ${flag.name}`); for (const r of result.removed) params.splice(params.indexOf(r), 1); @@ -231,15 +234,15 @@ class Parser extends EventEmitter { if (!arg.choices.some((choice) => { if (typeof arg.value === 'string' && typeof choice === 'string') return arg.value.toLowerCase() === choice.toLowerCase(); return arg.value === choice; - })) throw new Error(`Invalid choice: ${arg.name} value must be one of ${arg.choices.join(', ')}`); //return { error: true, index: 'O_COMMANDHANDLER_INVALID_CHOICE', params: { option: arg.name, value: arg.value, choices: arg.choices.map((c) => c).join('`, `') } }; + })) throw new ParseError(`Invalid choice: ${arg.name} value must be one of ${arg.choices.join(', ')}`); //return { error: true, index: 'O_COMMANDHANDLER_INVALID_CHOICE', params: { option: arg.name, value: arg.value, choices: arg.choices.map((c) => c).join('`, `') } }; } this.debug(`Making sure required options were given.`); for (const req of activeCommand.options.filter((opt) => opt.required)) if (!args[req.name]) - throw new Error(`${req.name} is a required option`); + throw new ParseError(`${req.name} is a required option`); - if (strings.length) throw new Error(`Unrecognised option(s): "${strings.join('", "')}"`);//return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_OPTIONS', params: { opts: strings.join('`, `') } }; + if (strings.length) throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);//return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_OPTIONS', params: { opts: strings.join('`, `') } }; return parseResult; @@ -251,4 +254,4 @@ class Parser extends EventEmitter { } -export { Parser }; \ No newline at end of file +export { Parser, ParseError }; \ No newline at end of file