Compare commits
3 Commits
6d921d6dec
...
1d5ee33286
Author | SHA1 | Date | |
---|---|---|---|
1d5ee33286 | |||
ac1ce34111 | |||
999543ced5 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@navy.gif/commandparser",
|
||||
"version": "1.6.8",
|
||||
"version": "1.7.2",
|
||||
"description": "Parser meant to parse commands and their options for discord bots",
|
||||
"author": "Navy.gif",
|
||||
"license": "MIT",
|
||||
|
@ -14,7 +14,7 @@ type ArgsResult = {
|
||||
}
|
||||
|
||||
type ParseResult = {
|
||||
args: ArgsResult,
|
||||
args: CommandOption[],
|
||||
globalFlags: ArgsResult,
|
||||
command: ICommand,
|
||||
subcommand: string | null,
|
||||
@ -172,7 +172,7 @@ class Parser extends EventEmitter
|
||||
}
|
||||
|
||||
const parseResult: ParseResult = {
|
||||
args: {},
|
||||
args: [],
|
||||
globalFlags: {},
|
||||
command,
|
||||
subcommand: null,
|
||||
@ -250,14 +250,14 @@ class Parser extends EventEmitter
|
||||
return parseResult;
|
||||
}
|
||||
|
||||
async parseOptions (params: string[], options: CommandOption[], guild?: unknown)
|
||||
async parseOptions<T extends CommandOption> (params: string[], options: T[], guild?: unknown): Promise<T[]>
|
||||
{
|
||||
const args: ArgsResult = {};
|
||||
const args: T[] = [];
|
||||
|
||||
// Parse flags
|
||||
const flags = await this.parseFlags(params, options);
|
||||
const flags = await this.parseFlags<T>(params, options);
|
||||
for (const flag of flags)
|
||||
args[flag.name] = flag;
|
||||
args.push(flag);
|
||||
|
||||
this.debug(`Params after flag parsing "${params.length ? params.join('", "') : 'NONE'}"`);
|
||||
// Non-string options
|
||||
@ -313,7 +313,7 @@ class Parser extends EventEmitter
|
||||
continue;
|
||||
}
|
||||
|
||||
args[cloned.name] = cloned;
|
||||
args.push(cloned as T);
|
||||
// Clean up params for string parsing
|
||||
for (const r of removed)
|
||||
params.splice(params.indexOf(r), 1, '');
|
||||
@ -353,7 +353,7 @@ class Parser extends EventEmitter
|
||||
break;
|
||||
const cloned = strOpt.clone([ val ], this.resolver);
|
||||
await cloned.parse(guild);
|
||||
args[cloned.name] = cloned;
|
||||
args.push(cloned as T);
|
||||
}
|
||||
|
||||
// This part is obsolete now, I think, the string option checks the choice value
|
||||
@ -371,14 +371,14 @@ class Parser extends EventEmitter
|
||||
this.debug('Making sure required options were given.');
|
||||
for (const req of options.filter((opt) => opt.required))
|
||||
{
|
||||
if (!args[req.name])
|
||||
if (!args.some(a => a.name === req.name))
|
||||
{
|
||||
if (req.defaultValue === null || !this.allowDefaultOnRequired)
|
||||
throw new ParserError(`${req.name} is a required option`);
|
||||
|
||||
const opt = req.clone();
|
||||
opt.value = req.defaultValue;
|
||||
args[req.name] = opt;
|
||||
args.push(opt as T);
|
||||
}
|
||||
}
|
||||
|
||||
@ -387,11 +387,11 @@ class Parser extends EventEmitter
|
||||
return args;
|
||||
}
|
||||
|
||||
async parseFlags (params: string[], options: CommandOption[], guild?: unknown, allowUnknown?: boolean)
|
||||
async parseFlags<T extends CommandOption> (params: string[], options: T[], guild?: unknown, allowUnknown?: boolean): Promise<T[]>
|
||||
{
|
||||
const flags = options.filter((opt) => opt.flag);
|
||||
let currentFlag = null;
|
||||
const parsed = [];
|
||||
const parsed: T[] = [];
|
||||
this.debug(`${params.length} params to go through: "${params.join('", "')}"`);
|
||||
for (let index = 0; index < params.length;)
|
||||
{
|
||||
@ -441,6 +441,17 @@ class Parser extends EventEmitter
|
||||
}
|
||||
this.debug(`Matched flag: ${flag.name} with ${_flag}`);
|
||||
|
||||
if (flag.instances)
|
||||
{
|
||||
const count = parsed.reduce((prev, curr) =>
|
||||
{
|
||||
if (curr.name === flag.name)
|
||||
prev++;
|
||||
return prev;
|
||||
}, 0);
|
||||
if (count === flag.instances)
|
||||
throw new ParserError(`Cannot have more than ${flag.instances} instances of option "${flag.name}"`);
|
||||
}
|
||||
// params.splice(index, 1, ''); // Replace with empty string as to not alter the length of the params array
|
||||
params.splice(index, 1);
|
||||
if (aliased)
|
||||
@ -448,7 +459,7 @@ class Parser extends EventEmitter
|
||||
this.debug('Aliased');
|
||||
const clone = flag.clone([ _flag ], this.resolver);
|
||||
clone.aliased = true;
|
||||
parsed.push(clone);
|
||||
parsed.push(clone as T);
|
||||
currentFlag = null;
|
||||
}
|
||||
else
|
||||
@ -456,7 +467,7 @@ class Parser extends EventEmitter
|
||||
this.debug('Not aliased');
|
||||
// eslint-disable-next-line no-undefined
|
||||
currentFlag = flag.clone(undefined, this.resolver);
|
||||
parsed.push(currentFlag);
|
||||
parsed.push(currentFlag as T);
|
||||
}
|
||||
// index++;
|
||||
}
|
||||
@ -477,7 +488,7 @@ class Parser extends EventEmitter
|
||||
params.splice(params.indexOf(r), 1);
|
||||
}
|
||||
|
||||
return parsed;
|
||||
return parsed as T[];
|
||||
}
|
||||
|
||||
private debug (message: string)
|
||||
|
@ -14,6 +14,7 @@ class CommandOption implements ICommandOption
|
||||
type: OptionType;
|
||||
minimum?: number;
|
||||
maximum?: number;
|
||||
instances: number;
|
||||
flag: boolean;
|
||||
valueOptional: boolean;
|
||||
defaultValue: boolean | number | string | null;
|
||||
@ -80,6 +81,9 @@ class CommandOption implements ICommandOption
|
||||
|
||||
this.minimum = def.minimum;
|
||||
this.maximum = def.maximum;
|
||||
// Default to 1 to retain original behaviour
|
||||
// Really only applies to flag type options, given that non-flags aren't parsed by their name, rather by their type and order
|
||||
this.instances = def.instances ?? 1;
|
||||
|
||||
this.required = def.required || false;
|
||||
}
|
||||
|
@ -87,6 +87,7 @@ type CommandOptionDefinition = {
|
||||
|
||||
minimum?: number;
|
||||
maximum?: number;
|
||||
instances?: number
|
||||
|
||||
flag?: boolean;
|
||||
valueOptional?: boolean;
|
||||
@ -109,6 +110,8 @@ interface ICommandOption {
|
||||
type: OptionType;
|
||||
strict: boolean;
|
||||
|
||||
// Number of times this option can be parsed, 0 being any
|
||||
instances: number;
|
||||
required: boolean;
|
||||
|
||||
minimum?: number;
|
||||
|
@ -27,7 +27,8 @@ const commands = [
|
||||
type: OptionType.INTEGER,
|
||||
defaultValue: 1,
|
||||
valueOptional: true,
|
||||
required: true
|
||||
required: true,
|
||||
instances: 2
|
||||
}]
|
||||
}]
|
||||
}),
|
||||
@ -79,7 +80,7 @@ const parser = new Parser({
|
||||
allowIncompleteReturn: true
|
||||
});
|
||||
parser.on('debug', console.log);
|
||||
console.log((await parser.parseMessage('create code -a 1')));
|
||||
console.log((await parser.parseMessage('create code -a 1 -a 6 -a 7')));
|
||||
console.log((await parser.parseMessage('create code --help')));
|
||||
console.log(await parser.parseMessage('create --help'));
|
||||
console.log((await parser.parseMessage('test sub --help')));
|
||||
|
Loading…
Reference in New Issue
Block a user