Improve command help
authorMikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 20:56:25 +0200
changeset 21 bc5d611e6d6f
parent 20 d6c602aaa231
child 22 4b360fe207e6
Improve command help
mcbot/cmds/help.lua
mcbot/cmds/misc.lua
mcbot/mcbot_engine.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcbot/cmds/help.lua	Thu Apr 15 20:56:25 2010 +0200
@@ -0,0 +1,31 @@
+
+local help = { ["desc"] = "Display the available commands" }
+
+function help.cmd (args)
+    local r = ""
+
+    local function pairsByKeys (t, f)
+        local a = {}
+        for n in pairs(t) do table.insert(a, n) end
+        table.sort(a, f)
+        local i = 0      -- iterator variable
+        local iter = function ()   -- iterator function
+            i = i + 1
+            if a[i] == nil then return nil
+            else return a[i], t[a[i]]
+            end
+        end
+        return iter
+    end
+
+    for name, obj in pairsByKeys(mcbot_get_command_list()) do
+        if not obj.hidden then
+            r = r .. string.format("%s\t%s\n", name, obj.desc or "")
+        end
+    end
+
+    r = r:gsub("\n+$", "")
+    return r
+end
+
+mcbot_register_command("help", help)
--- a/mcbot/cmds/misc.lua	Thu Apr 15 20:28:34 2010 +0200
+++ b/mcbot/cmds/misc.lua	Thu Apr 15 20:56:25 2010 +0200
@@ -34,11 +34,3 @@
 end
 
 mcbot_register_command("date", date)
-
-
-local help = { ["desc"] = "Display the available commands" }
-function help.cmd (args)
-    return "I can't help you, buddy"
-end
-
-mcbot_register_command("help", help)
--- a/mcbot/mcbot_engine.lua	Thu Apr 15 20:28:34 2010 +0200
+++ b/mcbot/mcbot_engine.lua	Thu Apr 15 20:56:25 2010 +0200
@@ -21,6 +21,7 @@
     return commands
 end
 
+require "cmds.help"
 require "cmds.wtf"
 require "cmds.calc"
 require "cmds.spell"