wiki-js-cli/source/helpers/args.ts
Fenris Wolf b109c6777b [ini]
2024-09-30 09:11:50 +02:00

72 lines
1.2 KiB
TypeScript

namespace _wiki_js_cli.helpers.args
{
/**
*/
export function parse(
args_raw : Array<string>
)
{
let result : {
positional : Array<
string
>;
volatile : Record<
string,
Array<
string
>
>;
} = {
"positional": [],
"volatile": {},
};
let state : ("free" | "bound") = "free";
let key : (null | string) = null;
args_raw.forEach(
(arg_raw) => {
switch (state) {
case "free": {
if (arg_raw.startsWith("-")) {
key = arg_raw.slice(1);
state = "bound";
}
else {
if (key === null) {
result.positional.push(arg_raw);
key = null;
state = "free";
}
else {
_wiki_js_cli.helpers.log.write(
_wiki_js_cli.helpers.log.enum_level.warning,
"arg_discarded",
{
"arg_raw": arg_raw,
}
);
key = null;
state = "free";
}
}
break;
}
case "bound": {
if (! (key in result["volatile"])) {
result["volatile"][key] = [];
}
else {
// do nothing
}
result["volatile"][key].push(arg_raw);
key = null;
state = "free";
}
}
}
);
return result;
}
}