diff options
Diffstat (limited to '_.lua')
-rwxr-xr-x | _.lua | 47 |
1 files changed, 33 insertions, 14 deletions
@@ -25,24 +25,43 @@ E = function(s) print('ERROR ' .. s); io.flush() end | |||
25 | C = function(s) print('CRITICAL ' .. s); io.flush() end | 25 | C = function(s) print('CRITICAL ' .. s); io.flush() end |
26 | 26 | ||
27 | 27 | ||
28 | _.parse = function(args, options) | 28 | _.parse = function(args, options, confFile) |
29 | --[[ TODO - use this for config files as well. | ||
30 | An optional argument, the base config file name to look for. In this order, with later ones overriding earlier ones - | ||
31 | defaults from the options table | ||
32 | /etc/base.conf.lua | ||
33 | ~/.base.conf.lua | ||
34 | command line arguments | ||
35 | ]] | ||
36 | local o = nil | 29 | local o = nil |
30 | |||
31 | local doIt = function(name, val, a, args, i) | ||
32 | local o = options[name] | ||
33 | if nil ~= o then | ||
34 | if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end | ||
35 | if nil ~= o.func then o:func(a, args, i) end | ||
36 | end | ||
37 | return o | ||
38 | end | ||
39 | |||
40 | if nil ~= confFile then | ||
41 | for i,v in ipairs{'/etc/', '~/.', './.'} do | ||
42 | local p = v .. confFile .. '.conf.lua' | ||
43 | local h, e = io.open(p, "r") | ||
44 | if nil ~= h then | ||
45 | D('Found configuration file '.. p) | ||
46 | h:close() | ||
47 | local ar = dofile(p) | ||
48 | for k, w in pairs(ar) do | ||
49 | o = doIt(k, w, k .. '=' .. tostring(w), args, i) | ||
50 | if nil == o then E('config variable not found ' .. k .. ' = ' .. tostring(w)) end | ||
51 | end | ||
52 | end | ||
53 | end | ||
54 | end | ||
55 | |||
37 | if 0 ~= #args then | 56 | if 0 ~= #args then |
38 | for i,a in ipairs(args) do | 57 | for i,a in ipairs(args) do |
39 | local s, e = a:find("=") | ||
40 | if nil == s then e = 0 end | ||
41 | opt = a:sub(1, e - 1) | ||
42 | o = options[opt] | ||
43 | D('Argument ' .. i .. ' = ' .. a) | 58 | D('Argument ' .. i .. ' = ' .. a) |
44 | if nil ~= o then | 59 | local s, e = a:find("=") |
45 | if nil ~= o.func then o:func(a, args, i) end | 60 | if nil == s then |
61 | e = 0 | ||
62 | o = doIt(a:sub(1, e - 1), nil, a, args, i) | ||
63 | else | ||
64 | o = doIt(a:sub(1, e - 1), a:sub(e + 1, -1), a, args, i) | ||
46 | end | 65 | end |
47 | end | 66 | end |
48 | end | 67 | end |