Fix some command parser issues
This commit is contained in:
parent
ecc6d98aec
commit
a52b05d8b7
1 changed files with 8 additions and 12 deletions
|
@ -1,16 +1,16 @@
|
||||||
module.exports = (input, allowed) => {
|
module.exports = (input) => {
|
||||||
if (typeof input === "string") input = input.split(/\s+/g);
|
if (typeof input === "string") input = input.split(/\s+/g);
|
||||||
const args = { _: [] };
|
const args = { _: [] };
|
||||||
let curr = null;
|
let curr = null;
|
||||||
let concated = "";
|
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("--") && !curr && isAllowed(a.slice(2).split("=")[0], allowed)) {
|
if (a.startsWith("--") && !curr) {
|
||||||
if (a.includes("=")) {
|
if (a.includes("=")) {
|
||||||
const [arg, value] = a.slice(2).split("=");
|
const [arg, value] = a.slice(2).split("=");
|
||||||
let ended = true;
|
let ended = true;
|
||||||
if (value.startsWith("\"")) {
|
if (value.startsWith("\"")) {
|
||||||
if (!value.endsWith("\"")) {
|
if (value.endsWith("\"")) {
|
||||||
args[arg] = value.slice(1).slice(0, -1);
|
args[arg] = value.slice(1).slice(0, -1);
|
||||||
} else {
|
} else {
|
||||||
args[arg] = `${value.slice(1)} `;
|
args[arg] = `${value.slice(1)} `;
|
||||||
|
@ -18,15 +18,16 @@ module.exports = (input, allowed) => {
|
||||||
}
|
}
|
||||||
} else if (value.endsWith("\"")) {
|
} else if (value.endsWith("\"")) {
|
||||||
args[arg] += a.slice(0, -1);
|
args[arg] += a.slice(0, -1);
|
||||||
} else if (value === "true") {
|
|
||||||
args[arg] = true;
|
|
||||||
} else if (value === "false") {
|
|
||||||
args[arg] = false;
|
|
||||||
} else if (value !== "") {
|
} else if (value !== "") {
|
||||||
args[arg] = value;
|
args[arg] = value;
|
||||||
} else {
|
} else {
|
||||||
args[arg] = true;
|
args[arg] = true;
|
||||||
}
|
}
|
||||||
|
if (args[arg] === "true") {
|
||||||
|
args[arg] = true;
|
||||||
|
} else if (args[arg] === "false") {
|
||||||
|
args[arg] = false;
|
||||||
|
}
|
||||||
if (!ended) curr = arg;
|
if (!ended) curr = arg;
|
||||||
} else {
|
} else {
|
||||||
args[a.slice(2)] = true;
|
args[a.slice(2)] = true;
|
||||||
|
@ -64,11 +65,6 @@ module.exports = (input, allowed) => {
|
||||||
return args;
|
return args;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isAllowed = (input, allowed) => {
|
|
||||||
if (!allowed) return true;
|
|
||||||
return allowed.includes(input);
|
|
||||||
};
|
|
||||||
|
|
||||||
// /*
|
// /*
|
||||||
// Format:
|
// Format:
|
||||||
// [{name: "verbose", type: "bool"}, {name: "username", type: "string"}]
|
// [{name: "verbose", type: "bool"}, {name: "username", type: "string"}]
|
||||||
|
|
Loading…
Reference in a new issue