-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
55 lines (53 loc) · 1.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export type OptionDefinitions = {
name: string;
type: (t: unknown) => unknown;
aliases?: string[];
default?: boolean;
}[];
export interface Result {
_unknown: {
_?: string;
[key: string]: unknown;
};
[key: string]: unknown;
}
export function bargs(
options: OptionDefinitions,
argv: string[] = process.argv.slice(2),
): Result {
if (options.filter((i) => i.default).length > 1)
throw new Error("More than one default option is not allowed");
const result: Result = { _unknown: {} };
const aliases: { [alias: string]: string } = {};
const splitargv = argv
.join(" ")
.split(/((--?))(?=\w)/g)
.filter((i) => !!i && i != "-" && i != "--")
.map((i) => i.trim());
for (const index in options) {
const option = options[index];
if (!option.name)
throw new Error("Option name should not be an empty string");
if (!option.aliases || !option.aliases.length) continue;
for (const alias of option.aliases as string[]) {
aliases[alias] = option.name;
}
}
for (const key of splitargv) {
const split = key.split(" ");
let name = split.shift() as string;
if (!argv.includes("-" + name) && !argv.includes("--" + name)) {
const defaultOption = options.find((option) => option.default);
if (defaultOption)
result[defaultOption.name] = defaultOption.type(key);
else result._unknown._ = key;
} else {
if (aliases[name]) name = aliases[name] as string;
const option = options.find((option) => option.name == name);
const value = split.join(" ") || true;
if (!option) result._unknown[name] = value;
else result[name] = option.type(value);
}
}
return result;
}