More command parser stuff
This commit is contained in:
parent
52d91b752d
commit
c2e13c26b6
1 changed files with 13 additions and 4 deletions
|
@ -1,17 +1,21 @@
|
||||||
module.exports = (input) => {
|
module.exports = (input, allowed) => {
|
||||||
input = input.split(" ");
|
input = input.split(" ");
|
||||||
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) {
|
if (a.startsWith("--") && !curr && isAllowed(a.slice(2).split("=")[0], allowed)) {
|
||||||
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("\"")) {
|
||||||
|
args[arg] = value.slice(1).slice(0, -1);
|
||||||
|
} else {
|
||||||
args[arg] = `${value.slice(1)} `;
|
args[arg] = `${value.slice(1)} `;
|
||||||
if (!value.endsWith("\"")) ended = false;
|
ended = false;
|
||||||
|
}
|
||||||
} else if (value.endsWith("\"")) {
|
} else if (value.endsWith("\"")) {
|
||||||
args[arg] += a.slice(0, -1);
|
args[arg] += a.slice(0, -1);
|
||||||
} else if (value !== "") {
|
} else if (value !== "") {
|
||||||
|
@ -56,6 +60,11 @@ module.exports = (input) => {
|
||||||
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