Compare commits

..

5 Commits

Author SHA1 Message Date
94d1291ffb
Use command interface internally 2023-08-15 17:51:08 +03:00
5e4c6bd7a3
v1.5.3 2023-08-15 16:44:08 +03:00
8881f5c6d9
Export command interface 2023-08-15 16:43:50 +03:00
9497ad6996
v1.5.2 2023-05-12 20:31:45 +03:00
0410e8d707
same as previous 2023-05-12 20:31:31 +03:00
3 changed files with 52 additions and 52 deletions

View File

@ -7,4 +7,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 } from './src/interfaces/Command.js'; export { CommandDefinition, ICommand } from './src/interfaces/Command.js';

View File

@ -1,41 +1,41 @@
{ {
"name": "@navy.gif/commandparser", "name": "@navy.gif/commandparser",
"version": "1.5.1", "version": "1.5.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",
"private": false, "private": false,
"repository": "", "repository": "",
"type": "module", "type": "module",
"main": "build/cjs/index.js", "main": "build/cjs/index.js",
"module": "build/esm/index.js", "module": "build/esm/index.js",
"types": "./build/esm/index.d.ts", "types": "./build/esm/index.d.ts",
"files": [ "files": [
"build/**/*" "build/**/*"
], ],
"exports": { "exports": {
".": { ".": {
"import": "./build/esm/index.js", "import": "./build/esm/index.js",
"require": "./build/cjs/index.js", "require": "./build/cjs/index.js",
"default": "./build/cjs/index.js", "default": "./build/cjs/index.js",
"types": "./build/esm/index.d.ts" "types": "./build/esm/index.d.ts"
} }
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.20.12", "@babel/core": "^7.20.12",
"@babel/preset-env": "^7.20.2", "@babel/preset-env": "^7.20.2",
"@types/node": "^18.6.2", "@types/node": "^18.6.2",
"@typescript-eslint/eslint-plugin": "^5.31.0", "@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0", "@typescript-eslint/parser": "^5.31.0",
"eslint": "^8.20.0", "eslint": "^8.20.0",
"eslint-plugin-jest": "^27.2.1", "eslint-plugin-jest": "^27.2.1",
"jest": "^29.4.2", "jest": "^29.4.2",
"typescript": "^4.7.4" "typescript": "^4.7.4"
}, },
"scripts": { "scripts": {
"build": "tsc && tsc -p tsconfig.cjs.json && node ./scripts/declareTypes.js", "build": "tsc && tsc -p tsconfig.cjs.json && node ./scripts/declareTypes.js",
"test": "yarn build && jest", "test": "yarn build && jest",
"release": "yarn build && yarn publish", "release": "yarn build && yarn publish",
"lint": "eslint --fix" "lint": "eslint --fix"
} }
} }

View File

@ -1,12 +1,12 @@
// eslint-disable-next-line max-classes-per-file // eslint-disable-next-line max-classes-per-file
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import Command from './classes/Command.js';
import CommandOption from './classes/CommandOption.js'; import CommandOption from './classes/CommandOption.js';
import { OptionType } from "./interfaces/CommandOption.js"; import { OptionType } from "./interfaces/CommandOption.js";
import IResolver from './interfaces/Resolver.js'; import IResolver from './interfaces/Resolver.js';
import ExtendedMap from "./util/Map.js"; import ExtendedMap from "./util/Map.js";
import Util from "./util/Util.js"; import Util from "./util/Util.js";
import { ICommand } from '../index.js';
type ArgsResult = { type ArgsResult = {
[key: string]: CommandOption [key: string]: CommandOption
@ -14,22 +14,22 @@ type ArgsResult = {
type ParseResult = { type ParseResult = {
args: ArgsResult, args: ArgsResult,
command: Command, command: ICommand,
subcommand: string | null, subcommand: string | null,
subcommandGroup: string | null subcommandGroup: string | null
} }
type ParserOptions = { type ParserOptions = {
commands: Iterable<Command>, commands: Iterable<ICommand>,
prefix?: string, prefix?: string,
debug?: boolean, debug?: boolean,
resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>, resolver?: IResolver,
allowDefaultOnRequired?: boolean allowDefaultOnRequired?: boolean
} }
type ParseOptions = { type ParseOptions = {
prefix?: string, prefix?: string,
commandFilter?: (cmd: Command) => boolean, commandFilter?: (cmd: ICommand) => boolean,
guild?: unknown guild?: unknown
} }
@ -39,10 +39,10 @@ const flagReg = /(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu;
class Parser extends EventEmitter { class Parser extends EventEmitter {
private commands: ExtendedMap<string, Command>; private commands: ExtendedMap<string, ICommand>;
private _debug = false; private _debug = false;
prefix: string; prefix: string;
resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>; resolver?: IResolver;
allowDefaultOnRequired: boolean; allowDefaultOnRequired: boolean;
constructor ({ commands, prefix, debug = false, resolver, allowDefaultOnRequired }: ParserOptions) { constructor ({ commands, prefix, debug = false, resolver, allowDefaultOnRequired }: ParserOptions) {
@ -50,7 +50,7 @@ class Parser extends EventEmitter {
super(); super();
this._debug = debug; this._debug = debug;
this.commands = new ExtendedMap<string, Command>(); this.commands = new ExtendedMap<string, ICommand>();
for (const command of commands) for (const command of commands)
this.commands.set(command.name, command); this.commands.set(command.name, command);
this.resolver = resolver; this.resolver = resolver;
@ -60,7 +60,7 @@ class Parser extends EventEmitter {
} }
private matchCommand (name: string): Command | null { private matchCommand (name: string): ICommand | null {
let command = null; let command = null;
if (this.commands.has(name)) if (this.commands.has(name))
command = this.commands.get(name); command = this.commands.get(name);
@ -138,7 +138,7 @@ class Parser extends EventEmitter {
parseResult.subcommandGroup = group?.name || null; parseResult.subcommandGroup = group?.name || null;
if (!subcommand) if (!subcommand)
throw new ParseError(`Expecting subcommand, one of ${subcommands.map((s) => s.name)}`); // 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}`); this.debug(`Got ${subcommand.name}`);
} }