Changeset

12264:ba712f7559dc

prosodyctl: Use argument parsing library to parse --help, -h, -? Reads nicer, but adds more code. Can always be reverted later I suppose.
author Kim Alvefur <zash@zash.se>
date Fri, 04 Feb 2022 18:56:01 +0100 (2022-02-04)
parents 12263:168970ce8543
children 12265:47d5233a1fc7
files prosodyctl
diffstat 1 files changed, 27 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/prosodyctl	Fri Feb 04 17:59:42 2022 +0100
+++ b/prosodyctl	Fri Feb 04 18:56:01 2022 +0100
@@ -58,6 +58,7 @@
 
 -----------------------
 
+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 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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 @@
 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