Gaming
 

KhaosSlashCommand

From WoWWiki

This article is a part of the documentation of the Khaos function library

	KhaosSlashCommand = {
		id = "KhaosCommandDemo";
		commands = { "/khaoscomm", "/khcm" };
		parseTree = { KhaosParseTree };
		helpText = "Just a demo function.";
	}

	KhaosParseTree = {
		-- String keywords branch into other trees
		["keyword"] = {KhaosParseTree}; -- IMPORTANT NOTE: KEYWORDS MUST BE LOWERCASE

		-- the default keyword is a special case
		default = {KhaosParseTree}; -- Will get executed only if nothing else matches

		-- Contrastingly, numeric indexes signify performed changes
		-- Each numberic index refers to a key you wish to change in the set
		[1] = {
			-- If you did not explicitly set a key the Id of the option will work. 
			key = "SomeKey"; 
			
			-- This will use a keyword map to determine how to modify that key
			stringMap = {
				-- If a custom keyword comes next, it will use the values
				-- to determine how to change the key
				["on"] = { checked=true; slider=5; color=RED_FONT_COLOR; }
				["off"] = { checked=false; slider=1; color=NORMAL_FONT_COLOR; }
			};

			-- You can also directly modify that key using special codes
			valueMap = {
				checked=true;
				-- You can also do checked = "!" - this means toggle the checked value

				slider="%1d";

				-- The %1 means parse the first word after "/khaoscomm keyword"; 
				-- So /khaoscomm 123 would say take "123" and parse it

				-- %1  - keep it as a string
				-- %1d - convert it to a number
				-- %1b - convert it to a boolean (true/false)

				-- Theoretically, I could support %##, but I won't until there's
				-- a need for it. So please keep modifiers down to 9 or less.
			};
		}
		-- Finally, you can specify a custom action to be performed
		[2] = {
			callback = function(msg) 
				Sea.io.print("You typed /khaoscomm "..msg);
			end;
		};
	};
	
	The Khaos slash command lets you setup a simple parse tree which will read the 
	input and reconfigure keys within the current set based on the words entered.

	If the key of the parseTree is a word, it will branch into that tree:

	Example: 
		{
			commands = {"/demo"};
			parseTree = {
				foo = {
					[1] = {
						key = "DemoKey1";
						stringMap = {
							blue = { checked=true; }
							red = { checked=false; }
							custom = { checked=true; }
						}
					};
					[2] = {
						key = "DemoKey2";
						stringMap = {
							blue = { slider=1; }
							red = { slider=2; }
							default = { slider=0; }
							custom = { slider="%1d"; }
						}
					};
				};
				bar = {
					[1] = {
						key = "DemoKey1";
						valueMap = {
							checked = true;
						};
					};
					[2] = {
						key = "DemoKey2";
	 					valueMap = {
							slider = "%1d";
						};
					};
				};
				baz = {
					callback = function(msg) 
						Sea.io.print(msg);
					end;
				}
			};
		}
		
	Result:
		/kt foo blue
		
		Would set DemoKey1 to checked and DemoKey2's slider to 1. 
		
		/kt foo custom 5
		
		Would set DemoKey1 to checked and DemoKey2's slider to 5. 

		/kt bar 1

		Would do the same thing. DemoKey1 would always be checked with bar, 
		but the %1d would convert it to a number. 

		/kt baz blue 43 hut!

		Would print out "blue 43 hut!".