Luaposix getopt for positional arguments

This commit is contained in:
gSpot
2025-02-06 18:55:57 +03:00
parent c20703d5be
commit 90b711f55e
8 changed files with 82 additions and 64 deletions

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector
PKG_VERSION:=1.3.2
PKG_VERSION:=1.3.3
PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -10,7 +10,7 @@ run_instance() {
config_get enabled "$1" enabled "0"
if [ $enabled = "1" ]; then
procd_open_instance "$1"
procd_set_param command "$PROG" "nodaemon" "$1"
procd_set_param command "$PROG" "-a" "nodaemon" "-i" "$1"
procd_close_instance
fi
}
@@ -24,7 +24,7 @@ start_service() {
}
stop_service() {
$PROG stop
$PROG -a stop
}
reload_service() {

View File

@@ -11,68 +11,88 @@
(с) 2024 gSpot (https://github.com/gSpotx2f/luci-app-internet-detector)
--]]
local getopt = require("posix.unistd").getopt
local InternetDetector = require("internet-detector.main")
local function help()
return string.format(
"Usage: %s daemon <UCI instance> | nodaemon <UCI instance> | debug <UCI instance> | stop | status | inet-status | uipoll | --help",
arg[0]
)
return table.concat({
[1] = string.format(
"Usage: %s -a daemon -i <UCI instance> | -a nodaemon -i <UCI instance> | -a debug -i <UCI instance> | -a stop | -S | -I | -U | -h",
arg[0]),
[2] = " -a ARG action: daemon | nodaemon | debug | stop",
[3] = " -i ARG instance: UCI instance name",
[4] = " -S status",
[5] = " -I inet status",
[6] = " -U uipoll",
[7] = " -h print this help text"
}, "\n")
end
local helpArgs = { ["-h"] = true, ["--help"] = true, help = true }
if arg[1] == "daemon" then
if arg[2] then
if InternetDetector:setServiceConfig(arg[2]) then
local action, instance
local params = {}
local last_index = 1
for r, optarg, optind in getopt(arg, "a:i:SIUh") do
if r == "?" then
print("Error! Unrecognized option")
os.exit(1)
end
last_index = optind
if r == "a" then
action = optarg
elseif r == "i" then
instance = optarg
else
params[#params + 1] = r
end
end
if action == "stop" then
InternetDetector:stop()
elseif action then
if not instance then
print("Error! Instance not specified [-i]")
os.exit(1)
end
if action == "daemon" then
if InternetDetector:setServiceConfig(instance) then
InternetDetector:daemon()
else
os.exit(126)
end
else
print(help())
os.exit(1)
end
elseif arg[1] == "nodaemon" then
if arg[2] then
if InternetDetector:setServiceConfig(arg[2]) then
elseif action == "nodaemon" then
if InternetDetector:setServiceConfig(instance) then
InternetDetector:noDaemon()
else
os.exit(126)
end
else
print(help())
os.exit(1)
end
elseif arg[1] == "debug" then
if arg[2] then
if InternetDetector:setServiceConfig(arg[2]) then
elseif action == "debug" then
if InternetDetector:setServiceConfig(instance) then
InternetDetector.debug = true
InternetDetector:noDaemon()
else
os.exit(126)
end
else
print("Error! Wrong action [-a]")
os.exit(1)
end
else
if params[1] == "S" then
print(InternetDetector:status())
elseif params[1] == "I" then
print(InternetDetector:inetStatus())
elseif params[1] == "U" then
if InternetDetector:status() == "stoped" then
os.exit(126)
else
InternetDetector:setSIGUSR()
print(InternetDetector:inetStatus())
end
elseif params[1] == "h" then
print(help())
else
print(help())
os.exit(1)
end
elseif arg[1] == "stop" then
InternetDetector:stop()
elseif arg[1] == "status" then
print(InternetDetector:status())
elseif arg[1] == "inet-status" then
print(InternetDetector:inetStatus())
elseif arg[1] == "uipoll" then
if InternetDetector:status() == "stoped" then
os.exit(126)
else
InternetDetector:setSIGUSR()
print(InternetDetector:inetStatus())
end
elseif helpArgs[arg[1]] then
print(help())
else
print(help())
os.exit(1)
end
os.exit(0)