From f28129b5c96317a0486592011db8e02655b18e1a Mon Sep 17 00:00:00 2001 From: dvs1 Date: Fri, 25 Oct 2024 17:03:00 +1000 Subject: Help command now prints out full usage as well. --- _.lua | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to '_.lua') diff --git a/_.lua b/_.lua index 03761d2..945f4f2 100755 --- a/_.lua +++ b/_.lua @@ -28,14 +28,15 @@ C = function(s) print('CRITICAL ' .. s); io.flush() end local optionsCommon = { - help = {help = 'Print the help text', - func = function(self, a, args, i) + help = {help = 'Print the help text.', + func = function(self, options, a, args, i) print(Help) + _.usage(args, options, true) os.exit(0) end }, ['--version'] = {help = 'Print the version details.', - func = function(self, a, args, i) + func = function(self, options, a, args, i) print('This is version ' ..Version .. ' of ' .. args[0]) os.exit(0) end @@ -43,6 +44,29 @@ local optionsCommon = } optionsCommon['--help'] = optionsCommon['help'] +_.usage = function(args, options, all) + local h = '' + for k, v in pairs(options) do + 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) .. '}') + if true == all then + for k, v in pairs(options) do + if 'table' == type(v) then + if nil ~= v.help then print(k .. '\t\t' .. v.help) end + end + end + for k, v in pairs(optionsCommon) do + if 'table' == type(v) then + if nil ~= v.help then print(k .. '\t\t' .. v.help) end + end + end + end +end + _.parse = function(args, options, confFile) local o = nil @@ -51,7 +75,7 @@ _.parse = function(args, options, confFile) 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 + if nil ~= o.func then o:func(options, a, args, i) end end return o end @@ -85,14 +109,7 @@ _.parse = function(args, options, confFile) end if nil == o then - local h = '' - for k, v in pairs(options) do - 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) .. '}') + _.usage(args, options) os.exit(0) end end -- cgit v1.1