Software / code / prosody
Comparison
util/prosodyctl/shell.lua @ 13732:1465b1e305df 13.0
mod_admin_shell, util.prosodyctl.shell: Process command-line args on server-side, with argparse support
This allow a shell-command to provide a 'flags' field, which will automatically
cause the parameters to be fed through argparse.
The rationale is to make it easier for more complex commands to be invoked
from the command line (`prosodyctl shell foo bar ...`). Until now they were
limited to accepting a list of strings, and any complex argument processing
was non-standard and awkward to implement.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 17 Feb 2025 17:02:35 +0000 |
| parent | 13643:e5f62871e6a5 |
| child | 13770:a28349b8a387 |
comparison
equal
deleted
inserted
replaced
| 13731:d78e0f422464 | 13732:1465b1e305df |
|---|---|
| 85 os.exit(1); | 85 os.exit(1); |
| 86 end | 86 end |
| 87 | 87 |
| 88 if arg[1] then | 88 if arg[1] then |
| 89 if arg[2] then | 89 if arg[2] then |
| 90 local fmt = { "%s"; ":%s("; ")" }; | 90 arg[1] = ("{"..string.rep("%q", #arg, ", ").."}"):format(table.unpack(arg, 1, #arg)); |
| 91 for i = 3, #arg do | |
| 92 if arg[i]:sub(1, 1) == ":" then | |
| 93 table.insert(fmt, i, ")%s("); | |
| 94 elseif i > 3 and fmt[i - 1]:match("%%q$") then | |
| 95 table.insert(fmt, i, ", %q"); | |
| 96 else | |
| 97 table.insert(fmt, i, "%q"); | |
| 98 end | |
| 99 end | |
| 100 arg[1] = string.format(table.concat(fmt), table.unpack(arg)); | |
| 101 end | 91 end |
| 102 | 92 |
| 103 client.events.add_handler("connected", function() | 93 client.events.add_handler("connected", function() |
| 104 send_line(client, arg[1]); | 94 send_line(client, arg[1]); |
| 105 return true; | 95 return true; |