diff --git a/src/Parser.ts b/src/Parser.ts index 244e174..6733976 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -100,7 +100,10 @@ class Parser extends EventEmitter const command = this.matchCommand(commandName); if (!command) + { + this.debug(`No command found for ${commandName}`); return null; + } this.debug(`Matched command ${command.name}`); if (typeof commandFilter === 'function') @@ -280,10 +283,15 @@ class Parser extends EventEmitter continue; } cloned.rawValue = [ params[index] ]; - ({ removed, error } = await cloned.parse(guild)); + // eslint-disable-next-line init-declarations + let valid; + ({ removed, error, valid } = await cloned.parse(guild)); // eslint-disable-next-line max-depth if (!error) break; + // eslint-disable-next-line max-depth + if (typeof valid !== 'undefined' && valid) + throw new ParserError(`Value ${params[index]} is out of bounds, must be in range [${cloned.minimum}, ${cloned.maximum}]`); index++; } } diff --git a/src/classes/CommandOption.ts b/src/classes/CommandOption.ts index 03ee970..df9d4d6 100644 --- a/src/classes/CommandOption.ts +++ b/src/classes/CommandOption.ts @@ -86,6 +86,9 @@ class CommandOption implements ICommandOption this.dependsOn = def.dependsOn || []; this.dependsOnMode = def.dependsOnMode || 'AND'; + this.minimum = def.minimum; + this.maximum = def.maximum; + this.required = def.required || false; } @@ -157,9 +160,9 @@ class CommandOption implements ICommandOption if (isNaN(integer)) return { error: true }; if (this.minimum !== undefined && integer < this.minimum) - return { error: true }; + return { error: true, valid: true }; if (this.maximum !== undefined && integer > this.maximum) - return { error: true }; + return { error: true, valid: true }; return { value: integer, removed: [ this.rawValue[0] ] }; } @@ -171,9 +174,9 @@ class CommandOption implements ICommandOption if (isNaN(float)) return { error: true }; if (this.minimum !== undefined && float < this.minimum) - return { error: true }; + return { error: true, valid: true }; if (this.maximum !== undefined && float > this.maximum) - return { error: true }; + return { error: true, valid: true }; return { value: float, removed: [ this.rawValue[0] ] }; } diff --git a/src/interfaces/CommandOption.ts b/src/interfaces/CommandOption.ts index 685c6b5..5480156 100644 --- a/src/interfaces/CommandOption.ts +++ b/src/interfaces/CommandOption.ts @@ -65,6 +65,8 @@ type DependsOnMode = 'AND' | 'OR'; type ParseResult = { error: boolean, + // Valid is set if the value is out of bounds + valid: boolean, removed: string[], value: never } diff --git a/tests/playground.js b/tests/playground.js index 0982bce..c9d54e6 100644 --- a/tests/playground.js +++ b/tests/playground.js @@ -1,34 +1,47 @@ import { Parser, Command, OptionType } from '../build/esm/index.js'; -// const command = new Command({ -// name: 'create', -// options: [{ -// name: 'registration-code', -// aliases: ['code'], -// type: OptionType.SUB_COMMAND, -// options: [{ -// name: 'amount', -// flag: true, -// type: OptionType.INTEGER, -// defaultValue: 1, -// valueOptional: true, -// required: true -// }] -// }] -// }); -const command = new Command({ +const createCommand = new Command({ + name: 'create', + options: [{ + name: 'registration-code', + aliases: ['code'], + type: OptionType.SUB_COMMAND, + options: [{ + name: 'amount', + flag: true, + type: OptionType.INTEGER, + defaultValue: 1, + valueOptional: true, + required: true + }] + }] +}); +const botbanCommand = new Command({ name: 'botban', options: [ { name: 'users', type: OptionType.STRING, required: true }, { name: 'service', choices: ['support', 'reports'], valueAsAlias: true, required: true } ] }); -const parser = new Parser({ commands: [command], prefix: '', debug: true }); + +const volumeCommand = new Command({ + name: 'volume', + options: [ + { + name: 'volume', + type: OptionType.INTEGER, + maximum: 100, + minimum: 0 + }, + ] +}); +const parser = new Parser({ commands: [createCommand, botbanCommand, volumeCommand], 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 --help')); +console.log((await parser.parseMessage('create code -a 1')).args); +console.log((await parser.parseMessage('create code --help')).args); +// console.log(await parser.parseMessage('create --help')); -// console.log(await parser.parseMessage('create code')); +console.log((await parser.parseMessage('create code')).args); -console.log(await parser.parseMessage('botban support dingus')); \ No newline at end of file +console.log((await parser.parseMessage('botban support dingus')).args); +console.log((await parser.parseMessage('volume 500')).args); \ No newline at end of file