From f75d9d7ed80d1ccf65b716733703ad84f7297a81 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 4 Feb 2022 18:56:01 +0100 Subject: prosodyctl: Use argument parsing library to parse --help, -h, -? Reads nicer, but adds more code. Can always be reverted later I suppose. --- prosodyctl | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'prosodyctl') diff --git a/prosodyctl b/prosodyctl index cb809b99..61c47bed 100755 --- a/prosodyctl +++ b/prosodyctl @@ -58,6 +58,7 @@ local lfs = dependencies.softreq "lfs"; ----------------------- +local parse_args = require "util.argparse".parse; local human_io = require "util.human.io"; local show_message, show_warning = prosodyctl.show_message, prosodyctl.show_warning; @@ -73,8 +74,11 @@ local prosodyctl_timeout = (configmanager.get("*", "prosodyctl_timeout") or 5) * local commands = {}; local command = table.remove(arg, 1); +local only_help = { short_params = { h = "help"; ["?"] = "help" } } + function commands.install(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help or not arg[1] then show_usage([[install]], [[Installs a prosody/luarocks plugin]]); return 1; end @@ -95,7 +99,8 @@ function commands.install(arg) end function commands.remove(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help or not arg[1] then show_usage([[remove]], [[Removes a module installed in the working directory's plugins folder]]); return 1; end @@ -105,7 +110,8 @@ function commands.remove(arg) end function commands.list(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[list]], [[Shows installed rocks]]); return 1; end @@ -114,7 +120,8 @@ function commands.list(arg) end function commands.adduser(arg) - if not arg[1] or arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help or not arg[1] then show_usage([[adduser JID]], [[Create the specified user account in Prosody]]); return 1; end @@ -153,7 +160,8 @@ function commands.adduser(arg) end function commands.passwd(arg) - if not arg[1] or arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help or not arg[1] then show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]); return 1; end @@ -192,7 +200,8 @@ function commands.passwd(arg) end function commands.deluser(arg) - if not arg[1] or arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help or not arg[1] then show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]); return 1; end @@ -250,7 +259,8 @@ local function service_command_warning(service_command) end function commands.start(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[start]], [[Start Prosody]]); return 1; end @@ -314,7 +324,8 @@ function commands.start(arg) end function commands.status(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[status]], [[Reports the running status of Prosody]]); return 1; end @@ -348,7 +359,8 @@ function commands.status(arg) end function commands.stop(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[stop]], [[Stop a running Prosody server]]); return 1; end @@ -385,7 +397,8 @@ function commands.stop(arg) end function commands.restart(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[restart]], [[Restart a running Prosody server]]); return 1; end @@ -397,7 +410,8 @@ function commands.restart(arg) end function commands.about(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[about]], [[Show information about this Prosody installation]]); return 1; end @@ -518,7 +532,8 @@ function commands.about(arg) end function commands.reload(arg) - if arg[1] == "--help" then + local opts = parse_args(arg, only_help); + if opts.help then show_usage([[reload]], [[Reload Prosody's configuration and re-open log files]]); return 1; end -- cgit v1.2.3