diff options
Diffstat (limited to '')
-rwxr-xr-x | _.lua | 47 | ||||
-rwxr-xr-x | test_.lua | 7 |
2 files changed, 38 insertions, 16 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 |
@@ -13,7 +13,7 @@ This is part of the blah blah blah... | |||
13 | 13 | ||
14 | local options = | 14 | local options = |
15 | { | 15 | { |
16 | start = {help = 'Command to start the scanning process, for Sys V init.',}, | 16 | start = {help = 'Command to start the scanning process, for Sys V init.', value = 'blah'}, |
17 | restart = {start}, | 17 | restart = {start}, |
18 | ['force-reload'] = {start}, | 18 | ['force-reload'] = {start}, |
19 | status = {help = 'Command to check the status of the scanning process, for Sys V init.',}, | 19 | status = {help = 'Command to check the status of the scanning process, for Sys V init.',}, |
@@ -55,7 +55,10 @@ local options = | |||
55 | ['--help'] = {help}, | 55 | ['--help'] = {help}, |
56 | } | 56 | } |
57 | 57 | ||
58 | _.parse(arg, options) | 58 | print("start = " .. options.start.value) |
59 | _.parse(arg, options, 'test_') | ||
60 | print("start = " .. options.start.value) | ||
61 | print("stop = " .. options.stop.value) | ||
59 | 62 | ||
60 | 63 | ||
61 | 64 | ||