update readme
allow default values on required opts
This commit is contained in:
parent
57313488b4
commit
1d8aa751b9
41
README.md
41
README.md
@ -1,3 +1,40 @@
|
|||||||
A command parser for Discord bots. Should work with any JS library.
|
A command parser initially made for quickly making discord bots, but works as a standalone parser too.
|
||||||
|
|
||||||
You're expected to implement a string -> abstraction resolver yourself and pass it to the parser.
|
Your commands are expected to inherit from the Command class
|
||||||
|
|
||||||
|
**Example usage**
|
||||||
|
```js
|
||||||
|
|
||||||
|
const commands: Command[] = [
|
||||||
|
new Command({
|
||||||
|
name: 'ping',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'respond', //
|
||||||
|
type: OptionType.BOOLEAN, // Boolean type expects the parser to have some kind of string -> boolean resolver, see the resolver interface for which methods are expected
|
||||||
|
required: true, // Will throw error if not provided
|
||||||
|
defaultValue: true, // The value that will be given back if no value is given, or if option is required but not provided
|
||||||
|
flag: true // The parser will look for the option in the "--respond value" form
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
]; // Command definitions
|
||||||
|
|
||||||
|
const parser = new Parser({commands, prefix: ''}); // Empty prefix
|
||||||
|
const parsed = await parser.parseMessage('ping --respond');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parsed will be an object cotaining properties args, command, subcommand, and subcommandgroup
|
||||||
|
* the args property is an object of the given options mapped by their names, e.g.
|
||||||
|
* {
|
||||||
|
* respond: {
|
||||||
|
* ... properties,
|
||||||
|
* value: true
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Some of the option types have built-in parsers (e.g. numbers, strings), but others will require the resolver implementation, and in some cases CommandOption extensions.
|
@ -23,7 +23,8 @@ type ParserOptions = {
|
|||||||
commands: Iterable<Command>,
|
commands: Iterable<Command>,
|
||||||
prefix?: string,
|
prefix?: string,
|
||||||
debug?: boolean,
|
debug?: boolean,
|
||||||
resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>
|
resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>,
|
||||||
|
allowDefaultOnRequired?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParseOptions = {
|
type ParseOptions = {
|
||||||
@ -39,14 +40,12 @@ 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, Command>;
|
||||||
|
|
||||||
private _debug = false;
|
private _debug = false;
|
||||||
|
|
||||||
prefix: string;
|
prefix: string;
|
||||||
|
|
||||||
resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>;
|
resolver?: IResolver<unknown, unknown, unknown, unknown, unknown>;
|
||||||
|
allowDefaultOnRequired: boolean;
|
||||||
|
|
||||||
constructor ({ commands, prefix, debug = false, resolver }: ParserOptions) {
|
constructor ({ commands, prefix, debug = false, resolver, allowDefaultOnRequired }: ParserOptions) {
|
||||||
|
|
||||||
super();
|
super();
|
||||||
|
|
||||||
@ -57,6 +56,7 @@ class Parser extends EventEmitter {
|
|||||||
this.resolver = resolver;
|
this.resolver = resolver;
|
||||||
|
|
||||||
this.prefix = prefix || '';
|
this.prefix = prefix || '';
|
||||||
|
this.allowDefaultOnRequired = allowDefaultOnRequired ?? true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,9 +311,16 @@ class Parser extends EventEmitter {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
this.debug(`Making sure required options were given.`);
|
this.debug(`Making sure required options were given.`);
|
||||||
for (const req of options.filter((opt) => opt.required))
|
for (const req of options.filter((opt) => opt.required)) {
|
||||||
if (!args[req.name])
|
if (!args[req.name]) {
|
||||||
throw new ParseError(`${req.name} is a required option`);
|
if (req.defaultValue === null || !this.allowDefaultOnRequired)
|
||||||
|
throw new ParseError(`${req.name} is a required option`);
|
||||||
|
|
||||||
|
const opt = req.clone();
|
||||||
|
opt.value = req.defaultValue;
|
||||||
|
args[req.name] = opt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (strings.length)
|
if (strings.length)
|
||||||
throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);
|
throw new ParseError(`Unrecognised option(s): "${strings.join('", "')}"`);
|
||||||
|
Loading…
Reference in New Issue
Block a user