From b1a7a27cfe6c6e3197ccaa95ce95ecc424b16044 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 12 Feb 2023 16:30:54 +0200 Subject: [PATCH] alias support, test file (TODO proper tests) --- src/classes/Command.ts | 4 ++-- tests/Parser.test.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/Parser.test.js diff --git a/src/classes/Command.ts b/src/classes/Command.ts index 98b6354..88057e6 100644 --- a/src/classes/Command.ts +++ b/src/classes/Command.ts @@ -39,12 +39,12 @@ abstract class Command implements ICommand { subcommand(name: string): SubcommandOption | null { name = name.toLowerCase(); - return this.subcommands.find((cmd) => cmd.name === name) || null; + return this.subcommands.find((cmd) => cmd.name === name || cmd.aliases.includes(name)) || null; } subcommandGroup(name: string): SubcommandGroupOption | null { name = name.toLowerCase(); - return this.subcommandGroups.find((opt) => opt.name === name) || null; + return this.subcommandGroups.find((grp) => grp.name === name || grp.aliases.includes(name)) || null; } private _subcommands(options: CommandOption[]): SubcommandOption[] { diff --git a/tests/Parser.test.js b/tests/Parser.test.js new file mode 100644 index 0000000..2653ed0 --- /dev/null +++ b/tests/Parser.test.js @@ -0,0 +1,23 @@ +const { inspect } = require('node:util'); +const { Parser, Command, OptionType } = require('../build'); + +const command = new Command({name: 'create', options:[{name: 'registration-code', aliases: ['code'], type: OptionType.SUB_COMMAND}]}) + +const parser = new Parser({commands: [command], prefix: ''}) +const debug = (a) => console.log(inspect(a)); + +const handle = async (data) => { + const raw = data.toString('utf-8'); + let words = raw.split(' ').map(word => word.trim()); + + words = words.filter(word => word.length); + if (!words.length) + return; + debug((words)); + + const result = await parser.parseMessage(words.join(' ')); + debug(result) + +} + +process.stdin.on('data', handle) \ No newline at end of file