diff --git a/src/Parser.ts b/src/Parser.ts index 96e7aeb..0ccf263 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -8,13 +8,16 @@ import ExtendedMap from './util/Map.js'; import Util from './util/Util.js'; import { ICommand } from '../index.js'; import { ParserError } from './util/Errors/ParserError.js'; +import DefaultResolver from './classes/DefaultResolver.js'; type ParseResult = { + // These are arrays to allow for multiple instances of the same argument options: CommandOption[], globalFlags: CommandOption[], command: ICommand, subCommand: string | null, subCommandGroup: string | null, + map: Map /** * Parsing was aborted early by user submitted filter function @@ -64,7 +67,7 @@ class Parser extends EventEmitter private commands: ExtendedMap; private _debug = false; prefix: string; - resolver?: AbstractResolver; + resolver: AbstractResolver; allowDefaultOnRequired: boolean; #globalFlags: CommandOption[]; #allowIncompleteReturn: string[]; @@ -105,7 +108,7 @@ class Parser extends EventEmitter for (const command of commands) this.commands.set(command.name, command); - this.resolver = resolver; + this.resolver = resolver ?? new DefaultResolver(); this.prefix = prefix || ''; this.allowDefaultOnRequired = allowDefaultOnRequired ?? true; @@ -168,6 +171,19 @@ class Parser extends EventEmitter } const parseResult: ParseResult = { + get map () + { + return this.options.reduce((prev, curr) => + { + const existing = prev.get(curr.name); + if (existing) + existing.push(curr); + else + prev.set(curr.name, [ curr ]); + + return prev; + }, new Map()); + }, options: [], globalFlags: [], command, diff --git a/src/classes/DefaultResolver.ts b/src/classes/DefaultResolver.ts new file mode 100644 index 0000000..3d1c32a --- /dev/null +++ b/src/classes/DefaultResolver.ts @@ -0,0 +1,24 @@ +import AbstractResolver from '../interfaces/Resolver.js'; + +class DefaultResolver extends AbstractResolver +{ + resolveUser (_resolveable: string, _strict?: boolean | undefined): Promise + { + throw new Error('Method not implemented.'); + } + resolveMember (_resolveable: string, _strict: boolean, _guild: Guild): Promise + { + throw new Error('Method not implemented.'); + } + resolveChannel (_resolveable: string, _strict: boolean, _guild: Guild): Promise + { + throw new Error('Method not implemented.'); + } + resolveRole (_resolveable: string, _strict: boolean, _guild: Guild): Promise + { + throw new Error('Method not implemented.'); + } + +} + +export default DefaultResolver; \ No newline at end of file diff --git a/tests/playground.js b/tests/playground.js index 3adf400..20f2450 100644 --- a/tests/playground.js +++ b/tests/playground.js @@ -29,6 +29,9 @@ const commands = [ valueOptional: true, required: true, instances: 2 + }, { + name: 'test', + flag: true }] }] }), @@ -80,7 +83,7 @@ const parser = new Parser({ allowIncompleteReturn: true }); parser.on('debug', console.log); -console.log((await parser.parseMessage('create code -a 1 -a 6 -a 7'))); +console.log((await parser.parseMessage('create code -a 1 -a 6 -a 7').catch(console.log))); console.log((await parser.parseMessage('create code --help'))); console.log(await parser.parseMessage('create --help')); console.log((await parser.parseMessage('test sub --help'))); @@ -92,4 +95,7 @@ console.log((await parser.parseMessage('botban support dingus'))); console.log((await parser.parseMessage('search --help'))); console.log((await parser.parseMessage('search --id 5000'))); console.log((await parser.parseMessage('volume 100'))); -console.log((await parser.parseMessage('volume 200'))); \ No newline at end of file +console.log((await parser.parseMessage('volume 200').catch(console.log))); + +const result = await parser.parseMessage('create code -a 1 -a 6 --test testing'); +console.log(result.map); \ No newline at end of file