From 5833e7483357daa24b8ddc1f766a6446d0d30495 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Fri, 25 Oct 2024 13:40:21 +1000 Subject: Moved common stuff like --help, --version to an internal options table we scan as well. --- _.lua | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to '_.lua') diff --git a/_.lua b/_.lua index 5c8963b..a2607f5 100755 --- a/_.lua +++ b/_.lua @@ -26,12 +26,29 @@ E = function(s) print('ERROR ' .. s); io.flush() end C = function(s) print('CRITICAL ' .. s); io.flush() end --- TODO - move common stuff like -q, -v, --help, --version to an internal options table we scan as well. +local optionsCommon = +{ + help = {help = 'Print the help text', + func = function(self, a, args, i) + print(Help) + os.exit(0) + end + }, + ['--version'] = {help = 'Print the version details.', + func = function(self, a, args, i) + print('This is version ' ..Version .. ' of ' .. args[0]) + os.exit(0) + end + }, +} +optionsCommon['--help'] = optionsCommon['help'] + _.parse = function(args, options, confFile) local o = nil local doIt = function(name, val, a, args, i) local o = options[name] + if nil == o then o = optionsCommon[name] end if nil ~= o then if nil ~= val then o.value = val; D(name .. ' = ' .. tostring(val)) end if nil ~= o.func then o:func(a, args, i) end @@ -48,8 +65,8 @@ _.parse = function(args, options, confFile) h:close() local ar = dofile(p) for k, w in pairs(ar) do - o = doIt(k, w, k .. '=' .. tostring(w), args, i) - if nil == o then E('config variable not found ' .. k .. ' = ' .. tostring(w)) end + doIt(k, w, k .. '=' .. tostring(w), args, i) + if nil == o then W('config variable not found ' .. k .. ' = ' .. tostring(w)) end end end end @@ -71,7 +88,10 @@ _.parse = function(args, options, confFile) if nil == o then local h = '' for k, v in pairs(options) do - h = h .. k .. ' | ' + if 'table' == type(v) then h = h .. k .. ' | ' end + end + for k, v in pairs(optionsCommon) do + if 'table' == type(v) then h = h .. k .. ' | ' end end print('Usage: ' .. args[0] .. ' {' .. string.sub(h, 1, -2) .. '}') os.exit(0) -- cgit v1.1