Compare commits

..

No commits in common. "471c749ce4b6888b351b6fe9b8e5ce48ea86be9c" and "94d1291ffbc9bee35da70c23582cd18fdcf4455e" have entirely different histories.

6 changed files with 19 additions and 29 deletions

View File

@ -1,6 +1,5 @@
export { Parser, ArgsResult } from './src/Parser.js';
export { Parser, ParseError, 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';
@ -8,4 +7,4 @@ export { SubcommandOption } from './src/classes/SubcommandOption.js';
export { IResolver } from './src/interfaces/Resolver.js';
export { OptionType, CommandOptionDefinition } from './src/interfaces/CommandOption.js';
export { CommandDefinition, ICommand, CommandOpts } from './src/interfaces/Command.js';
export { CommandDefinition, ICommand } from './src/interfaces/Command.js';

View File

@ -1,6 +1,6 @@
{
"name": "@navy.gif/commandparser",
"version": "1.5.7",
"version": "1.5.3",
"description": "Parser meant to parse commands and their options for discord bots",
"author": "Navy.gif",
"license": "MIT",

View File

@ -7,7 +7,6 @@ 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
@ -34,6 +33,8 @@ type ParseOptions = {
guild?: unknown
}
class ParseError extends Error {}
const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
class Parser extends EventEmitter {
@ -102,7 +103,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;
}
@ -118,7 +119,7 @@ class Parser extends EventEmitter {
if (subcommandGroups.length || subcommands.length) {
this.debug(`Expecting subcommand`);
if (!params.length)
throw new ParserError('Expected a subcommand(group), got nothing');
throw new ParseError('Expected a subcommand(group), got nothing');
const [ first, second, ...rest ] = params;
group = command.subcommandGroup(first);
@ -137,7 +138,7 @@ class Parser extends EventEmitter {
parseResult.subcommandGroup = group?.name || null;
if (!subcommand)
throw new ParserError(`Expecting subcommand, one of ${subcommands.map((s) => s.name).join(', ')}`); // return { showUsage: true, verbose: true };
throw new ParseError(`Expecting subcommand, one of ${subcommands.map((s) => s.name).join(', ')}`); // return { showUsage: true, verbose: true };
this.debug(`Got ${subcommand.name}`);
}
@ -188,7 +189,7 @@ class Parser extends EventEmitter {
return f.name === _flag || aliased;
});
if (!flag)
throw new ParserError(`Unrecognised flag: ${_flag}`);
throw new ParseError(`Unrecognised flag: ${_flag}`);
this.debug(`Matched flag: ${flag.name} with ${_flag}`);
params.splice(index, 1, '');
@ -210,9 +211,9 @@ class Parser extends EventEmitter {
const result = await flag.parse(guild);
if (result.error) {
if (flag.choices.length) {
throw new ParserError(`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 ParserError(`Failed to parse value for ${flag.name}, expected value type: ${OptionType[flag.type]}`); // return { option: flag, ...result.removed };
throw new ParseError(`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)
@ -313,7 +314,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 ParserError(`${req.name} is a required option`);
throw new ParseError(`${req.name} is a required option`);
const opt = req.clone();
opt.value = req.defaultValue;
@ -322,7 +323,7 @@ class Parser extends EventEmitter {
}
if (strings.length)
throw new ParserError(`Unrecognised option(s): "${strings.join('", "')}"`);
throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);
return args;
@ -335,4 +336,4 @@ class Parser extends EventEmitter {
}
export { Parser, ArgsResult };
export { Parser, ParseError, ArgsResult };

View File

@ -4,11 +4,7 @@ import SubcommandOption from '../classes/SubcommandOption.js';
import { ArgsResult } from '../Parser.js';
import { CommandOptionDefinition } from './CommandOption.js';
export type CommandOpts = {
args?: ArgsResult,
subcommand?: string | null,
subcommandGroup?: string | null
}
interface ICommand {
name: string,
@ -24,7 +20,7 @@ interface ICommand {
subcommandGroup(name: string): SubcommandGroupOption | null
execute(message: unknown, opts?: CommandOpts): Promise<unknown>
execute(message: unknown, args?: ArgsResult): Promise<unknown>
}

View File

@ -19,7 +19,7 @@ interface IResolver {
* @return {Member} {Member}
* @memberof IResolver
*/
resolveMember<Member, Guild>(resolveable: string, strict: boolean, guild: Guild): Promise<Member | null>
resolveMember<Guild, Member>(resolveable: string, strict: boolean, guild: Guild): Promise<Member | null>
/**
* Should resolve to a role abstraction
@ -30,7 +30,7 @@ interface IResolver {
* @return {Channel} {Channel}
* @memberof IResolver
*/
resolveChannel<Channel, Guild>(resolveable: string, strict: boolean, guild: Guild): Promise<Channel | null>
resolveChannel<Guild, Channel>(resolveable: string, strict: boolean, guild: Guild): Promise<Channel | null>
/**
* Should resolve to a role abstraction
@ -41,7 +41,7 @@ interface IResolver {
* @return {Role} {Role}
* @memberof IResolver
*/
resolveRole<Role, Guild>(resolveable: string, strict: boolean, guild: Guild): Promise<Role | null>
resolveRole<Guild, Role>(resolveable: string, strict: boolean, guild: Guild): Promise<Role | null>
/**
* Should resolve to true when a truthy resolveable is passed, i.e. yes, true, on etc

View File

@ -1,6 +0,0 @@
export class ParserError extends Error {
constructor (message: string, options?: ErrorOptions) {
super(message, options);
this.name = 'ParserError';
}
}