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(" ");
|
||||
const args = { _: [] };
|
||||
let curr = null;
|
||||
let concated = "";
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const a = input[i];
|
||||
if (a.startsWith("--") && !curr) {
|
||||
if (a.startsWith("--") && !curr && isAllowed(a.slice(2).split("=")[0], allowed)) {
|
||||
if (a.includes("=")) {
|
||||
const [ arg, value ] = a.slice(2).split("=");
|
||||
let ended = true;
|
||||
if (value.startsWith("\"")) {
|
||||
if (!value.endsWith("\"")) {
|
||||
args[arg] = value.slice(1).slice(0, -1);
|
||||
} else {
|
||||
args[arg] = `${value.slice(1)} `;
|
||||
if (!value.endsWith("\"")) ended = false;
|
||||
ended = false;
|
||||
}
|
||||
} else if (value.endsWith("\"")) {
|
||||
args[arg] += a.slice(0, -1);
|
||||
} else if (value !== "") {
|
||||
|
@ -56,6 +60,11 @@ module.exports = (input) => {
|
|||
return args;
|
||||
};
|
||||
|
||||
const isAllowed = (input, allowed) => {
|
||||
if (!allowed) return true;
|
||||
return allowed.includes(input);
|
||||
};
|
||||
|
||||
// /*
|
||||
// Format:
|
||||
// [{name: "verbose", type: "bool"}, {name: "username", type: "string"}]
|
||||
|
|
Loading…
Reference in a new issue