From 852d155717c4cdad52243a43cc1bf2b6e9dbec39 Mon Sep 17 00:00:00 2001 From: noolaan Date: Sat, 11 Apr 2020 04:00:53 -0600 Subject: [PATCH] more command handler changers --- .../components/observers/CommandHandler.js | 93 ++++++++++++++++--- structure/interfaces/Argument.js | 7 +- 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/structure/client/components/observers/CommandHandler.js b/structure/client/components/observers/CommandHandler.js index 51ebf05..1dcd9d6 100644 --- a/structure/client/components/observers/CommandHandler.js +++ b/structure/client/components/observers/CommandHandler.js @@ -125,34 +125,71 @@ class CommandHandler extends Observer { const { shortFlags, longFlags, keys } = await this._createFlags(command.arguments); + // -prune 50 -c #test + // -prune 50 -b -c #test + let currentArgument = null; for(const word of args) { - if(currentArgument) { - //what - } const [one,two,...chars] = word.split(''); if(one === '-' && two !== '-') { //short flag maybe? - const name = [ two, ...chars ].join(''); - currentArgument = shortFlags[name]; - if(!currentArgument) continue; + const name = [ two, ...chars ].join(''); + if(currentArgument) { + if(currentArgument.required) { + //Cannot use another flag after a required flag. + //Handle requirement error for currentArgument. + } + } + if(!keys.includes(name)) continue; + currentArgument = shortFlags[name]; + } else if(one === '-' && two === '-') { //long flag maybe? + if(currentArgument.required) { + //Cannot use another flag after a required flag. + //Handle requirement error for currentArgument. + } const name = chars.join(''); + if(!keys.includes(name)) continue; currentArgument = longFlags[name]; - if(!currentArgument) continue; - } else { //verbal argument + + } else { const regex = new RegExp('([0-9]*)([a-zA-Z]+)([0-9]*)', 'g'); let match = regex.exec(word); - if(!match) continue; + if(currentArgument && !match) { + await this._handleTypeParsing(currentArgument, word); + if(!currentArgument.infinite) currentArgument = null; + } + if(!keys[match[2]]) { + //do sometihng there + } currentArgument = longFlags[match[2]]; - if(!currentArgument) continue; const value = match[1] || match[3]; - console.log(currentArgument); + + } } } + async _handleTypeParsing(argument, string) { + + const { error, value } = await this.constructor.parseType(argument, string); //Cannot access static functions through "this". + if(error) { + //Failed to parse correctly. prompt.invalid + } + + if(['INTEGER', 'FLOAT'].includes(argument.type)) { + const { min, max } = argument; + if(value > max) { + //Failed to parse correctly. prompt.invalid + } + if(value < min) { + //Failed to parse correctly. prompt.invalid + } + } + + } + async _createFlags(args) { let shortFlags = {}; @@ -187,6 +224,40 @@ class CommandHandler extends Observer { } + static async parseType(type, str) { //this is in the class for a reason, will soon reference to a user resolver etc. + + //INTEGER AND FLOAT ARE SAME FUNCTION + + const types = { + STRING: (str) => { + return { error: false, value: `${str}` }; + }, + INTEGER: (str) => { + const int = parseInt(str); + if(Number.isNaN(int)) return { error: true } + return { error: false, value: int }; + }, + FLOAT: (str) => { + const float = parseInt(str); + if(Number.isNaN(float)) return { error: true } + return { error: false, value: float }; + }, + BOOLEAN: (str) => { + const truthy = ['yes', 'y', 'true', 't', 'on', 'enable']; + const falsey = ['no', 'n', 'false', 'f', 'off', 'disable']; + if(typeof str === 'boolean') return { error: false, value: str }; + + if(typeof str === 'string') str = str.toLowerCase(); + if(truthy.includes(str)) return { error: false, value: true }; + if(falsey.includes(str)) return { error: false, value: false }; + return { error: true }; + } + } + + return await types[type](str); + + } + } diff --git a/structure/interfaces/Argument.js b/structure/interfaces/Argument.js index 279e0cd..df75543 100644 --- a/structure/interfaces/Argument.js +++ b/structure/interfaces/Argument.js @@ -23,13 +23,14 @@ class Argument { this.required = options.type === 'BOOLEAN' ? false : Boolean(options.required); //If the argument must be required for the command to work. Booleans this.default = options.default || Constants.Defaults[options.type]; this.infinite = Boolean(options.infinite); //Accepts infinite amount of arguments e.g. -u @nolan @navy. If false, will only detect one user. + // Will turn value into an array instead of a string!!! this.min = typeof options.min === 'number' ? options.min : null; //Min/max will only be used for INTEGER/FLOAT types. this.max = typeof options.max === 'number' ? options.max : null; this.parser = options.parser || null; //Option to pass a function to verify values. - this.value = null; //The value provided to the flag; assigned in the command handler. + this.value = this.infinite ? [] : null; //The value provided to the flag; assigned in the command handler. } @@ -50,7 +51,9 @@ const Constants = { 'STRING', 'INTEGER', 'FLOAT', - 'BOOLEAN' + 'BOOLEAN', + 'MEMBER', + 'CHANNEL' ], ArgumentTypes: [ 'FLAG',