Software /
code /
prosody
Comparison
prosodyctl @ 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 |
parent | 12103:fc297128c33a |
child | 12265:47d5233a1fc7 |
comparison
equal
deleted
inserted
replaced
12263:168970ce8543 | 12264:ba712f7559dc |
---|---|
56 local dependencies = require "util.dependencies"; | 56 local dependencies = require "util.dependencies"; |
57 local lfs = dependencies.softreq "lfs"; | 57 local lfs = dependencies.softreq "lfs"; |
58 | 58 |
59 ----------------------- | 59 ----------------------- |
60 | 60 |
61 local parse_args = require "util.argparse".parse; | |
61 local human_io = require "util.human.io"; | 62 local human_io = require "util.human.io"; |
62 | 63 |
63 local show_message, show_warning = prosodyctl.show_message, prosodyctl.show_warning; | 64 local show_message, show_warning = prosodyctl.show_message, prosodyctl.show_warning; |
64 local show_usage = prosodyctl.show_usage; | 65 local show_usage = prosodyctl.show_usage; |
65 local read_password = human_io.read_password; | 66 local read_password = human_io.read_password; |
71 local prosodyctl_timeout = (configmanager.get("*", "prosodyctl_timeout") or 5) * 2; | 72 local prosodyctl_timeout = (configmanager.get("*", "prosodyctl_timeout") or 5) * 2; |
72 ----------------------- | 73 ----------------------- |
73 local commands = {}; | 74 local commands = {}; |
74 local command = table.remove(arg, 1); | 75 local command = table.remove(arg, 1); |
75 | 76 |
77 local only_help = { short_params = { h = "help"; ["?"] = "help" } } | |
78 | |
76 function commands.install(arg) | 79 function commands.install(arg) |
77 if arg[1] == "--help" then | 80 local opts = parse_args(arg, only_help); |
81 if opts.help or not arg[1] then | |
78 show_usage([[install]], [[Installs a prosody/luarocks plugin]]); | 82 show_usage([[install]], [[Installs a prosody/luarocks plugin]]); |
79 return 1; | 83 return 1; |
80 end | 84 end |
81 -- TODO finalize config option name | 85 -- TODO finalize config option name |
82 local server = configmanager.get("*", "plugin_server"); | 86 local server = configmanager.get("*", "plugin_server"); |
93 end | 97 end |
94 return ret; | 98 return ret; |
95 end | 99 end |
96 | 100 |
97 function commands.remove(arg) | 101 function commands.remove(arg) |
98 if arg[1] == "--help" then | 102 local opts = parse_args(arg, only_help); |
103 if opts.help or not arg[1] then | |
99 show_usage([[remove]], [[Removes a module installed in the working directory's plugins folder]]); | 104 show_usage([[remove]], [[Removes a module installed in the working directory's plugins folder]]); |
100 return 1; | 105 return 1; |
101 end | 106 end |
102 show_message("Removing %s from %s", arg[1], prosody.paths.installer); | 107 show_message("Removing %s from %s", arg[1], prosody.paths.installer); |
103 local ret = call_luarocks("remove", arg[1]); | 108 local ret = call_luarocks("remove", arg[1]); |
104 return ret; | 109 return ret; |
105 end | 110 end |
106 | 111 |
107 function commands.list(arg) | 112 function commands.list(arg) |
108 if arg[1] == "--help" then | 113 local opts = parse_args(arg, only_help); |
114 if opts.help then | |
109 show_usage([[list]], [[Shows installed rocks]]); | 115 show_usage([[list]], [[Shows installed rocks]]); |
110 return 1; | 116 return 1; |
111 end | 117 end |
112 local ret = call_luarocks("list", arg[1]); | 118 local ret = call_luarocks("list", arg[1]); |
113 return ret; | 119 return ret; |
114 end | 120 end |
115 | 121 |
116 function commands.adduser(arg) | 122 function commands.adduser(arg) |
117 if not arg[1] or arg[1] == "--help" then | 123 local opts = parse_args(arg, only_help); |
124 if opts.help or not arg[1] then | |
118 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]); | 125 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]); |
119 return 1; | 126 return 1; |
120 end | 127 end |
121 local user, host = jid_split(arg[1]); | 128 local user, host = jid_split(arg[1]); |
122 if not user and host then | 129 if not user and host then |
151 show_message(error_messages[msg]) | 158 show_message(error_messages[msg]) |
152 return 1; | 159 return 1; |
153 end | 160 end |
154 | 161 |
155 function commands.passwd(arg) | 162 function commands.passwd(arg) |
156 if not arg[1] or arg[1] == "--help" then | 163 local opts = parse_args(arg, only_help); |
164 if opts.help or not arg[1] then | |
157 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]); | 165 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]); |
158 return 1; | 166 return 1; |
159 end | 167 end |
160 local user, host = jid_split(arg[1]); | 168 local user, host = jid_split(arg[1]); |
161 if not user and host then | 169 if not user and host then |
190 show_message(error_messages[msg]) | 198 show_message(error_messages[msg]) |
191 return 1; | 199 return 1; |
192 end | 200 end |
193 | 201 |
194 function commands.deluser(arg) | 202 function commands.deluser(arg) |
195 if not arg[1] or arg[1] == "--help" then | 203 local opts = parse_args(arg, only_help); |
204 if opts.help or not arg[1] then | |
196 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]); | 205 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]); |
197 return 1; | 206 return 1; |
198 end | 207 end |
199 local user, host = jid_split(arg[1]); | 208 local user, host = jid_split(arg[1]); |
200 if not user and host then | 209 if not user and host then |
248 show_warning(""); | 257 show_warning(""); |
249 end | 258 end |
250 end | 259 end |
251 | 260 |
252 function commands.start(arg) | 261 function commands.start(arg) |
253 if arg[1] == "--help" then | 262 local opts = parse_args(arg, only_help); |
263 if opts.help then | |
254 show_usage([[start]], [[Start Prosody]]); | 264 show_usage([[start]], [[Start Prosody]]); |
255 return 1; | 265 return 1; |
256 end | 266 end |
257 service_command_warning("start"); | 267 service_command_warning("start"); |
258 local ok, ret = prosodyctl.isrunning(); | 268 local ok, ret = prosodyctl.isrunning(); |
312 show_message(error_messages[ret]) | 322 show_message(error_messages[ret]) |
313 return 1; | 323 return 1; |
314 end | 324 end |
315 | 325 |
316 function commands.status(arg) | 326 function commands.status(arg) |
317 if arg[1] == "--help" then | 327 local opts = parse_args(arg, only_help); |
328 if opts.help then | |
318 show_usage([[status]], [[Reports the running status of Prosody]]); | 329 show_usage([[status]], [[Reports the running status of Prosody]]); |
319 return 1; | 330 return 1; |
320 end | 331 end |
321 | 332 |
322 local ok, ret = prosodyctl.isrunning(); | 333 local ok, ret = prosodyctl.isrunning(); |
346 return 2 | 357 return 2 |
347 end | 358 end |
348 end | 359 end |
349 | 360 |
350 function commands.stop(arg) | 361 function commands.stop(arg) |
351 if arg[1] == "--help" then | 362 local opts = parse_args(arg, only_help); |
363 if opts.help then | |
352 show_usage([[stop]], [[Stop a running Prosody server]]); | 364 show_usage([[stop]], [[Stop a running Prosody server]]); |
353 return 1; | 365 return 1; |
354 end | 366 end |
355 | 367 |
356 service_command_warning("stop"); | 368 service_command_warning("stop"); |
383 show_message(error_messages[ret]); | 395 show_message(error_messages[ret]); |
384 return 1; | 396 return 1; |
385 end | 397 end |
386 | 398 |
387 function commands.restart(arg) | 399 function commands.restart(arg) |
388 if arg[1] == "--help" then | 400 local opts = parse_args(arg, only_help); |
401 if opts.help then | |
389 show_usage([[restart]], [[Restart a running Prosody server]]); | 402 show_usage([[restart]], [[Restart a running Prosody server]]); |
390 return 1; | 403 return 1; |
391 end | 404 end |
392 | 405 |
393 service_command_warning("restart"); | 406 service_command_warning("restart"); |
395 commands.stop(arg); | 408 commands.stop(arg); |
396 return commands.start(arg); | 409 return commands.start(arg); |
397 end | 410 end |
398 | 411 |
399 function commands.about(arg) | 412 function commands.about(arg) |
400 if arg[1] == "--help" then | 413 local opts = parse_args(arg, only_help); |
414 if opts.help then | |
401 show_usage([[about]], [[Show information about this Prosody installation]]); | 415 show_usage([[about]], [[Show information about this Prosody installation]]); |
402 return 1; | 416 return 1; |
403 end | 417 end |
404 | 418 |
405 local pwd = "."; | 419 local pwd = "."; |
516 end | 530 end |
517 print(""); | 531 print(""); |
518 end | 532 end |
519 | 533 |
520 function commands.reload(arg) | 534 function commands.reload(arg) |
521 if arg[1] == "--help" then | 535 local opts = parse_args(arg, only_help); |
536 if opts.help then | |
522 show_usage([[reload]], [[Reload Prosody's configuration and re-open log files]]); | 537 show_usage([[reload]], [[Reload Prosody's configuration and re-open log files]]); |
523 return 1; | 538 return 1; |
524 end | 539 end |
525 | 540 |
526 service_command_warning("reload"); | 541 service_command_warning("reload"); |