mrmBot-Matrix/utils/parseCommand.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

export default (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];
if ((a.startsWith("--") || a.startsWith("—")) && !curr) {
2021-06-06 00:55:40 +00:00
if (a.includes("=")) {
const [arg, value] = (a.startsWith("--") ? a.slice(2).split("=") : a.slice(1).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-07-02 15:52:44 +00:00
}
} 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
}
} 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"}]
// */
// export default (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;
// }