-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
162 lines (150 loc) · 4.25 KB
/
Copy pathindex.ts
File metadata and controls
162 lines (150 loc) · 4.25 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import type {
Aliases,
Args,
BooleanType,
Collectable,
NestedMapping,
ParseOptions,
StringType,
Values,
} from "./types.js";
export { ParseOptions, Args } from "./types.js";
const BOOL_RE = /^(true|false)$/;
const QUOTED_RE = /^('|").*\1$/;
const set = (obj: NestedMapping, key: string, value: any, type?: string) => {
if (key.includes(".")) {
const parts = key.split(".");
for (let i = 0; i < parts.length - 1; i++) {
const k = parts[i];
const tmp = {};
set(obj, k, tmp);
obj = tmp;
}
key = parts[parts.length - 1];
}
if (type === "array" && obj[key] !== undefined) {
if (Array.isArray(obj[key])) {
(obj[key] as any[]).push(value);
} else {
obj[key] = [obj[key], value];
}
} else {
obj[key] = type === "array" ? [value] : value;
}
};
const type = (
key: string,
opts: Record<"boolean" | "string" | "array", string[]>,
): "boolean" | "string" | "array" | undefined => {
if (opts.array && opts.array.length > 0 && opts.array.includes(key))
return "array";
if (opts.string && opts.string.length > 0 && opts.string.includes(key))
return "string";
if (opts.boolean && opts.boolean.length > 0 && opts.boolean.includes(key))
return "boolean";
return;
};
const defaultValue = (type?: "boolean" | "string" | "array") => {
if (type === "string") return "";
if (type === "array") return [];
return true;
};
const coerce = (value?: string, type?: "string" | "boolean" | "array") => {
if (type === "string") return value;
if (type === "boolean") return value === undefined ? true : value === "true";
if (!value) return value;
if (value.length > 3 && BOOL_RE.test(value)) return value === "true";
if (value.length > 2 && QUOTED_RE.test(value)) return value.slice(1, -1);
if ((value[0] === "." && /\d/.test(value[1])) || /\d/.test(value[0]))
return Number(value);
return value;
};
export function parse<
TArgs extends Values<
TBooleans,
TStrings,
TCollectable,
undefined,
TDefaults,
TAliases
>,
TBooleans extends BooleanType = undefined,
TStrings extends StringType = undefined,
TCollectable extends Collectable = undefined,
TDefaults extends Record<string, unknown> | undefined = undefined,
TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined,
TAliasArgNames extends string = string,
TAliasNames extends string = string,
>(
argv: string[],
{
default: defaults,
alias: aliases,
...types
}: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases> = {},
): Args<TArgs> {
const obj = { ...defaults, _: [] } as unknown as Args<TArgs>;
if (argv.length === 0) return obj;
for (let i = 0; i < argv.length; i++) {
const curr = argv[i];
const next = argv[i + 1];
let t: "string" | "boolean" | "array" | undefined;
let key = "";
let value: string | undefined;
if (curr.length > 1 && curr[0] === "-") {
if (curr[1] !== "-" && curr.length > 2 && !curr.includes("=")) {
if (curr.includes(".")) {
key = curr.slice(1, 2);
value = curr.slice(2);
} else {
const keys = curr.slice(1, -1);
for (let key of keys) {
if (
aliases &&
(aliases as Record<string, any>)[key] !== undefined
) {
key = aliases[key as keyof typeof aliases] as string;
}
set(obj, key, defaultValue(t), t);
}
key = curr.slice(-1);
if (next && next[0] !== "-") {
value = next;
i++;
}
}
} else if (!curr.includes("=") && next && next[0] !== "-") {
key = curr.replace(/^-{1,2}/, "");
t = type(key, types as any);
// treat boolean as flag without parameter
if (t === "boolean") {
value = "true";
} else {
value = next;
i++;
}
} else {
const eq = curr.indexOf("=");
if (eq === -1) {
key = curr.replace(/^-{1,2}/, "");
} else {
key = curr.slice(0, eq).replace(/^-{1,2}/, "");
value = curr.slice(eq + 1);
}
t = type(key, types as any);
}
if ((!t || t === "boolean") && key.length > 3 && key.startsWith("no-")) {
set(obj, key.slice(3), false);
} else {
if (aliases && (aliases as Record<string, any>)[key] !== undefined) {
key = aliases[key as keyof typeof aliases] as string;
}
set(obj, key, coerce(value, t) ?? defaultValue(t), t);
}
} else if (curr) {
(obj as any)._.push(coerce(curr));
continue;
}
}
return obj;
}