Compare commits

...

2 Commits

Author SHA1 Message Date
679f6fff3a v.1.7.3 2024-03-31 19:12:50 +03:00
ca9b98212e More typings 2024-03-31 19:12:23 +03:00
6 changed files with 18 additions and 32 deletions

View File

@ -1,4 +1,4 @@
export { Parser, ArgsResult } from './src/Parser.js';
export { Parser, ParseResult } from './src/Parser.js';
export { ParserError } from './src/util/Errors/ParserError.js';
export { Command } from './src/classes/Command.js';
@ -8,4 +8,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, CommandArgs } from './src/interfaces/Command.js';

View File

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

View File

@ -9,13 +9,9 @@ import Util from './util/Util.js';
import { ICommand } from '../index.js';
import { ParserError } from './util/Errors/ParserError.js';
type ArgsResult = {
[key: string]: CommandOption | undefined
}
type ParseResult = {
args: CommandOption[],
globalFlags: ArgsResult,
options: CommandOption[],
globalFlags: CommandOption[],
command: ICommand,
subcommand: string | null,
subcommandGroup: string | null,
@ -172,8 +168,8 @@ class Parser extends EventEmitter
}
const parseResult: ParseResult = {
args: [],
globalFlags: {},
options: [],
globalFlags: [],
command,
subcommand: null,
subcommandGroup: null,
@ -188,12 +184,7 @@ class Parser extends EventEmitter
if (this.#globalFlags)
{
this.debug('Parsing global flags');
const flags = await this.parseFlags(params, this.#globalFlags, null, true);
parseResult.globalFlags = flags.reduce((prev, curr) =>
{
prev[curr.name] = curr;
return prev;
}, {} as ArgsResult);
parseResult.globalFlags = await this.parseFlags(params, this.#globalFlags, null, true);
params = params.filter(elem => Boolean(elem));
}
@ -245,7 +236,7 @@ class Parser extends EventEmitter
this.debug(`Params pre-quotes: ${params.length ? params.join(', ') : 'NONE'}`);
params = Util.parseQuotes(params.join(' ')).map(([ str ]: (string | boolean)[]): string => str.toString());
this.debug(`Given params: "${params.length ? params.join('", "') : 'NONE'}"`);
parseResult.args = await this.parseOptions(params, activeCommand.options, guild);
parseResult.options = await this.parseOptions(params, activeCommand.options, guild);
return parseResult;
}
@ -506,4 +497,4 @@ class Parser extends EventEmitter
}
export { Parser, ArgsResult };
export { Parser, ParseResult };

View File

@ -1,5 +1,5 @@
import { OptionType } from '../interfaces/CommandOption.js';
import { ICommand, CommandDefinition, CommandOpts } from '../interfaces/Command.js';
import { ICommand, CommandDefinition, CommandArgs } from '../interfaces/Command.js';
import SubcommandOption from './SubcommandOption.js';
import SubcommandGroupOption from './SubcommandGroupOption.js';
import CommandOption from './CommandOption.js';
@ -38,7 +38,7 @@ abstract class Command implements ICommand
throw new Error('Cannot have subcommand(group)s on the same level as an option');
}
abstract execute(message: unknown, args: CommandOpts): Promise<unknown>;
abstract execute(message: unknown, args: CommandArgs): Promise<unknown>;
get subcommands (): SubcommandOption[]
{

View File

@ -57,9 +57,7 @@ class CommandOption implements ICommandOption
throw new Error('Cannot have subcommand groups under a subcommand');
if (this.type === OptionType.SUB_COMMAND && this.options.some(opt => opt.type === OptionType.SUB_COMMAND))
throw new Error('Cannot nest subcommands');
if (!SUB.includes(this.type)
&& this.options.some(opt => SUB.includes(opt.type))
&& this.name !== 'help') // Allow the help flag at any level
if (!SUB.includes(this.type) && this.options.some(opt => SUB.includes(opt.type)))
throw new Error('Cannot have subcommand(group)s under an option');
// if sub commands and normal options exists together, throw an error

View File

@ -1,14 +1,11 @@
import CommandOption from '../classes/CommandOption.js';
import SubcommandGroupOption from '../classes/SubcommandGroupOption.js';
import SubcommandOption from '../classes/SubcommandOption.js';
import { ArgsResult } from '../Parser.js';
import { ParseResult } from '../Parser.js';
import { CommandOptionDefinition } from './CommandOption.js';
export type CommandOpts = {
args: ArgsResult,
subcommand?: string | null,
subcommandGroup?: string | null
}
type CommandArgs = Omit<ParseResult, 'command' | 'partial' | 'aborted'>;
interface ICommand {
name: string,
@ -24,7 +21,7 @@ interface ICommand {
subcommandGroup(name: string): SubcommandGroupOption | null
execute(message: unknown, opts: CommandOpts): Promise<unknown>
execute(message: unknown, args: CommandArgs): Promise<unknown>
}
@ -35,5 +32,5 @@ type CommandDefinition = {
help?: string
}
export { ICommand, CommandDefinition };
export { ICommand, CommandDefinition, CommandArgs };
export default ICommand;