Command parser changes
This commit is contained in:
parent
ba3e92b1de
commit
8b07524e6b
1 changed files with 35 additions and 15 deletions
|
@ -2,32 +2,52 @@ module.exports = (input) => {
|
|||
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("--")) {
|
||||
if (curr) {
|
||||
args[curr] = true;
|
||||
if (a.startsWith("--") && !curr) {
|
||||
if (a.includes("=")) {
|
||||
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) {
|
||||
if (a.startsWith("\"")) {
|
||||
args[curr] = `${a.slice(1)} `;
|
||||
} else if (a.endsWith("\"")) {
|
||||
if (a.endsWith("\"")) {
|
||||
args[curr] += a.slice(0, -1);
|
||||
curr = null;
|
||||
} else {
|
||||
if (args[curr].split(" ").length == 1) {
|
||||
args[curr] += a;
|
||||
curr = null;
|
||||
args[curr] += `${a} `;
|
||||
}
|
||||
} else if (a.startsWith("\"")) {
|
||||
if (a.endsWith("\"")) {
|
||||
args.push(a.slice(1).slice(0, -1));
|
||||
} 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 {
|
||||
args._.push(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (curr && args[curr] == "") {
|
||||
args[curr] = true;
|
||||
|
|
Loading…
Reference in a new issue