diff options
author | Pedro Gimeno <pgimeno@users.noreply.notabug.org> | 2021-04-03 22:51:09 +0200 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2021-10-01 16:22:05 +0200 |
commit | d7e7ade0f61a0f5e05d889ffbc7d9a878148a461 (patch) | |
tree | 5822c6b56d0750b05eaeb6c435daaae807b565da /builtin | |
parent | 982e03f60dc95cb2605a4a1c6520b604f85dd1d0 (diff) | |
download | minetest-d7e7ade0f61a0f5e05d889ffbc7d9a878148a461.tar.gz minetest-d7e7ade0f61a0f5e05d889ffbc7d9a878148a461.tar.bz2 minetest-d7e7ade0f61a0f5e05d889ffbc7d9a878148a461.zip |
Add an option `-t` to force text output in /help
This also improves detection of whether formspecs are available.
Diffstat (limited to 'builtin')
-rw-r--r-- | builtin/common/chatcommands.lua | 87 | ||||
-rw-r--r-- | builtin/common/information_formspecs.lua | 32 |
2 files changed, 75 insertions, 44 deletions
diff --git a/builtin/common/chatcommands.lua b/builtin/common/chatcommands.lua index c945e7bdb..21417e42b 100644 --- a/builtin/common/chatcommands.lua +++ b/builtin/common/chatcommands.lua @@ -6,6 +6,40 @@ local S = core.get_translator("__builtin") core.registered_chatcommands = {} +-- Interpret the parameters of a command, separating options and arguments. +-- Input: parameters of a command +-- Returns: opts, args +-- opts is a string of option letters, or false on error +-- args is an array with the non-option arguments in order, or an error message +-- Example: for this command line: +-- /command a b -cd e f -g +-- the function would receive: +-- a b -cd e f -g +-- and it would return: +-- "cdg", {"a", "b", "e", "f"} +-- Negative numbers are taken as arguments. Long options (--option) are +-- currently rejected as reserved. +local function getopts(param) + local opts = "" + local args = {} + for match in param:gmatch("%S+") do + if match:byte(1) == 45 then -- 45 = '-' + local second = match:byte(2) + if second == 45 then + return false, S("Flags beginning with -- are reserved") + elseif second and (second < 48 or second > 57) then -- 48 = '0', 57 = '9' + opts = opts .. match:sub(2) + else + -- numeric, add it to args + args[#args + 1] = match + end + else + args[#args + 1] = match + end + end + return opts, args +end + function core.register_chatcommand(cmd, def) def = def or {} def.params = def.params or "" @@ -33,22 +67,30 @@ function core.override_chatcommand(name, redefinition) core.registered_chatcommands[name] = chatcommand end +local function format_help_line(cmd, def) + local cmd_marker = INIT == "client" and "." or "/" + local msg = core.colorize("#00ffff", cmd_marker .. cmd) + if def.params and def.params ~= "" then + msg = msg .. " " .. def.params + end + if def.description and def.description ~= "" then + msg = msg .. ": " .. def.description + end + return msg +end + local function do_help_cmd(name, param) - local function format_help_line(cmd, def) - local cmd_marker = "/" - if INIT == "client" then - cmd_marker = "." - end - local msg = core.colorize("#00ffff", cmd_marker .. cmd) - if def.params and def.params ~= "" then - msg = msg .. " " .. def.params - end - if def.description and def.description ~= "" then - msg = msg .. ": " .. def.description - end - return msg + local opts, args = getopts(param) + if not opts then + return false, args + end + if #args > 1 then + return false, S("Too many arguments, try using just /help <command>") end - if param == "" then + local use_gui = INIT ~= "client" and core.get_player_by_name(name) + use_gui = use_gui and not opts:find("t") + + if #args == 0 and not use_gui then local cmds = {} for cmd, def in pairs(core.registered_chatcommands) do if INIT == "client" or core.check_player_privs(name, def.privs) then @@ -71,7 +113,10 @@ local function do_help_cmd(name, param) .. "everything.") end return true, msg - elseif param == "all" then + elseif #args == 0 or (args[1] == "all" and use_gui) then + core.show_general_help_formspec(name) + return true + elseif args[1] == "all" then local cmds = {} for cmd, def in pairs(core.registered_chatcommands) do if INIT == "client" or core.check_player_privs(name, def.privs) then @@ -86,7 +131,11 @@ local function do_help_cmd(name, param) msg = core.gettext("Available commands:") end return true, msg.."\n"..table.concat(cmds, "\n") - elseif INIT == "game" and param == "privs" then + elseif INIT == "game" and args[1] == "privs" then + if use_gui then + core.show_privs_help_formspec(name) + return true + end local privs = {} for priv, def in pairs(core.registered_privileges) do privs[#privs + 1] = priv .. ": " .. def.description @@ -94,7 +143,7 @@ local function do_help_cmd(name, param) table.sort(privs) return true, S("Available privileges:").."\n"..table.concat(privs, "\n") else - local cmd = param + local cmd = args[1] local def = core.registered_chatcommands[cmd] if not def then local msg @@ -120,8 +169,8 @@ if INIT == "client" then }) else core.register_chatcommand("help", { - params = S("[all | privs | <cmd>]"), - description = S("Get help for commands or list privileges"), + params = S("[all | privs | <cmd>] [-t]"), + description = S("Get help for commands or list privileges (-t: output in chat)"), func = do_help_cmd, }) end diff --git a/builtin/common/information_formspecs.lua b/builtin/common/information_formspecs.lua index e814b4c43..3405263bf 100644 --- a/builtin/common/information_formspecs.lua +++ b/builtin/common/information_formspecs.lua @@ -125,30 +125,12 @@ core.register_on_player_receive_fields(function(player, formname, fields) end end) - -local help_command = core.registered_chatcommands["help"] -local old_help_func = help_command.func - -help_command.func = function(name, param) - local admin = core.settings:get("name") - - -- If the admin ran help, put the output in the chat buffer as well to - -- work with the server terminal - if param == "privs" then - core.show_formspec(name, "__builtin:help_privs", - build_privs_formspec(name)) - if name ~= admin then - return true - end - end - if param == "" or param == "all" then - core.show_formspec(name, "__builtin:help_cmds", - build_chatcommands_formspec(name)) - if name ~= admin then - return true - end - end - - return old_help_func(name, param) +function core.show_general_help_formspec(name) + core.show_formspec(name, "__builtin:help_cmds", + build_chatcommands_formspec(name)) end +function core.show_privs_help_formspec(name) + core.show_formspec(name, "__builtin:help_privs", + build_privs_formspec(name)) +end |