mcbot/cmds/help.lua
author Mikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 20:56:25 +0200
changeset 21 bc5d611e6d6f
parent 20 mcbot/cmds/misc.lua@d6c602aaa231
child 35 1fba2631c743
permissions -rw-r--r--
Improve command help
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
     1
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
     2
local help = { ["desc"] = "Display the available commands" }
21
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
     3
16
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
     4
function help.cmd (args)
21
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
     5
    local r = ""
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
     6
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
     7
    local function pairsByKeys (t, f)
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
     8
        local a = {}
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
     9
        for n in pairs(t) do table.insert(a, n) end
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    10
        table.sort(a, f)
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    11
        local i = 0      -- iterator variable
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    12
        local iter = function ()   -- iterator function
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    13
            i = i + 1
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    14
            if a[i] == nil then return nil
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    15
            else return a[i], t[a[i]]
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    16
            end
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    17
        end
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    18
        return iter
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    19
    end
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    20
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    21
    for name, obj in pairsByKeys(mcbot_get_command_list()) do
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    22
        if not obj.hidden then
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    23
            r = r .. string.format("%s\t%s\n", name, obj.desc or "")
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    24
        end
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    25
    end
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    26
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    27
    r = r:gsub("\n+$", "")
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 20
diff changeset
    28
    return r
16
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    29
end
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    30
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    31
mcbot_register_command("help", help)