From bdd73dd6a94c4434a6252efcc5e540422dae1cf4 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 12 Feb 2023 14:51:45 +0200 Subject: [PATCH] default parsers for some built-in types --- src/classes/CommandOption.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/classes/CommandOption.ts b/src/classes/CommandOption.ts index d15f76c..c8a3468 100644 --- a/src/classes/CommandOption.ts +++ b/src/classes/CommandOption.ts @@ -115,6 +115,32 @@ class CommandOption implements ICommandOption { return { value: this.rawValue.join(' '), removed: this.rawValue }; } + protected INTEGER() { + if (!this.rawValue) return { error: true }; + const integer = parseInt(this.rawValue[0]); + if(isNaN(integer)) return { error: true }; + if (this.minimum !== undefined && integer < this.minimum) return { error: true }; + if (this.maximum !== undefined && integer > this.maximum) return { error: true }; + return { value: integer, removed: [this.rawValue[0]] }; + } + + protected FLOAT() { + if (!this.rawValue) return { error: true }; + const float = parseFloat(this.rawValue[0]); + if(isNaN(float)) return { error: true }; + if (this.minimum !== undefined && float < this.minimum) return { error: true }; + if (this.maximum !== undefined && float > this.maximum) return { error: true }; + return { value: (float), removed: [this.rawValue[0]] }; + } + + protected BOOLEAN() { + if (!this.rawValue) return { error: true }; + const boolean = this.resolver?.resolveBoolean(this.rawValue[0]) || null; + if (boolean === null && this.valueOptional) return { value: this.defaultValue, removed: [] }; + else if(boolean === null) return { error: true }; + return { value: boolean, removed: [this.rawValue[0]] }; + } + } export { CommandOption };