2021-06-15 03:19:10 +00:00
|
|
|
module.exports = (input, allowed) => {
|
2021-05-17 21:49:21 +00:00
|
|
|
input = input.split(" ");
|
|
|
|
const args = { _: [] };
|
|
|
|
let curr = null;
|
2021-06-06 00:55:40 +00:00
|
|
|
let concated = "";
|
2021-05-17 21:49:21 +00:00
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
const a = input[i];
|
2021-06-15 03:19:10 +00:00
|
|
|
if (a.startsWith("--") && !curr && isAllowed(a.slice(2).split("=")[0], allowed)) {
|
2021-06-06 00:55:40 +00:00
|
|
|
if (a.includes("=")) {
|
2021-06-19 00:10:13 +00:00
|
|
|
const [arg, value] = a.slice(2).split("=");
|
2021-06-06 00:55:40 +00:00
|
|
|
let ended = true;
|
|
|
|
if (value.startsWith("\"")) {
|
2021-06-15 03:19:10 +00:00
|
|
|
if (!value.endsWith("\"")) {
|
|
|
|
args[arg] = value.slice(1).slice(0, -1);
|
|
|
|
} else {
|
|
|
|
args[arg] = `${value.slice(1)} `;
|
|
|
|
ended = false;
|
|
|
|
}
|
2021-06-06 00:55:40 +00:00
|
|
|
} 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;
|
2021-05-17 21:49:21 +00:00
|
|
|
}
|
|
|
|
} else if (curr) {
|
2021-06-06 00:55:40 +00:00
|
|
|
if (a.endsWith("\"")) {
|
2021-05-17 21:49:21 +00:00
|
|
|
args[curr] += a.slice(0, -1);
|
|
|
|
curr = null;
|
|
|
|
} else {
|
2021-06-06 00:55:40 +00:00
|
|
|
args[curr] += `${a} `;
|
2021-05-17 21:49:21 +00:00
|
|
|
}
|
2021-06-06 00:55:40 +00:00
|
|
|
} else if (a.startsWith("\"")) {
|
|
|
|
if (a.endsWith("\"")) {
|
|
|
|
args.push(a.slice(1).slice(0, -1));
|
|
|
|
} else {
|
|
|
|
concated += `${a.slice(1)} `;
|
|
|
|
}
|
|
|
|
} else if (a.endsWith("\"")) {
|
|
|
|
concated += a.slice(0, -1);
|
|
|
|
args._.push(concated);
|
|
|
|
concated = "";
|
2021-05-17 21:49:21 +00:00
|
|
|
} else {
|
2021-06-06 00:55:40 +00:00
|
|
|
if (concated !== "") {
|
|
|
|
concated += `${a} `;
|
|
|
|
} else {
|
|
|
|
args._.push(a);
|
|
|
|
}
|
2020-12-26 00:14:10 +00:00
|
|
|
}
|
2021-05-17 21:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (curr && args[curr] == "") {
|
|
|
|
args[curr] = true;
|
|
|
|
}
|
2020-12-26 00:14:10 +00:00
|
|
|
|
2021-05-17 21:49:21 +00:00
|
|
|
return args;
|
|
|
|
};
|
2020-12-26 00:14:10 +00:00
|
|
|
|
2021-06-15 03:19:10 +00:00
|
|
|
const isAllowed = (input, allowed) => {
|
|
|
|
if (!allowed) return true;
|
|
|
|
return allowed.includes(input);
|
|
|
|
};
|
|
|
|
|
2020-12-26 00:14:10 +00:00
|
|
|
// /*
|
|
|
|
// Format:
|
|
|
|
// [{name: "verbose", type: "bool"}, {name: "username", type: "string"}]
|
|
|
|
// */
|
|
|
|
// module.exports = (input, format) => {
|
|
|
|
// let results = {};
|
|
|
|
// let text = input.split(' ').slice(1).join(' ');
|
|
|
|
// format.forEach(element => {
|
|
|
|
// if(element.pos !== undefined) return;
|
|
|
|
// switch (element.type) {
|
|
|
|
// case "bool":
|
|
|
|
// res = text.match(`--${element.name}[ |=](.*?)($| )`);
|
|
|
|
// if(res) {
|
|
|
|
// text = text.replace(res[0], "");
|
|
|
|
// results[element.name] = (res[1].toLowerCase() == "true");
|
|
|
|
// } else {
|
|
|
|
// res = text.match(`--${element.name}`);
|
|
|
|
// if(res) text = text.replace(res[0], "");
|
|
|
|
// results[element.name] = (res != null);
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// case "string":
|
|
|
|
// res = text.match(`--${element.name}[ |=](.*?)($| )`);
|
|
|
|
// if(res) text = text.replace(res[0], "");
|
|
|
|
// results[element.name] = (res ? res[1].replace('\\','') : null);
|
|
|
|
// break;
|
|
|
|
// case "int":
|
|
|
|
// res = text.match(`--${element.name}[ |=](.*?)($| )`);
|
|
|
|
// if(res) text = text.replace(res[0], "");
|
|
|
|
// results[element.name] = (res ? parseInt(res[1]) : null);
|
|
|
|
// break;
|
|
|
|
// case "float":
|
|
|
|
// res = text.match(`--${element.name}[ |=](.*?)($| )`);
|
|
|
|
// if(res) text = text.replace(res[0], "");
|
|
|
|
// results[element.name] = (res ? parseFloat(res[1]) : null);
|
|
|
|
// break;
|
|
|
|
// default:
|
|
|
|
// throw Error("unknown type");
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// let s = text.split(' ');
|
|
|
|
// results._ = text;
|
|
|
|
// format.forEach(element => {
|
|
|
|
// if(element.pos === undefined) return;
|
|
|
|
// if(element.pos <= s.length) {
|
|
|
|
// results[element.name] = s[element.pos];
|
|
|
|
// } else {
|
|
|
|
// results[element.name] = null;
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// return results;
|
|
|
|
// }
|