From ca9b98212e85616abdec1b34f569f70ce535768f Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 31 Mar 2024 19:09:06 +0300 Subject: [PATCH] More typings --- index.ts | 4 ++-- src/Parser.ts | 23 +++++++---------------- src/classes/Command.ts | 4 ++-- src/classes/CommandOption.ts | 4 +--- src/interfaces/Command.ts | 13 +++++-------- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/index.ts b/index.ts index 20e3c4d..f1a3a4b 100644 --- a/index.ts +++ b/index.ts @@ -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'; \ No newline at end of file +export { CommandDefinition, ICommand, CommandArgs } from './src/interfaces/Command.js'; \ No newline at end of file diff --git a/src/Parser.ts b/src/Parser.ts index 85f311a..2a9df10 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -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 }; \ No newline at end of file +export { Parser, ParseResult }; \ No newline at end of file diff --git a/src/classes/Command.ts b/src/classes/Command.ts index 77baae9..24aae10 100644 --- a/src/classes/Command.ts +++ b/src/classes/Command.ts @@ -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; + abstract execute(message: unknown, args: CommandArgs): Promise; get subcommands (): SubcommandOption[] { diff --git a/src/classes/CommandOption.ts b/src/classes/CommandOption.ts index 0c79626..a3e7252 100644 --- a/src/classes/CommandOption.ts +++ b/src/classes/CommandOption.ts @@ -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 diff --git a/src/interfaces/Command.ts b/src/interfaces/Command.ts index 3e1212b..7fd2fba 100644 --- a/src/interfaces/Command.ts +++ b/src/interfaces/Command.ts @@ -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; + interface ICommand { name: string, @@ -24,7 +21,7 @@ interface ICommand { subcommandGroup(name: string): SubcommandGroupOption | null - execute(message: unknown, opts: CommandOpts): Promise + execute(message: unknown, args: CommandArgs): Promise } @@ -35,5 +32,5 @@ type CommandDefinition = { help?: string } -export { ICommand, CommandDefinition }; +export { ICommand, CommandDefinition, CommandArgs }; export default ICommand; \ No newline at end of file