-
Notifications
You must be signed in to change notification settings - Fork 1
/
mst_cliargs.lua
376 lines (341 loc) · 8.59 KB
/
mst_cliargs.lua
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
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: mst_cliargs.lua $
--
-- Author: Markus Stenberg <[email protected]>
--
-- Copyright (c) 2013 cisco Systems, Inc.
--
-- Created: Wed Jul 17 15:15:29 2013 mstenber
-- Last modified: Mon Oct 21 10:05:22 2013 mstenber
-- Edit time: 81 min
--
-- My variant on CLI argument parsing.
-- Notable features:
-- - ~similar to cliargs
-- - handles multiple optional arguments
-- - uses dictionaries instead of lists as arguments, and has somewhat
-- simpler API
-- basic idea:
-- mst_cliargs.parse{o} => args
-- parse arguments contain following:
-- process='process name'
-- error=<what to do on error; by default, call os.exit(1)
-- options = {{[name='option name']
-- [,alias='alias name']
-- [,desc='description for help']
-- [,flag='is a flag?']
-- [,value='value description']
-- [,default='default value']
-- [,convert=<e.g. tonumber>]
-- [,min=N]
-- [,max=N]}...}
-- note: All options should have name, except for one.
-- TODO:
-- value-only option should be sorted to be last s.t. it is used as default
-- (and presence of only one should be checked)
require 'mst'
module(..., package.seeall)
-- -name=
-- -f
-- --name=
-- --flag
-- VALUE
local function option_to_prefix_i(opt, n, eqsign)
-- specific option
if n
then
eqsign = eqsign or '='
local eq = opt.flag and "" or eqsign
local prefix = #n == 1 and '' or '-'
return string.format('-%s%s%s', prefix, n, eq)
end
-- default argument
return ''
end
local function option_to_prefix(opt)
if opt.alias
then
-- show shorter first; the longer later
local shorter = #opt.name > #opt.alias and opt.alias or opt.name
local longer = #opt.name <= #opt.alias and opt.alias or opt.name
return option_to_prefix_i(opt, shorter, '') .. '/' ..
option_to_prefix_i(opt, longer)
end
return option_to_prefix_i(opt, opt.name)
end
-- this wraps prefix[=VALUE] with optionality constraints
local function option_to_sdesc(opt)
local p = option_to_prefix(opt)
local optional
if opt.flag
then
optional = true
elseif not opt.min
then
optional = true
end
if not opt.flag
then
local value = opt.value or opt.name
mst.a(value, 'no value for non-flag option', opt)
p = p .. string.upper(value)
end
if optional
then
return string.format('[%s]', p)
end
return p
end
local function option_to_desc(opt)
local l = {option_to_sdesc(opt)}
if opt.desc
then
table.insert(l, opt.desc)
end
if opt.min and opt.max
then
table.insert(l, string.format('[%d-%d]', opt.min, opt.max))
elseif opt.min
then
table.insert(l, string.format('[>=%d]', opt.min))
elseif opt.max
then
table.insert(l, string.format('[<=%d]', opt.max))
end
if opt.default
then
table.insert(l, string.format('[default=%s]',
mst.repr(opt.default)))
end
return table.concat(l, ' ')
end
cli = mst.create_class{class='cli'}
function cli:init()
self.args = self.arg or arg
self.process = self.process or self.args[0] or '?'
self.print = self.print or print
local opts = self.options or {}
opts = mst.table_deep_copy(opts)
mst.d('opts', opts)
local seen = {}
-- add help handler
table.insert(opts, {
name='help',
flag=1,
desc='Show help for the program',
})
-- insert auto-generated one-letter aliases
mst.d('generating aliases')
for i, opt in ipairs(opts)
do
if opt.name and #opt.name == 1
then
seen[opt.name] = 1
end
if opt.alias and #opt.alias == 1
then
seen[opt.alias] = 1
end
end
for i, opt in ipairs(opts)
do
if opt.name and #opt.name > 1
then
if not opt.alias
then
-- auto-generate alias _if possible_
for i, v in mst.string_ipairs(opt.name)
do
if not seen[v]
then
mst.d('added alias', v, opt)
seen[v] = 1
opt.alias = v
break
end
end
end
end
end
-- create a prefix list. it is ordered in the inverse length of the
-- original option match, so that the default 'value' options
-- (nothing to match) are handled last.
local pl = {}
for i, opt in ipairs(opts)
do
local p1 = option_to_prefix_i(opt, opt.name)
local p2 = option_to_prefix_i(opt, opt.alias)
table.insert(pl, {p1, opt})
if not opt.flag
then
table.insert(pl, {option_to_prefix_i(opt, opt.name, ''), opt})
end
if #p2 > 0
then
table.insert(pl, {p2, opt})
if not opt.flag
then
table.insert(pl, {option_to_prefix_i(opt, opt.alias, '', opt)})
end
end
end
table.sort(pl, function (o1, o2)
return #o1[1] > #o2[1]
end)
self.opts = opts
self.pl = pl
end
function cli:print_help()
local print = self.print
print(string.format('%s %s', self.process,
table.concat(mst.array_map(self.opts,
option_to_sdesc), ' ')))
for i, opt in ipairs(self.opts)
do
print('', option_to_desc(opt))
end
end
function cli:print_help_and_exit()
self:print_help()
self:error()
end
function cli:parse()
local print = self.print
mst.d('parsing arguments', self.args)
local r = {}
local had_error
local topt
local function _to_opt(found, opt)
local n = opt.name or opt.value
if opt.convert
then
local r = opt.convert(found)
if not r
then
print('conversion error', opt, found)
had_error = true
end
found = r
end
if opt.min or opt.max
then
local l = r[n] or {}
r[n] = l
table.insert(l, found)
else
if r[n]
then
print('multiple instances of option', n)
had_error = true
else
r[n] = found
end
end
end
for i, arg in ipairs(self.args)
do
if topt
then
_to_opt(arg, topt)
topt = nil
else
local found
for i, v in ipairs(self.pl)
do
local p, opt = unpack(v)
found = mst.string_startswith(arg, p)
mst.d('considering', arg, p, opt)
if found
then
mst.d('matched', arg, opt, found)
-- store the value, if applicable
local n = opt.name or opt.value
if opt.flag
then
r[n] = true
else
-- there are 3 forms for non opt.flag ones
-- "--flag=value"
-- "--flag" "value"
-- and "value"
if opt.name and string.sub(p, #p) ~= '='
then
topt = opt
found = true
break
end
_to_opt(found, opt)
end
break
end
end
if not found
then
print('unable to parse', arg)
had_error = true
end
end
end
if topt
then
print('trailing option', topt.name)
had_error = true
end
for i, opt in ipairs(self.opts)
do
local n = opt.name or opt.value
if opt.min or opt.max
then
local l = r[n]
if opt.min and opt.min>0
then
if not l or #l < opt.min
then
print('too few arguments to', n)
had_error = true
end
end
if opt.max
then
if l and #l > opt.max
then
print('too many arguments to', n)
had_error = true
end
end
end
end
if r.help or had_error
then
mst.d('showing help', r.help, had_error)
self:print_help_and_exit()
-- in case error is nonfatal, we return nil
return
end
-- set default values based on options
for i, opt in ipairs(self.opts)
do
if opt.default
then
local n = opt.name or opt.value
if not r[n]
then
r[n] = opt.default
end
end
end
return r
end
function cli:error()
os.exit(1)
end
function parse(o)
-- backwards compatible API (woah, the legacy starts early! ;-)
local c = new(o)
return c:parse()
end
function new(...)
return cli:new(...)
end