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 ++++++++++++++++++++++++---- aataaj.lua | 15 +++------------ test_.lua | 12 +++--------- 3 files changed, 30 insertions(+), 25 deletions(-) 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) diff --git a/aataaj.lua b/aataaj.lua index 6108dd5..5e45525 100755 --- a/aataaj.lua +++ b/aataaj.lua @@ -17,7 +17,7 @@ local _ = require '_' -local Help = [[ +Help = [[ This is part of the AllAudioToALSAandJACK project, aataaj for short, pronounced like "attach". @@ -81,7 +81,7 @@ NOTE - Seems both ALSA and JACK are per user. So you need to run "aataaj JACK" for each user. ]] - +Version = '0.0 crap' local options = @@ -107,7 +107,7 @@ local options = STOP = { help = 'Command to stop the JACK stuff, for users.', - func = function(a, args, i) + func = function(self, a, args, i) _.killEmAll{'qsynth'} __[[#!/bin/bash a2j_control --stop @@ -132,15 +132,6 @@ local options = os.exit(0) end }, - help = - { - help = 'Print help.', - func = function(a, args, i) - print(Help) - os.exit(0) - end - }, - ['--help'] = {help}, } _.parse(arg, options) diff --git a/test_.lua b/test_.lua index 8d52896..82c418f 100755 --- a/test_.lua +++ b/test_.lua @@ -6,10 +6,11 @@ local _ = require '_' I'Starting from the top.' -local Help = [[ +Help = [[ This is part of the blah blah blah... ]] +Version = '0.0 crap' local options = { @@ -20,7 +21,7 @@ local options = stop = {help = 'Command to stop the scanning process, for Sys V init.',}, JACK = {help = 'Command to start the JACK stuff, for users.',}, STOP = {help = 'Command to stop the JACK stuff, for users.', - func = function(a, args, i) + func = function(self, a, args, i) --[=[ _.killEmAll{'qsynth'} __[[ @@ -46,13 +47,6 @@ local options = ]=] end }, - help = {help = 'Print help.', - func = function(a, args, i) - print(Help) - os.exit(0) - end - }, - ['--help'] = {help}, } print("start = " .. options.start.value) -- cgit v1.1