Compare commits
2 Commits
1d5ee33286
...
679f6fff3a
Author | SHA1 | Date | |
---|---|---|---|
679f6fff3a | |||
ca9b98212e |
4
index.ts
4
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 { ParserError } from './src/util/Errors/ParserError.js';
|
||||||
export { Command } from './src/classes/Command.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 { IResolver } from './src/interfaces/Resolver.js';
|
||||||
export { OptionType, CommandOptionDefinition } from './src/interfaces/CommandOption.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';
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@navy.gif/commandparser",
|
"name": "@navy.gif/commandparser",
|
||||||
"version": "1.7.2",
|
"version": "1.7.3",
|
||||||
"description": "Parser meant to parse commands and their options for discord bots",
|
"description": "Parser meant to parse commands and their options for discord bots",
|
||||||
"author": "Navy.gif",
|
"author": "Navy.gif",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -9,13 +9,9 @@ import Util from './util/Util.js';
|
|||||||
import { ICommand } from '../index.js';
|
import { ICommand } from '../index.js';
|
||||||
import { ParserError } from './util/Errors/ParserError.js';
|
import { ParserError } from './util/Errors/ParserError.js';
|
||||||
|
|
||||||
type ArgsResult = {
|
|
||||||
[key: string]: CommandOption | undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
type ParseResult = {
|
type ParseResult = {
|
||||||
args: CommandOption[],
|
options: CommandOption[],
|
||||||
globalFlags: ArgsResult,
|
globalFlags: CommandOption[],
|
||||||
command: ICommand,
|
command: ICommand,
|
||||||
subcommand: string | null,
|
subcommand: string | null,
|
||||||
subcommandGroup: string | null,
|
subcommandGroup: string | null,
|
||||||
@ -172,8 +168,8 @@ class Parser extends EventEmitter
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parseResult: ParseResult = {
|
const parseResult: ParseResult = {
|
||||||
args: [],
|
options: [],
|
||||||
globalFlags: {},
|
globalFlags: [],
|
||||||
command,
|
command,
|
||||||
subcommand: null,
|
subcommand: null,
|
||||||
subcommandGroup: null,
|
subcommandGroup: null,
|
||||||
@ -188,12 +184,7 @@ class Parser extends EventEmitter
|
|||||||
if (this.#globalFlags)
|
if (this.#globalFlags)
|
||||||
{
|
{
|
||||||
this.debug('Parsing global flags');
|
this.debug('Parsing global flags');
|
||||||
const flags = await this.parseFlags(params, this.#globalFlags, null, true);
|
parseResult.globalFlags = await this.parseFlags(params, this.#globalFlags, null, true);
|
||||||
parseResult.globalFlags = flags.reduce((prev, curr) =>
|
|
||||||
{
|
|
||||||
prev[curr.name] = curr;
|
|
||||||
return prev;
|
|
||||||
}, {} as ArgsResult);
|
|
||||||
params = params.filter(elem => Boolean(elem));
|
params = params.filter(elem => Boolean(elem));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +236,7 @@ class Parser extends EventEmitter
|
|||||||
this.debug(`Params pre-quotes: ${params.length ? params.join(', ') : 'NONE'}`);
|
this.debug(`Params pre-quotes: ${params.length ? params.join(', ') : 'NONE'}`);
|
||||||
params = Util.parseQuotes(params.join(' ')).map(([ str ]: (string | boolean)[]): string => str.toString());
|
params = Util.parseQuotes(params.join(' ')).map(([ str ]: (string | boolean)[]): string => str.toString());
|
||||||
this.debug(`Given params: "${params.length ? params.join('", "') : 'NONE'}"`);
|
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;
|
return parseResult;
|
||||||
}
|
}
|
||||||
@ -506,4 +497,4 @@ class Parser extends EventEmitter
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Parser, ArgsResult };
|
export { Parser, ParseResult };
|
@ -1,5 +1,5 @@
|
|||||||
import { OptionType } from '../interfaces/CommandOption.js';
|
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 SubcommandOption from './SubcommandOption.js';
|
||||||
import SubcommandGroupOption from './SubcommandGroupOption.js';
|
import SubcommandGroupOption from './SubcommandGroupOption.js';
|
||||||
import CommandOption from './CommandOption.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');
|
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[]
|
get subcommands (): SubcommandOption[]
|
||||||
{
|
{
|
||||||
|
@ -57,9 +57,7 @@ class CommandOption implements ICommandOption
|
|||||||
throw new Error('Cannot have subcommand groups under a subcommand');
|
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))
|
if (this.type === OptionType.SUB_COMMAND && this.options.some(opt => opt.type === OptionType.SUB_COMMAND))
|
||||||
throw new Error('Cannot nest subcommands');
|
throw new Error('Cannot nest subcommands');
|
||||||
if (!SUB.includes(this.type)
|
if (!SUB.includes(this.type) && this.options.some(opt => SUB.includes(opt.type)))
|
||||||
&& this.options.some(opt => SUB.includes(opt.type))
|
|
||||||
&& this.name !== 'help') // Allow the help flag at any level
|
|
||||||
throw new Error('Cannot have subcommand(group)s under an option');
|
throw new Error('Cannot have subcommand(group)s under an option');
|
||||||
|
|
||||||
// if sub commands and normal options exists together, throw an error
|
// if sub commands and normal options exists together, throw an error
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
import CommandOption from '../classes/CommandOption.js';
|
import CommandOption from '../classes/CommandOption.js';
|
||||||
import SubcommandGroupOption from '../classes/SubcommandGroupOption.js';
|
import SubcommandGroupOption from '../classes/SubcommandGroupOption.js';
|
||||||
import SubcommandOption from '../classes/SubcommandOption.js';
|
import SubcommandOption from '../classes/SubcommandOption.js';
|
||||||
import { ArgsResult } from '../Parser.js';
|
import { ParseResult } from '../Parser.js';
|
||||||
import { CommandOptionDefinition } from './CommandOption.js';
|
import { CommandOptionDefinition } from './CommandOption.js';
|
||||||
|
|
||||||
export type CommandOpts = {
|
type CommandArgs = Omit<ParseResult, 'command' | 'partial' | 'aborted'>;
|
||||||
args: ArgsResult,
|
|
||||||
subcommand?: string | null,
|
|
||||||
subcommandGroup?: string | null
|
|
||||||
}
|
|
||||||
interface ICommand {
|
interface ICommand {
|
||||||
|
|
||||||
name: string,
|
name: string,
|
||||||
@ -24,7 +21,7 @@ interface ICommand {
|
|||||||
|
|
||||||
subcommandGroup(name: string): SubcommandGroupOption | null
|
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
|
help?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export { ICommand, CommandDefinition };
|
export { ICommand, CommandDefinition, CommandArgs };
|
||||||
export default ICommand;
|
export default ICommand;
|
Loading…
Reference in New Issue
Block a user