This commit is contained in:
Erik 2023-05-05 18:29:54 +03:00
parent c669d60428
commit aa7fe8643c
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
6 changed files with 12 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@navy.gif/commandparser",
"version": "1.4.4",
"version": "1.4.5",
"description": "Parser meant to parse commands and their options for discord bots",
"author": "Navy.gif",
"license": "MIT",

View File

@ -10,6 +10,7 @@ abstract class Command implements ICommand {
name: string;
aliases: string[];
options: CommandOption[];
help?: string;
constructor (def: CommandDefinition) {
@ -18,9 +19,10 @@ abstract class Command implements ICommand {
if (!def.name)
throw new Error('Missing name for command');
this.name = def.name;
this.aliases = def.aliases || [];
this.help = def.help;
// Add a built-in help flag to any non-subcommand option
this.options = [

View File

@ -8,6 +8,7 @@ class CommandOption implements ICommandOption {
[key: string]: unknown;
name: string;
aliases: string[];
help?: string;
// eslint-disable-next-line no-use-before-define
options: CommandOption[];
type: OptionType;
@ -35,6 +36,7 @@ class CommandOption implements ICommandOption {
this.strict = def.strict || false;
this.type = def.type ?? OptionType.STRING;
this.flag = def.flag || false;
this.help = def.help;
this.options = [];
if (SUB.includes(this.type))

View File

@ -10,6 +10,7 @@ interface ICommand {
name: string,
aliases: string[]
options: CommandOption[]
help?: string
get subcommands(): SubcommandOption[]
@ -27,6 +28,7 @@ type CommandDefinition = {
name: string;
aliases?: string[];
options?: (CommandOptionDefinition | CommandOption)[];
help?: string
}
export { ICommand, CommandDefinition };

View File

@ -72,6 +72,7 @@ type ParseResult = {
type CommandOptionDefinition = {
name: string;
aliases?: string[];
help?: string;
// eslint-disable-next-line no-use-before-define
options?: (CommandOptionDefinition|CommandOption)[];
type?: OptionType;
@ -99,6 +100,7 @@ interface ICommandOption {
name: string;
// Optional alises
aliases: string[];
help?: string;
// Sub options
options: CommandOption[];

View File

@ -18,4 +18,5 @@ const command = new Command({
const parser = new Parser({ commands: [command], prefix: '', debug: true });
parser.on('debug', console.log)
console.log(await parser.parseMessage('create code -a 1'));
console.log(await parser.parseMessage('create code --help'))
console.log(await parser.parseMessage('create code --help'));
console.log(await parser.parseMessage('create --help'));