Added getter to result that converts options to a map

This commit is contained in:
Erik 2024-04-13 13:03:54 +03:00
parent e3cad2c397
commit b01b3ff5a9
3 changed files with 50 additions and 4 deletions

View File

@ -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<string, CommandOption[]>
/**
* Parsing was aborted early by user submitted filter function
@ -64,7 +67,7 @@ class Parser extends EventEmitter
private commands: ExtendedMap<string, ICommand>;
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<string, CommandOption[]>());
},
options: [],
globalFlags: [],
command,

View File

@ -0,0 +1,24 @@
import AbstractResolver from '../interfaces/Resolver.js';
class DefaultResolver extends AbstractResolver
{
resolveUser<User> (_resolveable: string, _strict?: boolean | undefined): Promise<User | null>
{
throw new Error('Method not implemented.');
}
resolveMember<Member, Guild> (_resolveable: string, _strict: boolean, _guild: Guild): Promise<Member | null>
{
throw new Error('Method not implemented.');
}
resolveChannel<Channel, Guild> (_resolveable: string, _strict: boolean, _guild: Guild): Promise<Channel | null>
{
throw new Error('Method not implemented.');
}
resolveRole<Role, Guild> (_resolveable: string, _strict: boolean, _guild: Guild): Promise<Role | null>
{
throw new Error('Method not implemented.');
}
}
export default DefaultResolver;

View File

@ -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')));
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);