- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharg_parser.lua
More file actions
Latest commit
393 lines (356 loc) · 11.3 KB
/
Copy patharg_parser.lua
File metadata and controls
393 lines (356 loc) · 11.3 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
localtype_defs= {}
localfunctionregister_type(definition)
type_defs[definition.id] =definition
end
register_type{
id="string",
arg_count=1,
convert=function(arg, context)
localsuccess, value=pcall(tostring, arg)
ifnotsuccessthen
returnnil, value..""..context.."."
end
returnvalue
end,
compare=function(left, right)
returnleft==right
end,
tostring=function(str) returnstrend,
}
register_type{
id="number",
arg_count=1,
convert=function(arg, context)
localsuccess, value=pcall(tonumber, arg)
ifnotsuccessthen
returnnil, value..""..context.."."
end
returnvalue
end,
compare=function(left, right)
returnleft==right
end,
}
register_type{
id="boolean",
arg_count=1,
convert=function(arg, context)
ifarg=="true" orarg=="1" then
returntrue
elseifarg=="false" orarg=="0" then
returnfalse
else
returnnil, "Expected a boolean, got '"..tostring(arg).."' "..context.."."
end
end,
compare=function(left, right)
returnleft==right
end,
}
localhelp_option= {
field="help",
long="help",
short="h",
description="Show help message.",
flag=true,
}
localfunctionget_option_descriptor(option)
return (option.shortand ("-"..option.short) or"")
..(option.longandoption.shortand" | " or"")
..(option.longand ("--"..option.long) or"")
end
---@paramargsstring[]
---@paramconfigArgsConfig
---@paramstart_index?integer @ 1 based including
---@returntable|nil args @ returns `nil` if there was an error
---@returninteger|string last_consumed_index_or_message @ If there was an error this is the error message
localfunctionparse(args, config, start_index)
localerr
locali= (start_indexand (start_index-1)) or0
localcurrent
localfunctionnext_arg()
i=i+1
current=args[i]
returncurrent
end
localfunctionpeek_next()
returnargs[i+1]
end
localfunctionconsume_args_and_convert_for_type(type_id, context)
ifnottype_defs[type_id] then
error("Invalid type_def id '"..type_id.."' "..context..". No type conversion registered with that id.")
end
localtype_args= {}
forj=1, type_defs[type_id].arg_countdo
ifnotnext_arg() then
err="Expected "..j.." arg(s) for the type '"..type_id.."' "..context.."."
return
end
type_args[j] =current
end
type_args[#type_args+1] =context
localresult
result, err=type_defs[type_id].convert(table.unpack(type_args))
returnresult
end
localno_more_options=false
localfunctionconsume_args_and_convert_for_type_array(type_id, min, max, context)
localvalue_count=0
localvalues= {}
while ((notmax) orvalue_count<max)
andpeek_next()
and (no_more_optionsor (value_count<min) or (peek_next():sub(1, 1) ~="-"))
and (noterr)
do
value_count=value_count+1
values[value_count] =consume_args_and_convert_for_type(type_id, context)
end
iferrthenreturnvaluesend
ifvalue_count<minthen
err="Expected "..min..(maxand (max~=minand (" to "..max) or"") or" or more")
.." parameters of the type '"..type_id.."', got "..value_count..""..context.."."
end
returnvalues
end
localresult= {}
localfound_options= {}
localpositional_index=0
localcurrent_positional
localfunctionnext_positional()
positional_index=positional_index+1
current_positional=config.positionalandconfig.positional[positional_index] ornil
returncurrent_positional
end
whilenext_arg() do
ifnotno_more_optionsthen
-- first check for options
ifcurrent:sub(1, 1) =="-" then
localoption_config
ifcurrent:sub(2, 2) =="-" then
locallong=current:sub(3)
iflong=="" then
no_more_options=true
gotocontinue
else
iflong==help_option.longthen
option_config=help_option
else
for_, optioninipairs(config.optionsor {}) do
ifoption.long==longthen
option_config=option
break
end
end
end
end
else
localshort=current:sub(2)
ifshort=="" then
returnnil, "Invalid arg '-'."
end
ifshort==help_option.shortthen
option_config=help_option
else
for_, optioninipairs(config.optionsor {}) do
ifoption.short==shortthen
option_config=option
break
end
end
end
end
ifnotoption_configthen
returnnil, "Invalid option '"..current.."'."
end
iffound_options[option_config] then
returnnil, "Duplicate option '"..get_option_descriptor(option_config).."'."
end
found_options[option_config] =true
ifoption_config.flagthen
result[option_config.field] =true
elseifoption_config.single_paramthen
result[option_config.field] =consume_args_and_convert_for_type(
option_config.type,
"for the option '"..get_option_descriptor(option_config).."'"
)
iferrthen
returnnil, err
end
else
result[option_config.field] =consume_args_and_convert_for_type_array(
option_config.type,
option_config.min_paramsoroption_config.params,
option_config.max_paramsoroption_config.params,
"for the option '"..get_option_descriptor(option_config).."'"
)
iferrthen
returnnil, err
end
end
gotocontinue
end
end
-- positional args
no_more_options=true
ifnotnext_positional() then
break
end
i=i-1-- the consume functions expect the current position to be just before what they are consuming
ifcurrent_positional.singlethen
result[current_positional.field] =consume_args_and_convert_for_type(
current_positional.type,
"for #"..positional_index.." positional arg '"..current_positional.name.."'"
)
iferrthen
returnnil, err
end
else
result[current_positional.field] =consume_args_and_convert_for_type_array(
current_positional.type,
current_positional.min_amountorcurrent_positional.amount,
current_positional.max_amountorcurrent_positional.amount,
"for #"..positional_index.." positional arg group '"..current_positional.name.."'"
)
iferrthen
returnnil, err
end
end
::continue::
end
for_, optioninipairs(config.optionsor {}) do
ifnotfound_options[option] then
ifoption.flagthen
result[option.field] =false
elseifoption.optionalor (option.default_value~=nil) then
result[option.field] =option.default_value
elseifnotresult.helpthen
returnnil, "Missing option '"..get_option_descriptor(option).."'."
end
end
end
whilenext_positional() do
ifcurrent_positional.singlethen
ifnotcurrent_positional.optionalthen
ifnotresult.helpthen
returnnil, "Missing #"..positional_index.." positional arg '"..current_positional.name.."'."
end
end
else
if (notcurrent_positional.optional) andcurrent_positional.min_amount~=0then
ifnotresult.helpthen
returnnil, "Missing #"..positional_index.." positional arg group '"..current_positional.name.."'."
end
end
result[current_positional.field] = {}
end
end
returnresult, i-1
end
localfunctionget_type(option_or_positional)
ifoption_or_positional.flagthen
return""
elseifoption_or_positional.single_paramoroption_or_positional.singlethen
return" <"..option_or_positional.type..">"
else
return" <"..option_or_positional.type.."[]>"
end
end
localfunctionget_help_string(config, help_config)
help_config=help_configor {}
localindent_length=help_config.indent_lengthor4
locallabel_length=help_config.label_lengthor32
localspacing_length=help_config.spacing_lengthor2
localindent=string.rep("", indent_length)
localfull_indent=string.rep("", indent_length+label_length+spacing_length)
localhelp= {}
localfunctionadd_entry(label, description)
help[#help+1] =label
ifdescriptionthen
if#label>label_lengththen
help[#help+1] ="\n"
help[#help+1] =full_indent
else
help[#help+1] =string.rep("", label_length-#label+spacing_length)
end
help[#help+1] =description:gsub("\n", "\n"..full_indent)
end
help[#help+1] ="\n"
end
localfunctiondefault_value_tostring(option)
localtostring= (type_defs[option.type].tostringortostring)
ifoption.single_paramthen
returntostring(option.default_value)
else
localvalues= {}
fori, valueinipairs(option.default_value) do
values[i] =tostring(value)
end
returntable.concat(values, ", ")
end
end
localfunctionadd_option(option)
help[#help+1] =indent
locallabel
ifoption.flagoroption.optionaloroption.default_value~=nilthen
label="["..get_option_descriptor(option)..get_type(option)
..(
option.default_value~=nil
and (" | default: "..default_value_tostring(option))
or""
).."]"
else
label=get_option_descriptor(option)..get_type(option)
end
add_entry(label, option.description)
end
ifhelp_config.usagethen
help[#help+1] ="Usage: "
help[#help+1] =help_config.usage
help[#help+1] ="\n"
end
for_, optioninipairs(config.optionsor {}) do
add_option(option)
end
add_option(help_option)
ifnext(config.optionsor {}) andnext(config.positionalor {}) then
help[#help+1] ="\n"
end
for_, positionalinipairs(config.positionalor {}) do
help[#help+1] =indent
locallabel
ifpositional.optionalthen
label="["..get_type(positional).."]"
else
label=get_type(positional)
end
add_entry(label, positional.description)
end
help[#help] =nil-- remove last newline
returntable.concat(help)
end
---@paramargsstring[]
---@paramconfigArgsConfig
---@paramhelp_config?ArgsHelpConfig
---@returntable|nil args @ returns `nil` if there was an error, `{help = true}` if it was help
---@returninteger|nil last_consumed_index @ returns `nil` under the same condition.
localfunctionparse_and_print_on_error_or_help(args, config, help_config)
localresult, err_or_index=parse--[[@as fun(a,b):table?,integer?]](args, config)
if (notresult) orresult.helpthen
ifnotresultthen
print(err_or_index)
print()
err_or_index=nil
else
result= {help=true}
end
print(get_help_string(config, help_config))
end
returnresult, err_or_index
end
return {
register_type=register_type,
parse=parse,
parse_and_print_on_error_or_help=parse_and_print_on_error_or_help,
get_help_string=get_help_string,
help_option=help_option, -- one can modify this table by reference
}