@@ -6,6 +6,40 @@ local S = core.get_translator("__builtin")
66
77core .registered_chatcommands = {}
88
9+ -- Interpret the parameters of a command, separating options and arguments.
10+ -- Input: parameters of a command
11+ -- Returns: opts, args
12+ -- opts is a string of option letters, or false on error
13+ -- args is an array with the non-option arguments in order, or an error message
14+ -- Example: for this command line:
15+ -- /command a b -cd e f -g
16+ -- the function would receive:
17+ -- a b -cd e f -g
18+ -- and it would return:
19+ -- "cdg", {"a", "b", "e", "f"}
20+ -- Negative numbers are taken as arguments. Long options (--option) are
21+ -- currently rejected as reserved.
22+ local function getopts (param )
23+ local opts = " "
24+ local args = {}
25+ for match in param :gmatch (" %S+" ) do
26+ if match :byte (1 ) == 45 then -- 45 = '-'
27+ local second = match :byte (2 )
28+ if second == 45 then
29+ return false , S (" Flags beginning with -- are reserved" )
30+ elseif second and (second < 48 or second > 57 ) then -- 48 = '0', 57 = '9'
31+ opts = opts .. match :sub (2 )
32+ else
33+ -- numeric, add it to args
34+ args [# args + 1 ] = match
35+ end
36+ else
37+ args [# args + 1 ] = match
38+ end
39+ end
40+ return opts , args
41+ end
42+
943function core .register_chatcommand (cmd , def )
1044 def = def or {}
1145 def .params = def .params or " "
@@ -33,22 +67,30 @@ function core.override_chatcommand(name, redefinition)
3367 core .registered_chatcommands [name ] = chatcommand
3468end
3569
70+ local function format_help_line (cmd , def )
71+ local cmd_marker = INIT == " client" and " ." or " /"
72+ local msg = core .colorize (" #00ffff" , cmd_marker .. cmd )
73+ if def .params and def .params ~= " " then
74+ msg = msg .. " " .. def .params
75+ end
76+ if def .description and def .description ~= " " then
77+ msg = msg .. " : " .. def .description
78+ end
79+ return msg
80+ end
81+
3682local function do_help_cmd (name , param )
37- local function format_help_line (cmd , def )
38- local cmd_marker = " /"
39- if INIT == " client" then
40- cmd_marker = " ."
41- end
42- local msg = core .colorize (" #00ffff" , cmd_marker .. cmd )
43- if def .params and def .params ~= " " then
44- msg = msg .. " " .. def .params
45- end
46- if def .description and def .description ~= " " then
47- msg = msg .. " : " .. def .description
48- end
49- return msg
83+ local opts , args = getopts (param )
84+ if not opts then
85+ return false , args
86+ end
87+ if # args > 1 then
88+ return false , S (" Too many arguments, try using just /help <command>" )
5089 end
51- if param == " " then
90+ local use_gui = INIT ~= " client" and core .get_player_by_name (name )
91+ use_gui = use_gui and not opts :find (" t" )
92+
93+ if # args == 0 and not use_gui then
5294 local cmds = {}
5395 for cmd , def in pairs (core .registered_chatcommands ) do
5496 if INIT == " client" or core .check_player_privs (name , def .privs ) then
@@ -71,7 +113,10 @@ local function do_help_cmd(name, param)
71113 .. " everything." )
72114 end
73115 return true , msg
74- elseif param == " all" then
116+ elseif # args == 0 or (args [1 ] == " all" and use_gui ) then
117+ core .show_general_help_formspec (name )
118+ return true
119+ elseif args [1 ] == " all" then
75120 local cmds = {}
76121 for cmd , def in pairs (core .registered_chatcommands ) do
77122 if INIT == " client" or core .check_player_privs (name , def .privs ) then
@@ -86,15 +131,19 @@ local function do_help_cmd(name, param)
86131 msg = core .gettext (" Available commands:" )
87132 end
88133 return true , msg .. " \n " .. table.concat (cmds , " \n " )
89- elseif INIT == " game" and param == " privs" then
134+ elseif INIT == " game" and args [1 ] == " privs" then
135+ if use_gui then
136+ core .show_privs_help_formspec (name )
137+ return true
138+ end
90139 local privs = {}
91140 for priv , def in pairs (core .registered_privileges ) do
92141 privs [# privs + 1 ] = priv .. " : " .. def .description
93142 end
94143 table.sort (privs )
95144 return true , S (" Available privileges:" ).. " \n " .. table.concat (privs , " \n " )
96145 else
97- local cmd = param
146+ local cmd = args [ 1 ]
98147 local def = core .registered_chatcommands [cmd ]
99148 if not def then
100149 local msg
@@ -120,8 +169,8 @@ if INIT == "client" then
120169 })
121170else
122171 core .register_chatcommand (" help" , {
123- params = S (" [all | privs | <cmd>]" ),
124- description = S (" Get help for commands or list privileges" ),
172+ params = S (" [all | privs | <cmd>] [-t] " ),
173+ description = S (" Get help for commands or list privileges (-t: output in chat) " ),
125174 func = do_help_cmd ,
126175 })
127176end
0 commit comments