mrmBot-Matrix/utils/parseCommand.js

112 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-07-02 15:52:44 +00:00
module.exports = (input) => {
if (typeof input === "string") input = input.split(/\s+/g);
2021-05-17 21:49:21 +00:00
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-07-02 15:52:44 +00:00
if (a.startsWith("--") && !curr) {
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;
2021-07-11 20:40:31 +00:00
if (arg !== "_") {
if (value.startsWith("\"")) {
if (value.endsWith("\"")) {
args[arg] = value.slice(1).slice(0, -1);
} else {
args[arg] = `${value.slice(1)} `;
ended = false;
}
} else if (value.endsWith("\"")) {
args[arg] += a.slice(0, -1);
} else if (value !== "") {
args[arg] = value;
2021-06-15 03:19:10 +00:00
} else {
2021-07-11 20:40:31 +00:00
args[arg] = true;
2021-06-15 03:19:10 +00:00
}
2021-07-11 20:40:31 +00:00
if (args[arg] === "true") {
args[arg] = true;
} else if (args[arg] === "false") {
args[arg] = false;
}
if (!ended) curr = arg;
2021-06-06 00:55:40 +00:00
} else {
2021-07-11 20:40:31 +00:00
args[a.slice(2)] = true;
2021-07-02 15:52:44 +00:00
}
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
}
} else {
2021-06-06 00:55:40 +00:00
if (concated !== "") {
concated += `${a} `;
} else {
args._.push(a);
}
}
2021-05-17 21:49:21 +00:00
}
if (curr && args[curr] == "") {
args[curr] = true;
}
2021-05-17 21:49:21 +00:00
return args;
};
// /*
// 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;
// }