CCCPaste

chat.lua

local chat = {
	commands = {}
}




function chat.newCommand(commandName, commandFunction)
	chat.commands[commandName] = {
		name = commandName,
		commandFunction = commandFunction,
		subCommands = {}
	}
	
	local lastSpace
	local spacePos
	repeat
		lastSpace = spacePos
		spacePos = commandName:find("%s", (lastSpace or -1) + 1)
	until not spacePos
	
	if lastSpace then
		chat.commands[commandName].superCommand = chat.commands[commandName:sub(1, lastSpace - 1)]
		table.insert(chat.commands[commandName].superCommand.subCommands, chat.commands[commandName])
	end
end



local function printCommands(commands, position, dontPrintChilds)
	position = position or 0
	for k,command in pairs(commands) do
		if not (command.superCommand and dontPrintChilds) then
			print(string.rep(" ", position) .. command.name)
		end
		printCommands(command.subCommands, position + 2)
	end
end



local function help()
	printCommands(chat.commands, 0, true)
end


chat.newCommand("test")
chat.newCommand("test blub")
chat.newCommand("aha")
chat.newCommand("aha tada")
chat.newCommand("aha nein")
chat.newCommand("aha nein blub")

help()