Added getter to result that converts options to a map
This commit is contained in:
parent
e3cad2c397
commit
b01b3ff5a9
@ -8,13 +8,16 @@ import ExtendedMap from './util/Map.js';
|
|||||||
import Util from './util/Util.js';
|
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';
|
||||||
|
import DefaultResolver from './classes/DefaultResolver.js';
|
||||||
|
|
||||||
type ParseResult = {
|
type ParseResult = {
|
||||||
|
// These are arrays to allow for multiple instances of the same argument
|
||||||
options: CommandOption[],
|
options: CommandOption[],
|
||||||
globalFlags: CommandOption[],
|
globalFlags: CommandOption[],
|
||||||
command: ICommand,
|
command: ICommand,
|
||||||
subCommand: string | null,
|
subCommand: string | null,
|
||||||
subCommandGroup: string | null,
|
subCommandGroup: string | null,
|
||||||
|
map: Map<string, CommandOption[]>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parsing was aborted early by user submitted filter function
|
* Parsing was aborted early by user submitted filter function
|
||||||
@ -64,7 +67,7 @@ class Parser extends EventEmitter
|
|||||||
private commands: ExtendedMap<string, ICommand>;
|
private commands: ExtendedMap<string, ICommand>;
|
||||||
private _debug = false;
|
private _debug = false;
|
||||||
prefix: string;
|
prefix: string;
|
||||||
resolver?: AbstractResolver;
|
resolver: AbstractResolver;
|
||||||
allowDefaultOnRequired: boolean;
|
allowDefaultOnRequired: boolean;
|
||||||
#globalFlags: CommandOption[];
|
#globalFlags: CommandOption[];
|
||||||
#allowIncompleteReturn: string[];
|
#allowIncompleteReturn: string[];
|
||||||
@ -105,7 +108,7 @@ class Parser extends EventEmitter
|
|||||||
|
|
||||||
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 ?? new DefaultResolver();
|
||||||
|
|
||||||
this.prefix = prefix || '';
|
this.prefix = prefix || '';
|
||||||
this.allowDefaultOnRequired = allowDefaultOnRequired ?? true;
|
this.allowDefaultOnRequired = allowDefaultOnRequired ?? true;
|
||||||
@ -168,6 +171,19 @@ class Parser extends EventEmitter
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parseResult: ParseResult = {
|
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: [],
|
options: [],
|
||||||
globalFlags: [],
|
globalFlags: [],
|
||||||
command,
|
command,
|
||||||
|
24
src/classes/DefaultResolver.ts
Normal file
24
src/classes/DefaultResolver.ts
Normal 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;
|
@ -29,6 +29,9 @@ const commands = [
|
|||||||
valueOptional: true,
|
valueOptional: true,
|
||||||
required: true,
|
required: true,
|
||||||
instances: 2
|
instances: 2
|
||||||
|
}, {
|
||||||
|
name: 'test',
|
||||||
|
flag: true
|
||||||
}]
|
}]
|
||||||
}]
|
}]
|
||||||
}),
|
}),
|
||||||
@ -80,7 +83,7 @@ const parser = new Parser({
|
|||||||
allowIncompleteReturn: true
|
allowIncompleteReturn: true
|
||||||
});
|
});
|
||||||
parser.on('debug', console.log);
|
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 code --help')));
|
||||||
console.log(await parser.parseMessage('create --help'));
|
console.log(await parser.parseMessage('create --help'));
|
||||||
console.log((await parser.parseMessage('test sub --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 --help')));
|
||||||
console.log((await parser.parseMessage('search --id 5000')));
|
console.log((await parser.parseMessage('search --id 5000')));
|
||||||
console.log((await parser.parseMessage('volume 100')));
|
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);
|
Loading…
Reference in New Issue
Block a user