-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
178 lines (136 loc) · 5.29 KB
/
parser.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
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
import type { CommandArgs, ParsedContext } from './types'
import { processError } from './utils/error'
import { isBoolean, isNotEmptyArray, isNotEmptyObject, isNumber, isString, isUndefined } from './utils/is'
import { isSingleChar, kebabCase } from './utils/string'
export interface ParseOptions<Args extends CommandArgs> {
args?: CommandArgs & Args
argv?: string[]
handleError?: ((error: unknown) => unknown) | boolean
}
export function parse<Args extends CommandArgs>(
options: ParseOptions<Args> = {}
) {
const { argv, args, handleError } = { argv: process.argv.slice(2), ...options }
const maps = {
name: {} as Record<string, string>,
alias: {} as Record<string, string>
}
const result: ParsedContext<CommandArgs> = {
argv: {
end: [],
unknown: {},
positional: []
},
args: {}
}
try {
if (isNotEmptyObject(args)) {
for (const [argName, argOptions] of Object.entries(args)) {
const { type, defaultValue } = argOptions
if ((!isUndefined(defaultValue)) && (
(type === 'boolean' && !isBoolean(defaultValue)) ||
(type === 'string' && !isString(defaultValue)) ||
(type === 'positional' && !isString(defaultValue)) ||
(type === 'number' && !isNumber(defaultValue))
)) {
const expectedType = type === 'positional' ? 'string' : type
throw new Error(`The \`defaultValue\` of argument "${argName}" must be one of: ${expectedType} or undefined`)
}
if (type === 'positional') {
continue
}
const { alias } = argOptions
if (!['boolean', 'number', 'string'].includes(type)) {
throw new Error(`The \`type\` of argument "${argName}" must be one of: "boolean", "number", "string", or "positional"`)
}
const kebabCaseName = kebabCase(argName)
if (!isUndefined(maps.name[argName]) || !isUndefined(maps.name[kebabCaseName])) {
const errorName = maps.name[argName === kebabCaseName ? argName : kebabCaseName]
throw new Error(`Invalid argument "${argName}": collides with argument "${errorName}"`)
}
maps.name[argName] = argName
if (argName !== kebabCaseName) {
maps.name[kebabCaseName] = argName
}
if (!isUndefined(alias)) {
if (!isSingleChar(alias)) {
throw new Error(`The \`alias\` of argument "${argName}" must be a single char`)
}
if (!isUndefined(maps.alias[alias])) {
throw new Error(`Duplicate alias "${alias}": argument "${argName}" collides with argument "${maps.alias[alias]}"`)
}
maps.alias[alias] = argName
}
}
}
if (isNotEmptyArray(argv)) {
let skipNext = false
for (const [index, value] of argv.entries()) {
if (skipNext) {
skipNext = false
continue
}
if (value === '--') {
const val = argv.slice(index + 1)
result.argv.end.push(...val)
result.argv.positional.push(...val)
break
}
const dashCount = value.match(/^-+/)?.[0]?.length ?? 0
const isPositionalArg = dashCount === 0
const isAliasedArg = dashCount === 1
const isNamedArg = dashCount === 2
if (isPositionalArg || (!isAliasedArg && !isNamedArg)) {
result.argv.positional.push(value)
continue
}
const equalsIndex = value.indexOf('=')
const hasEquals = equalsIndex > 0
const key = value.slice(dashCount, hasEquals ? equalsIndex : value.length)
const isUnknown = !(isAliasedArg && !isUndefined(maps.alias[key])) && !(isNamedArg && !isUndefined(maps.name[key]))
const nextValue = argv[index + 1]
const val = hasEquals
? (value.slice(equalsIndex + 1))
: (isUnknown || !nextValue || (nextValue.startsWith('-') && nextValue.length > 1) ? true : nextValue)
if (isUnknown) {
result.argv.unknown[key] = val
continue
}
const name = (isAliasedArg ? maps.alias[key] : maps.name[key])!
const { type } = args![name]!
if (type === 'boolean') {
result.args[name] = hasEquals ? val !== 'false' : true
}
if (type === 'number') {
result.args[name] = Number(val)
}
if (type === 'string') {
result.args[name] = val
}
if (!hasEquals && type !== 'boolean') {
skipNext = true
}
}
}
if (isNotEmptyObject(args)) {
for (const [argName, argOptions] of Object.entries(args)) {
const { type, defaultValue, isRequired } = argOptions
if (type === 'positional') {
result.args[argName] = result.argv.positional.shift()
}
if (isUndefined(result.args[argName])) {
result.args[argName] = defaultValue
}
if (isRequired && isUndefined(result.args[argName])) {
const errorArgType = type === 'positional' ? 'argument' : 'option'
const errorArgName = (type === 'positional' ? '' : '--') + kebabCase(argName)
const errorMessage = `Missing required ${errorArgType}: ${errorArgName}`
throw new Error(errorMessage)
}
}
}
} catch (error) {
processError(error, handleError)
}
return result as ParsedContext<Args>
}