-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
253 lines (248 loc) · 11.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const { isOption, findGroupAndCommand, findOption, isOptionPassed, findCommandByFullPath, findLongOption } = require('./src/argv')
const { parseValue } = require('./src/parse')
const { completePath } = require('./src/path')
const {
printCommandUsage,
printGroups,
printCommands,
maybePrintOptionFamily,
createDefaultPrinter,
createDefaultApplication,
getHeadlineMessage,
printUsage
} = require('./src/print')
const { suggest } = require('./src/suggest')
const { tokenize } = require('./src/tokenize')
const { Group, Command } = require('./src/type')
const { detectShell, generateCompletion, getShellPaths } = require('./src/shell')
function findOptionTargets(context, option) {
if (option.global) {
return context.sibling ? [context.options, context.sibling.options] : [context.options]
} else {
const targets = []
if (findLongOption(context.command.options, option.key)) {
targets.push(context.options)
}
if (context.sibling && findLongOption(context.sibling.command.options, option.key)) {
targets.push(context.sibling.options)
}
return targets
}
}
function findArgumentTarget(context, commandArguments) {
if (context.sibling && context.argumentIndex < context.sibling.command.arguments.length) {
return context.sibling.arguments
} else if (context.argumentIndex < commandArguments.length) {
return context.arguments
}
}
function createParser(options) {
const printer = (options && options.printer) || createDefaultPrinter()
const application = (options && options.application) || createDefaultApplication()
const pathResolver = (options && options.pathResolver) || completePath
const groups = []
const commands = []
const globalOptions = []
const handleError = (context, reason, silent) => {
if (!context.group && !context.command) {
printer.print(getHeadlineMessage(application))
printer.print('')
printUsage(printer, application)
printer.print('')
printGroups(printer, application, groups)
printCommands(printer, application, commands)
maybePrintOptionFamily(printer, 'Global Options', globalOptions)
} else if (!context.command) {
printUsage(printer, application, context.group)
printer.print('')
printCommands(printer, application, context.group.commands)
maybePrintOptionFamily(printer, 'Global Options', globalOptions)
} else {
const siblingOptions = context.sibling ? context.sibling.command.options : []
const options = [...globalOptions, ...context.command.options, ...siblingOptions]
const siblingArguments = context.sibling ? context.sibling.command.arguments : []
const commandArguments = [...siblingArguments, ...context.command.arguments]
printCommandUsage(printer, application, context.command, options, commandArguments)
}
if (!context.options.help && !silent) {
printer.printHeading('Command failed with reason:')
printer.print('')
printer.printError(reason)
}
return reason
}
return {
addGlobalOption: option => {
option.global = true
globalOptions.push(option)
},
addGroup: group => {
groups.push(group)
},
addCommand: command => {
commands.push(command)
},
suggest: (line, offset = 0, trailing = '') => {
return suggest(line, offset, [...groups, ...commands], globalOptions, pathResolver, trailing)
},
parse: async argv => {
const context = {
options: {},
arguments: {},
sourcemap: {},
argumentIndex: 0
}
const findResult = findGroupAndCommand(argv, groups, commands)
context.group = findResult.group
context.command = findResult.command
for (const option of globalOptions) {
if (isOptionPassed(argv, option)) {
if (option.handler) {
await option.handler(context)
return { exitReason: option.key }
}
if (option.key === 'help') {
context.options.help = true
}
}
}
if (!context.group && !context.command) {
if (argv.join(' ') === '') {
return handleError(context, 'No group or command specified', true)
} else {
return handleError(context, 'Not a valid group or command: ' + argv[0])
}
}
if (context.group && !context.command) {
if (argv.join(' ') === context.group.fullPath) {
return handleError(context, `You need to specify a command in group [${context.group.key}]`, true)
} else {
const depth = context.group.fullPath.split(' ').length
return handleError(context, 'Not a valid command in group [' + context.group.key + ']: ' + argv[depth])
}
}
context.fn = () => context.command.fn(context)
const sibling = context.command.sibling ? findCommandByFullPath(groups, commands, context.command.sibling) : null
if (context.command.sibling && !sibling) {
return handleError(context, 'Expected sibling command not found!')
}
if (sibling) {
context.sibling = {
command: sibling,
arguments: {},
options: {}
}
}
const siblingOptions = sibling ? sibling.options : []
const options = [...globalOptions, ...context.command.options, ...siblingOptions]
const siblingArguments = sibling ? sibling.arguments : []
const commandArguments = [...siblingArguments, ...context.command.arguments]
if (context.options.help) {
printCommandUsage(printer, application, context.command, options, commandArguments)
return { exitReason: 'help' }
}
const skipAmount = context.command.depth + 1
for (let i = skipAmount; i < argv.length; i++) {
const current = argv[i]
const next = argv[i + 1]
if (isOption(current)) {
const option = findOption(options, current)
if (!option) {
return handleError(context, 'Unexpected option: ' + current)
}
const parseResult = parseValue(option, next)
if (parseResult instanceof Error) {
return handleError(context, parseResult.message)
}
const targets = findOptionTargets(context, option)
for (const target of targets) {
if (target[option.key] !== undefined) {
return handleError(context, 'Option passed more than once: ' + option.key)
}
target[option.key] = parseResult.value
context.sourcemap[option.key] = 'explicit'
}
i += parseResult.skip
} else {
const target = findArgumentTarget(context, commandArguments)
if (!target) {
return handleError(context, 'Unexpected argument: ' + current)
}
const argument = commandArguments[context.argumentIndex++]
const parseResult = parseValue(argument, current)
if (parseResult instanceof Error) {
return handleError(context, parseResult.message)
}
target[argument.key] = parseResult.value
context.sourcemap[argument.key] = 'explicit'
}
}
for (const option of options) {
if (!option.envKey) {
continue
}
if (process.env[option.envKey] === undefined) {
continue
}
const parseResult = parseValue(option, process.env[option.envKey])
if (parseResult instanceof Error) {
return handleError(context, parseResult.message)
}
const targets = findOptionTargets(context, option)
for (const target of targets) {
if (target[option.key] !== undefined) {
continue
}
target[option.key] = parseResult.value
context.sourcemap[option.key] = 'env'
}
}
context.argumentIndex = 0
for (const commandArgument of commandArguments) {
const target = findArgumentTarget(context, commandArguments)
context.argumentIndex++
if (target[commandArgument.key] === undefined) {
if (commandArgument.default !== undefined) {
target[commandArgument.key] = commandArgument.default
context.sourcemap[commandArgument.key] = 'default'
} else if (commandArgument.required && !commandArgument.noErrors) {
const silent = argv.join(' ') === context.command.fullPath
return handleError(context, `Required argument [${commandArgument.key}] is not provided`, silent)
}
}
}
for (const option of options) {
const targets = findOptionTargets(context, option)
if (option.default !== undefined && (!option.conflicts || context.options[option.conflicts] === undefined)) {
for (const target of targets) {
if (target[option.key] !== undefined) {
continue
}
target[option.key] = option.default
context.sourcemap[option.key] = 'default'
}
}
if (option.conflicts && context.options[option.key] && context.options[option.conflicts]) {
return handleError(context, option.key + ' and ' + option.conflicts + ' are incompatible, please only specify one.')
}
if (option.required && !option.noErrors) {
for (const target of targets) {
if (
(target[option.key] === undefined ||
(context.sourcemap[option.key] === 'default' &&
target[option.required.when] &&
context.sourcemap[option.required.when] !== 'default')) &&
(!option.conflicts || !target[option.conflicts]) &&
(!option.required.when || target[option.required.when]) &&
(!option.required.unless || !target[option.required.unless])
) {
return handleError(context, 'Required option not provided: ' + option.key)
}
}
}
}
return context
}
}
}
module.exports = { tokenize, createParser, Group, Command, getShellPaths, generateCompletion, detectShell }