text based commands parsing

This commit is contained in:
Erik 2022-07-25 01:19:16 +03:00
parent 10aa85f149
commit c33d11c508
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 27 additions and 3 deletions

View File

@ -80,3 +80,6 @@ Command Error
[O_COMMANDHANDLER_COMMAND_NORESPONSE] [O_COMMANDHANDLER_COMMAND_NORESPONSE]
Command returned no response. This should not happen and is likely a bug. Command returned no response. This should not happen and is likely a bug.
[O_COMMANDHANDLER_UNRECOGNISED_FLAG]
Unrecognised flag: `{flag}`
See command help for valid flags.

View File

@ -92,7 +92,9 @@ class CommandHandler extends Observer {
_parseResponse(invoker, response) { _parseResponse(invoker, response) {
const { command } = invoker; const { command } = invoker;
if (response.error) { if (response.error && response.index) {
return invoker.reply(response);
} else if (response.error) {
let content = invoker.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, { let content = invoker.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, {
option: response.option.name, min: response.option.minimum, max: response.option.maximum option: response.option.name, min: response.option.minimum, max: response.option.maximum
}); });
@ -275,10 +277,29 @@ class CommandHandler extends Observer {
const activeCommand = subcommand || command; const activeCommand = subcommand || command;
const flags = activeCommand.options.filter((opt) => opt.flag); const flags = activeCommand.options.filter((opt) => opt.flag);
for (const flag of flags) { params = Util.parseQuotes(params.join(' ')).map(([x]) => x);
for (let index = 0; index < params.length;) {
const match = (/(?:^| )(?<flag>(?:--[a-z0-9]{3,})|(?:-[a-z]{1,2}))(?:$| )/iu).exec(params[index]);
if (!match) {
index++;
continue;
}
const _flag = match.groups.flag.replace(/--?/u, '');
const flag = flags.find((f) => f.name === _flag.toLowerCase());
if (!flag) return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_FLAG', params: { flag: _flag } };
params.splice(index, 1);
args[flag.name] = flag.clone(params[index]);
params.splice(index, 1);
} }
const options = activeCommand.options.filter((opt) => !opt.flag);
return { options: { args, parameters: params }, verbose: true }; return { options: { args, parameters: params }, verbose: true };
} }