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
|
local helpstr = 'usage: parsav [-' .. flags .. '] [<arg>...]\n'
options.entries = {
{field = 'arglist', type = lib.mem.ptr(rawstring)}
}
local shortcases, longcases, init = {}, {}, {}
local self = symbol(&options)
local arg = symbol(rawstring)
local skip = label()
for o,desc in pairs(tbl) do
options.entries[#options.entries + 1] = {
field = o, type = bool
}
helpstr = helpstr .. string.format(' -%s --%s: %s\n',
desc[1], o, desc[2])
end
for o,desc in pairs(tbl) do
local flag = desc[1]
init[#init + 1] = quote [self].[o] = false end
shortcases[#shortcases + 1] = quote
case [int8]([string.byte(flag)]) then [self].[o] = true end
end
longcases[#longcases + 1] = quote
if lib.str.cmp([arg]+2, o) == 0 then
[self].[o] = true
goto [skip]
end
end
end
options.methods.parse = terra([self], argc: int, argv: &rawstring)
[init]
var parseopts = true
self.arglist = lib.mem.heapa(rawstring, argc)
var finalargc = 0
for i=0,argc do
var [arg] = argv[i]
if arg[0] == ('-')[0] and parseopts then
if arg[1] == ('-')[0] then -- long option
if arg[2] == 0 then -- last option
parseopts = false
else [longcases] end
else -- short options
|
>
>
>
|
<
>
>
|
>
>
>
>
>
>
|
|
<
<
|
|
<
>
>
|
|
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
|
local helpstr = 'usage: parsav [-' .. flags .. '] [<arg>...]\n'
options.entries = {
{field = 'arglist', type = lib.mem.ptr(rawstring)}
}
local shortcases, longcases, init = {}, {}, {}
local self = symbol(&options)
local arg = symbol(rawstring)
local idxo = symbol(uint)
local skip = label()
local sanitize = function(s) return s:gsub('_','-') end
for o,desc in pairs(tbl) do
local consume = desc[3] or 0
options.entries[#options.entries + 1] = {
field = o, type = (consume > 0) and uint or bool
}
helpstr = helpstr .. string.format(' -%s --%s: %s\n',
desc[1], sanitize(o), desc[2])
end
for o,desc in pairs(tbl) do
local flag = desc[1]
local consume = desc[3] or 0
init[#init + 1] = quote [self].[o] = [consume > 0 and 0 or false] end
local ch if consume > 0 then ch = quote
[self].[o] = idxo
idxo = idxo + consume
end else ch = quote
[self].[o] = true
end end
shortcases[#shortcases + 1] = quote
case [int8]([string.byte(flag)]) then [ch] end
end
longcases[#longcases + 1] = quote
if lib.str.cmp([arg]+2, [sanitize(o)]) == 0 then [ch] goto [skip] end
end
end
terra options:free() self.arglist:free() end
options.methods.parse = terra([self], argc: int, argv: &rawstring)
[init]
var parseopts = true
var [idxo] = 1
self.arglist = lib.mem.heapa(rawstring, argc)
var finalargc = 0
for i=1,argc do
var [arg] = argv[i]
if arg[0] == ('-')[0] and parseopts then
if arg[1] == ('-')[0] then -- long option
if arg[2] == 0 then -- last option
parseopts = false
else [longcases] end
else -- short options
|