Command parser changes

This commit is contained in:
TheEssem 2021-06-05 19:55:40 -05:00
parent ba3e92b1de
commit 8b07524e6b
No known key found for this signature in database
GPG key ID: A3F9F02129092FCA

View file

@ -2,30 +2,50 @@ module.exports = (input) => {
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("--")) {
if (curr) {
args[curr] = true;
if (a.startsWith("--") && !curr) {
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;
} else if (value.endsWith("\"")) {
args[arg] += a.slice(0, -1);
} else if (value !== "") {
args[arg] = value;
} else {
args[arg] = true;
}
if (!ended) curr = arg;
} else {
args[a.slice(2)] = true;
}
args[a.slice(2)] = "";
curr = a.slice(2);
} else if (curr) {
if (a.startsWith("\"")) {
args[curr] = `${a.slice(1)} `;
} else if (a.endsWith("\"")) {
if (a.endsWith("\"")) {
args[curr] += a.slice(0, -1);
curr = null;
} else {
if (args[curr].split(" ").length == 1) {
args[curr] += a;
curr = null;
} else {
args[curr] += a;
}
args[curr] += `${a} `;
}
} else if (a.startsWith("\"")) {
if (a.endsWith("\"")) {
args.push(a.slice(1).slice(0, -1));
} else {
concated += `${a.slice(1)} `;
}
} else if (a.endsWith("\"")) {
concated += a.slice(0, -1);
args._.push(concated);
concated = "";
} else {
args._.push(a);
if (concated !== "") {
concated += `${a} `;
} else {
args._.push(a);
}
}
}