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,32 +2,52 @@ module.exports = (input) => {
input = input.split(" "); input = input.split(" ");
const args = { _: [] }; const args = { _: [] };
let curr = null; let curr = null;
let concated = "";
for (let i = 0; i < input.length; i++) { for (let i = 0; i < input.length; i++) {
const a = input[i]; const a = input[i];
if (a.startsWith("--")) { if (a.startsWith("--") && !curr) {
if (curr) { if (a.includes("=")) {
args[curr] = true; 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) { } else if (curr) {
if (a.startsWith("\"")) { if (a.endsWith("\"")) {
args[curr] = `${a.slice(1)} `;
} else if (a.endsWith("\"")) {
args[curr] += a.slice(0, -1); args[curr] += a.slice(0, -1);
curr = null; curr = null;
} else { } else {
if (args[curr].split(" ").length == 1) { args[curr] += `${a} `;
args[curr] += a; }
curr = null; } else if (a.startsWith("\"")) {
if (a.endsWith("\"")) {
args.push(a.slice(1).slice(0, -1));
} else { } else {
args[curr] += a; concated += `${a.slice(1)} `;
}
} }
} else if (a.endsWith("\"")) {
concated += a.slice(0, -1);
args._.push(concated);
concated = "";
} else {
if (concated !== "") {
concated += `${a} `;
} else { } else {
args._.push(a); args._.push(a);
} }
} }
}
if (curr && args[curr] == "") { if (curr && args[curr] == "") {
args[curr] = true; args[curr] = true;