-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
193 lines (167 loc) · 4.44 KB
/
index.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const EMPTYARR = []
const SHORTSPLIT = /$|[!-@[-`{-~][\s\S]*/g
const isArray = Array.isArray
const parseValue = function (any) {
if (any === "") return ""
if (any === "false") return false
const maybe = +any
return maybe * 0 === 0 ? maybe : any
}
const parseAlias = function (aliases) {
let out = {},
alias,
prev,
any
for (let key in aliases) {
any = aliases[key]
alias = out[key] = isArray(any) ? any : [any]
for (let i = 0; i < alias.length; i++) {
prev = out[alias[i]] = [key]
for (let k = 0; k < alias.length; k++) {
if (i !== k) prev.push(alias[k])
}
}
}
return out
}
const parseDefault = function (aliases, defaults) {
let out = {},
alias,
value
for (let key in defaults) {
alias = aliases[key]
value = defaults[key]
out[key] = value
if (alias === undefined) {
aliases[key] = EMPTYARR
} else {
for (let i = 0; i < alias.length; i++) {
out[alias[i]] = value
}
}
}
return out
}
const parseOptions = function (aliases, options, value) {
let out = {},
key,
alias
if (options !== undefined) {
for (let i = 0; i < options.length; i++) {
key = options[i]
alias = aliases[key]
out[key] = value
if (alias === undefined) {
aliases[key] = EMPTYARR
} else {
for (let k = 0, end = alias.length; k < end; k++) {
out[alias[k]] = value
}
}
}
}
return out
}
const write = function (out, key, value, aliases, unknown) {
let prev,
alias = aliases[key],
len = alias === undefined ? -1 : alias.length
if (len >= 0 || unknown === undefined || unknown(key)) {
prev = out[key]
if (prev === undefined) {
out[key] = value
} else {
if (isArray(prev)) {
prev.push(value)
} else {
out[key] = [prev, value]
}
}
for (let i = 0; i < len; i++) {
out[alias[i]] = out[key]
}
}
}
export default function (argv, opts) {
let unknown = (opts = opts || {}).unknown,
aliases = parseAlias(opts.alias),
strings = parseOptions(aliases, opts.string, ""),
values = parseDefault(aliases, opts.default),
bools = parseOptions(aliases, opts.boolean, false),
stopEarly = opts.stopEarly,
_ = [],
out = { _ },
key,
arg,
end,
match,
value
for (let i = 0, len = argv.length; i < len; i++) {
arg = argv[i]
if (arg[0] !== "-" || arg === "-") {
if (stopEarly) {
while (i < len) {
_.push(argv[i++])
}
} else {
_.push(arg)
}
} else if (arg === "--") {
while (++i < len) {
_.push(argv[i])
}
} else if (arg[1] === "-") {
end = arg.indexOf("=", 2)
if (arg[2] === "n" && arg[3] === "o" && arg[4] === "-") {
key = arg.slice(5, end >= 0 ? end : undefined)
value = false
} else if (end >= 0) {
key = arg.slice(2, end)
value =
bools[key] !== undefined ||
(strings[key] === undefined
? parseValue(arg.slice(end + 1))
: arg.slice(end + 1))
} else {
key = arg.slice(2)
value =
bools[key] !== undefined ||
(len === i + 1 || argv[i + 1][0] === "-"
? strings[key] === undefined
? true
: ""
: strings[key] === undefined
? parseValue(argv[++i])
: argv[++i])
}
write(out, key, value, aliases, unknown)
} else {
SHORTSPLIT.lastIndex = 2
match = SHORTSPLIT.exec(arg)
end = match.index
value = match[0]
for (let k = 1; k < end; k++) {
write(
out,
(key = arg[k]),
k + 1 < end
? strings[key] === undefined ||
arg.substring(k + 1, (k = end)) + value
: value === ""
? len === i + 1 || argv[i + 1][0] === "-"
? strings[key] === undefined || ""
: bools[key] !== undefined ||
(strings[key] === undefined ? parseValue(argv[++i]) : argv[++i])
: bools[key] !== undefined ||
(strings[key] === undefined ? parseValue(value) : value),
aliases,
unknown
)
}
}
}
for (let key in values) if (out[key] === undefined) out[key] = values[key]
for (let key in bools) if (out[key] === undefined) out[key] = false
for (let key in strings) if (out[key] === undefined) out[key] = ""
return out
}