forked from lunarmodules/lua_cliargs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcliargs.lua
More file actions
Latest commit
410 lines (338 loc) · 10.9 KB
/
Copy pathcliargs.lua
File metadata and controls
410 lines (338 loc) · 10.9 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
localcli= {}
-- ------- --
-- Helpers --
-- ------- --
localexpand=function(str, size, fill)
ifnotfillthenfill='' end
localout=str
fori=0,size-#strdo
out=out..fill
end
returnout
end
localsplit=function(str, pat)
localt= {}
localfpat="(.-)" ..pat
locallast_end=1
locals, e, cap=str:find(fpat, 1)
whilesdo
ifs~=1orcap~="" then
table.insert(t,cap)
end
last_end=e+1
s, e, cap=str:find(fpat, last_end)
end
iflast_end<=#strthen
cap=str:sub(last_end)
table.insert(t, cap)
end
returnt
end
localjoin=function(t, delim)
out=""
i=0
for_,iteminpairs(t) do
out=item..delim
end
out=out:sub(0, #out-#delim)
returnout
end
localdelimit=function(str, size, pad)
ifnotpadthenpad=0end
localout=""
localwords=split(str, '')
localoffset=0
forword_idx,wordinpairs(words) do
out=out..word..''
offset=offset+#word
ifoffset>sizeandword_idx~=#wordsthen
out=out..'\n'
fori=0,paddoout=out..''; end
offset=0
end
end
returnout
end
-- -------- --
-- CLI Main --
-- -------- --
--- Creates a new instance of the CLI arguments parser and consumes `arg`.
---
--- **Note**: you don't need to invoke this directly as it is automatically
--- done when you `require("cli")`.
---
--- ### Parameters
--- 1. **name**: The name of the application, used in usage and help listings
functioncli:new(name)
localo= {}
setmetatable(o, { __index=self })
self.__index=self
o.name=name
o.required=required_argsor {}
o.optional=optional_argsor {}
o.args=arg
o.colsz= { 20, 45 }
returno
end
functioncli:error(msg)
print(self.name..": error: " ..msg..'; re-run with --help for usage.')
returnfalse
end
--- Assigns the name of the program which will be used for logging.
functioncli:set_name(name)
self.name=name
end
--- Defines a required argument.
--- Required arguments have no special notation and are order-sensitive.
--- *Note:* if `@ref` is omitted, the value will be stored in `args[@key]`.
--- *Aliases: `add_argument`*
---
--- ### Parameters
--- 1. **key**: the argument's "name" that will be displayed to the user
--- 1. **desc**: a description of the argument
--- 1. **ref**: optional; the table key that will be used to hold the value of this argument
---
--- ### Usage example
--- The following will parse the argument (if specified) and set its value in `args["root_path"]`:
--- `cli:add_arg("root", "path to where root scripts can be found", "root_path")`
functioncli:add_arg(key, desc, ref)
ifnotrefthenref=keyend
table.insert(self.required, { key=key, desc=desc, ref=ref })
end
--- Defines an optional argument.
--- Optional arguments can use 3 different notations, and can accept a value.
--- *Aliases: `add_option`*
---
--- ### Parameters
--- 1. **key**: the argument identifier, can be either `-key`, or `-key, --expanded-key`:
--- if the first notation is used then a value can be defined after a space (`'-key VALUE'`),
--- if the 2nd notation is used then a value can be defined after an `=` (`'key, --expanded-key=VALUE'`).
--- 1. **desc**: a description for the argument to be shown in --help
--- 1. **ref**: *optional*; override where the value will be stored, @see cli:add_arg
--- 1. **default**: *optional*; specify a default value (the default is "")
---
--- ### Usage example
--- The following option will be stored in `args["i"]` with a default value of `my_file.txt`:
--- `cli:add_option("-i, --input=FILE", "path to the input file", nil, "my_file.txt")`
functioncli:add_opt(key, desc, ref, default)
ifnotrefthen
ifkey:find('%-%-') ==1then
ref=key:gsub("(%-%-)", ""):gsub("(=.*)", "")
else
ref=string.gsub(key, '[%W]', ''):sub(0,1)
end
end
localentry= {
key=key,
expanded_key="",
ref=ref,
value="",
desc=desc,
default=default==niland"" ordefault
}
-- parameterize the key if needed, possible variations:
-- 1. -key
-- 2. -key VALUE
-- 3. -key, --expanded
-- 4. -key, --expanded=VALUE
-- -a, --argument[=VALUE] was passed
ifkey:find(',') then
localk,ek=unpack( split(key, ',') )
ek=ek:gsub('', '')
ifek:find('=') then
ek,v=unpack( split(ek,'=') )
v=v:gsub('', '')
entry.value=v
end
entry.key=k
entry.expanded_key=ek
-- --argument[=VALUE] was passed
elseifkey:find('%-%-') ==1then
ifkey:find('=') then
k,v=unpack( split(key,'=') )
v=v:gsub('', '')
entry.value=v
key=k
end
entry.key=""-- key
entry.expanded_key=key
-- -a[ VALUE] was passed
elseifkey:find('') then
localk,v=unpack( split(key, '') )
k=k:gsub('', '')
v=v:gsub('', '')
entry.key, entry.value=k, v
end
table.insert(self.optional, entry)
end
--- Define a flag argument (on/off). This is a convenience helper for cli.add_opt().
--- See cli.add_opt() for more information.
---
--- ### Parameters
-- 1. **key**: the argument's key
-- 1. **desc**: a description of the argument to be displayed in the help listing
-- 1. **ref**: optionally override where the key which will hold the value
functioncli:add_flag(key, desc, ref)
returnself:add_opt(key, desc, ref, false)
end
functioncli:locate_entry(key)
-- strip the leading -- from the key if it's an expanded one
ifkey:find('%-%-') ==1andkey:find('=') then
key=split(key, '=')[1]
end
for_,entryinipairs(self.optional) do
ifentry.key==keythen
returnentry,false
elseifentry.expanded_key==keythen
returnentry,true
end
end
returnnil, nil
end
--- Parses the arguments found in #arg and returns a table with the populated values.
--- ### Returns
--- 1. a table containing the keys specified when the arguments were defined along with the parsed values.
functioncli:parse_args(dump)
-- missing any required arguments?
if#self.args<#self.requiredthen
self:error("missing arguments, at least " ..#self.required.." argument(s) must be specified")
self:print_usage()
returnfalse
end
-- starts with --help? display the help listing and abort!
ifself.args[1] andself.args[1] =="--help" then
returnself:print_help()
end
-- print("Received " .. #self.args .. " arguments, required: " .. #self.required)
localargs= {} -- returned set
-- set up defaults
for_,entryinipairs(self.optional) do
args[ entry.ref ] =entry.default
end
localreq_idx=1
forarg_idx, arginipairs(arg) do
repeat
ifskipthen
skip=false
break
end
localentry, uses_expanded=self:locate_entry(arg)
-- if it's an optional argument (starts with '-'), it must be listed
ifarg:find('-') ==1andnotentrythen
returnself:error("unknown option " ..arg)
end
-- it's a required argument
ifnotentrythen
-- or it's one too many arguments
ifnotself.required[req_idx] then
returnself:error("too many arguments! Can't map '" ..arg.."'")
end
args[ self.required[req_idx].ref ] =arg
req_idx=req_idx+1
-- it's an optional argument, determine its type and which notation it uses
else
localarg_val=nil
-- it's a flag, using either -f --f notations
if#entry.value==0then
arg_val=true
-- an option using the -option VALUE notation:
elseifnotuses_expandedthen
if#self.args==arg_idxthen
returnself:error("missing argument value in '" ..entry.key.."" ..entry.value.."'")
else
arg_val=self.args[arg_idx+1]
skip=true
end
-- an option using the --option=VALUE notation
else
ifnotarg:find('=') then
return
self:error("missing argument value in '" ..entry.expanded_key..
"', value must be specified using: " ..entry.expanded_key.."=" ..entry.value)
end
-- local v = split(arg, '=')
arg_val=arg:sub(#entry.expanded_key+2,#arg)
-- for i=1,#v do
-- arg_val = v[i] .. "="
-- end
-- table.remove(arg_val, 1)
-- arg_val = join(arg_val, '=')
-- print('joined value: ' .. arg_val)
end
args[ entry.ref ] =arg_val
end
untiltrue
end
ifreq_idx-1<#self.requiredthen
returnself:error("missing required arguments")
end
ifdumpthen
fork,vinpairs(args) doprint("" ..k.." => " ..tostring(v)) end
end
returnargs
end
functioncli:print_usage()
-- print the USAGE heading
localmsg="Usage: " ..self.name
ifself.optionaland#self.optional>0then
msg=msg.." [OPTIONS] "
end
ifself.requiredand#self.required>0then
for_,entryinipairs(self.required) do
localarg_key, arg_desc, arg_name=
entry.key, entry.desc, entry.ref
msg=msg.."" ..arg_key..""
end
end
print(msg)
end
functioncli:print_help()
self:print_usage()
localkeysz=20
localmsg=""
ifself.requiredand#self.required>0then
msg=msg.."\nRequired arguments: \n"
for_,entryinipairs(self.required) do
localarg_key, arg_desc, arg_name=
entry.key, entry.desc, entry.ref
msg=msg..
"" ..expand(arg_key, keysz) ..
arg_desc.."\n"
end
end
ifself.optionaland#self.optional>0then
msg=msg.."\nOptional arguments: \n"
for_,entryinipairs(self.optional) do
localarg_key, arg_desc, arg_name, arg_default, arg_ph=
entry.key, entry.desc, entry.ref, entry.default, entry.value
ifarg_defaultthen
arg_desc=arg_desc.." (default: " ..arg_default..")"
end
localseparator=""
if#entry.expanded_key>0then
arg_key= (#arg_key>0andarg_key..", " or"") ..entry.expanded_key
separator=#entry.value>0and"=" or""
end
ifarg_phthen
arg_key=arg_key..separator..arg_ph
end
msg=msg.."" ..
expand(arg_key, self.colsz[1]) ..
delimit(arg_desc, self.colsz[2], self.colsz[1] +2--[[ margin ]]) ..'\n'
end
end
print(msg)
end
--- Sets the amount of space allocated to the argument keys and descriptions in the help listing.
--- The sizes are used for wrapping long argument keys and descriptions.
--- ### Parameters
--- 1. **key_cols**: the number of columns assigned to the argument keys (default: 20)
--- 1. **desc_cols**: the number of columns assigned to the argument descriptions (default: 45)
functioncli:set_colsz(key_cols, desc_cols)
self.colsz= { key_colsorself.colsz[1], desc_colsorself.colsz[2] }
end
cli.version="1.1-0"
-- aliases
cli.add_argument=cli.add_arg
cli.add_option=cli.add_opt
returncli:new("")