mcbot/cmds/help.lua
changeset 21 bc5d611e6d6f
parent 20 d6c602aaa231
child 35 1fba2631c743
equal deleted inserted replaced
20:d6c602aaa231 21:bc5d611e6d6f
       
     1 
       
     2 local help = { ["desc"] = "Display the available commands" }
       
     3 
       
     4 function help.cmd (args)
       
     5     local r = ""
       
     6 
       
     7     local function pairsByKeys (t, f)
       
     8         local a = {}
       
     9         for n in pairs(t) do table.insert(a, n) end
       
    10         table.sort(a, f)
       
    11         local i = 0      -- iterator variable
       
    12         local iter = function ()   -- iterator function
       
    13             i = i + 1
       
    14             if a[i] == nil then return nil
       
    15             else return a[i], t[a[i]]
       
    16             end
       
    17         end
       
    18         return iter
       
    19     end
       
    20 
       
    21     for name, obj in pairsByKeys(mcbot_get_command_list()) do
       
    22         if not obj.hidden then
       
    23             r = r .. string.format("%s\t%s\n", name, obj.desc or "")
       
    24         end
       
    25     end
       
    26 
       
    27     r = r:gsub("\n+$", "")
       
    28     return r
       
    29 end
       
    30 
       
    31 mcbot_register_command("help", help)