Fix some command parser issues

This commit is contained in:
Essem 2021-07-02 10:52:44 -05:00
parent ecc6d98aec
commit a52b05d8b7
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -1,16 +1,16 @@
module.exports = (input, allowed) => {
module.exports = (input) => {
if (typeof input === "string") input = input.split(/\s+/g);
const args = { _: [] };
let curr = null;
let concated = "";
for (let i = 0; i < input.length; i++) {
const a = input[i];
if (a.startsWith("--") && !curr && isAllowed(a.slice(2).split("=")[0], allowed)) {
if (a.startsWith("--") && !curr) {
if (a.includes("=")) {
const [arg, value] = a.slice(2).split("=");
let ended = true;
if (value.startsWith("\"")) {
if (!value.endsWith("\"")) {
if (value.endsWith("\"")) {
args[arg] = value.slice(1).slice(0, -1);
} else {
args[arg] = `${value.slice(1)} `;
@ -18,15 +18,16 @@ module.exports = (input, allowed) => {
}
} else if (value.endsWith("\"")) {
args[arg] += a.slice(0, -1);
} else if (value === "true") {
args[arg] = true;
} else if (value === "false") {
args[arg] = false;
} else if (value !== "") {
args[arg] = value;
} else {
args[arg] = true;
}
if (args[arg] === "true") {
args[arg] = true;
} else if (args[arg] === "false") {
args[arg] = false;
}
if (!ended) curr = arg;
} else {
args[a.slice(2)] = true;
@ -64,11 +65,6 @@ module.exports = (input, allowed) => {
return args;
};
const isAllowed = (input, allowed) => {
if (!allowed) return true;
return allowed.includes(input);
};
// /*
// Format:
// [{name: "verbose", type: "bool"}, {name: "username", type: "string"}]