From c2e13c26b6e4415993e83f437924b98b412c367c Mon Sep 17 00:00:00 2001 From: TheEssem Date: Mon, 14 Jun 2021 22:19:10 -0500 Subject: [PATCH] More command parser stuff --- utils/parseCommand.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/utils/parseCommand.js b/utils/parseCommand.js index e001db0..8478609 100644 --- a/utils/parseCommand.js +++ b/utils/parseCommand.js @@ -1,17 +1,21 @@ -module.exports = (input) => { +module.exports = (input, allowed) => { input = input.split(" "); const args = { _: [] }; let curr = null; let concated = ""; for (let i = 0; i < input.length; i++) { const a = input[i]; - if (a.startsWith("--") && !curr) { + if (a.startsWith("--") && !curr && isAllowed(a.slice(2).split("=")[0], allowed)) { if (a.includes("=")) { const [ arg, value ] = a.slice(2).split("="); let ended = true; if (value.startsWith("\"")) { - args[arg] = `${value.slice(1)} `; - if (!value.endsWith("\"")) ended = false; + if (!value.endsWith("\"")) { + args[arg] = value.slice(1).slice(0, -1); + } else { + args[arg] = `${value.slice(1)} `; + ended = false; + } } else if (value.endsWith("\"")) { args[arg] += a.slice(0, -1); } else if (value !== "") { @@ -56,6 +60,11 @@ module.exports = (input) => { return args; }; +const isAllowed = (input, allowed) => { + if (!allowed) return true; + return allowed.includes(input); +}; + // /* // Format: // [{name: "verbose", type: "bool"}, {name: "username", type: "string"}]