More command parser stuff

This commit is contained in:
TheEssem 2021-06-14 22:19:10 -05:00
parent 52d91b752d
commit c2e13c26b6
No known key found for this signature in database
GPG key ID: A3F9F02129092FCA

View file

@ -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"}]