From 8e7c1c219589120f2088ce9924907c7d66492312 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Wed, 23 Oct 2024 19:16:01 +1000 Subject: New options parsing stuff. --- _.lua | 45 +++++++++++++++++++++----- test_.lua | 108 ++++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 103 insertions(+), 50 deletions(-) diff --git a/_.lua b/_.lua index 19b73b4..12d7461 100755 --- a/_.lua +++ b/_.lua @@ -6,13 +6,38 @@ local _ = {} _.version = '0.0 crap' -D = function(s) print('DEBUG ' .. s) end -I = function(s) print('INFO ' .. s) end -T = function(s) print('TIMEOUT ' .. s) end -W = function(s) print('WARNING ' .. s) end -E = function(s) print('ERROR ' .. s) end -C = function(s) print('CRITICAL ' .. s) end +D = function(s) print('DEBUG ' .. s); io.flush() end +I = function(s) print('INFO ' .. s); io.flush() end +T = function(s) print('TIMEOUT ' .. s); io.flush() end +W = function(s) print('WARNING ' .. s); io.flush() end +E = function(s) print('ERROR ' .. s); io.flush() end +C = function(s) print('CRITICAL ' .. s); io.flush() end + + +_.parseArgs = function(options, args) + local o = nil + if 0 ~= #args then + for i,a in pairs(args) do + local s, e = a:find("=") + if nil == s then e = 0 end + opt = a:sub(1, e - 1) + o = options[opt] + D('Argument ' .. i .. ' = ' .. a) + if nil ~= o then + o:func(a, args, i) + end + end + end + if nil == o then + local h = '' + for k, v in pairs(options) do + h = h .. k .. '|' + end + print('Usage: aataaj.lua {' .. string.sub(h, 1, -2) .. '}') + os.exit(0) + end +end _.runnable = function(c) return ( 0 == __('which ' .. c):Do().status ) @@ -66,8 +91,12 @@ __ = function(c) elseif 'string' == type(c) then for l in string.gmatch(c, "\n*([^\n]+)\n*") do if '' ~= l then - n = n + 1 - exe.cmd = exe.cmd .. l .. ' ; ' + if ('#!' == l:sub(1,2)) and (n == 0) then + -- TODO - should do something with this, not just run it as shell stuff. + else + n = n + 1 + exe.cmd = exe.cmd .. l .. ' ; ' + end end end end diff --git a/test_.lua b/test_.lua index c9884ea..6784039 100755 --- a/test_.lua +++ b/test_.lua @@ -11,31 +11,59 @@ This is part of the blah blah blah... ]] - -local args = {...} -if 0 ~= #args then --- for i,a in pairs(args) do --- print('Argument ' .. i .. ' = ' .. a) --- end - - if 'start' == args[1] then - elseif 'stop' == args[1] then +local options = +{ + start = + { + help = 'Command to start the scanning process, for Sys V init.', + value = false, + func = function(a, args, i) + end + }, + restart = {start}, + ['force-reload'] = {start}, + status = + { + help = 'Command to check the status of the scanning process, for Sys V init.', + value = false, + func = function(a, args, i) + end + }, + stop = + { + help = 'Command to stop the scanning process, for Sys V init.', + value = false, + func = function(a, args, i) + end + }, + JACK = + { + help = 'Command to start the JACK stuff, for users.', + value = false, + func = function(a, args, i) + end + }, + STOP = + { + help = 'Command to stop the JACK stuff, for users.', + value = false, + func = function(a, args, i) --[=[ _.killEmAll{'qsynth'} __[[ -a2j_control --stop -sleep 2 -a2j_control --exit -sleep 2 -]]:Do() + a2j_control --stop + sleep 2 + a2j_control --exit + sleep 2 + ]]:Do() _.killEmAll{'alsa_in', 'alsa_out', 'zita-a2j', 'zita-j2a', 'aseqjoy', 'jack-plumbing'} __[[ -sleep 2 -jack_control stop -sleep 2 -jack_control exit -sleep 2 -]]:Do() + sleep 2 + jack_control stop + sleep 2 + jack_control exit + sleep 2 + ]]:Do() _.killEmAll{'jmcore', 'qjackctl'} -- Catia is python, and no easy way to kill it. -- Also it keeps jackdbus alive, no matter how hard you kill it. @@ -43,26 +71,22 @@ sleep 2 __"sleep 2":Do() _.killEmAll{'jackdbus', 'a2jmidid'} ]=] - return(0) - elseif 'JACK' == args[1] then - elseif 'restart' == args[1] then args[1] = 'start' - elseif 'force-reload' == args[1] then args[1] = 'start' - elseif 'status' == args[1] then - return(0) - elseif 'help' == args[1] then - print(Help) - return(0) - elseif '--help' == args[1] then - print(Help) - return(0) - else - print("Usage: /etc/init.d/aataaj.lua {start|stop|restart|force-reload|status}") - return(1) - end -else - print("Usage: /etc/init.d/aataaj.lua {help|start|stop|restart|force-reload|status|JACK}") - return(1) -end + end + }, + help = + { + help = 'Print help.', + value = false, + func = function(a, args, i) + print(Help) + os.exit(0) + end + }, + ['--help'] = {help}, +} + +local args = {...} +_.parseArgs(options, args) @@ -87,7 +111,7 @@ for i,l in ipairs(__'df -h\nfree -h':log():Do().lines) do end print('') -__[[ +__[[#!/bin/bash df -h free -h ]]:log():show():Do() @@ -112,4 +136,4 @@ print(__'command NoSuchCommand':log():show():Do().status) D('debug?') -I('info?') \ No newline at end of file +I('info?') -- cgit v1.1