tercio@0: --- AceConfigCmd-3.0 handles access to an options table through the "command line" interface via the ChatFrames. tercio@0: -- @class file tercio@0: -- @name AceConfigCmd-3.0 Tercio@17: -- @release $Id: AceConfigCmd-3.0.lua 1161 2017-08-12 14:30:16Z funkydude $ tercio@0: tercio@0: --[[ tercio@0: AceConfigCmd-3.0 tercio@0: tercio@0: Handles commandline optionstable access tercio@0: tercio@0: REQUIRES: AceConsole-3.0 for command registration (loaded on demand) tercio@0: tercio@0: ]] tercio@0: tercio@0: -- TODO: plugin args tercio@0: Tercio@17: local cfgreg = LibStub("AceConfigRegistry-3.0") tercio@0: Tercio@17: local MAJOR, MINOR = "AceConfigCmd-3.0", 14 tercio@0: local AceConfigCmd = LibStub:NewLibrary(MAJOR, MINOR) tercio@0: tercio@0: if not AceConfigCmd then return end tercio@0: tercio@0: AceConfigCmd.commands = AceConfigCmd.commands or {} tercio@0: local commands = AceConfigCmd.commands tercio@0: tercio@0: local AceConsole -- LoD tercio@0: local AceConsoleName = "AceConsole-3.0" tercio@0: tercio@0: -- Lua APIs tercio@0: local strsub, strsplit, strlower, strmatch, strtrim = string.sub, string.split, string.lower, string.match, string.trim tercio@0: local format, tonumber, tostring = string.format, tonumber, tostring tercio@0: local tsort, tinsert = table.sort, table.insert tercio@0: local select, pairs, next, type = select, pairs, next, type tercio@0: local error, assert = error, assert tercio@0: tercio@0: -- WoW APIs tercio@0: local _G = _G tercio@0: tercio@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded tercio@0: -- List them here for Mikk's FindGlobals script tercio@0: -- GLOBALS: LibStub, SELECTED_CHAT_FRAME, DEFAULT_CHAT_FRAME tercio@0: tercio@0: tercio@0: local L = setmetatable({}, { -- TODO: replace with proper locale tercio@0: __index = function(self,k) return k end tercio@0: }) tercio@0: tercio@0: tercio@0: tercio@0: local function print(msg) tercio@0: (SELECTED_CHAT_FRAME or DEFAULT_CHAT_FRAME):AddMessage(msg) tercio@0: end tercio@0: tercio@0: -- constants used by getparam() calls below tercio@0: tercio@0: local handlertypes = {["table"]=true} tercio@0: local handlermsg = "expected a table" tercio@0: tercio@0: local functypes = {["function"]=true, ["string"]=true} tercio@0: local funcmsg = "expected function or member name" tercio@0: tercio@0: tercio@0: -- pickfirstset() - picks the first non-nil value and returns it tercio@0: tercio@0: local function pickfirstset(...) tercio@0: for i=1,select("#",...) do tercio@0: if select(i,...)~=nil then tercio@0: return select(i,...) tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: tercio@0: -- err() - produce real error() regarding malformed options tables etc tercio@0: tercio@0: local function err(info,inputpos,msg ) tercio@0: local cmdstr=" "..strsub(info.input, 1, inputpos-1) tercio@0: error(MAJOR..": /" ..info[0] ..cmdstr ..": "..(msg or "malformed options table"), 2) tercio@0: end tercio@0: tercio@0: tercio@0: -- usererr() - produce chatframe message regarding bad slash syntax etc tercio@0: tercio@0: local function usererr(info,inputpos,msg ) tercio@0: local cmdstr=strsub(info.input, 1, inputpos-1); tercio@0: print("/" ..info[0] .. " "..cmdstr ..": "..(msg or "malformed options table")) tercio@0: end tercio@0: tercio@0: tercio@0: -- callmethod() - call a given named method (e.g. "get", "set") with given arguments tercio@0: tercio@0: local function callmethod(info, inputpos, tab, methodtype, ...) tercio@0: local method = info[methodtype] tercio@0: if not method then tercio@0: err(info, inputpos, "'"..methodtype.."': not set") tercio@0: end tercio@0: tercio@0: info.arg = tab.arg tercio@0: info.option = tab tercio@0: info.type = tab.type tercio@0: tercio@0: if type(method)=="function" then tercio@0: return method(info, ...) tercio@0: elseif type(method)=="string" then tercio@0: if type(info.handler[method])~="function" then tercio@0: err(info, inputpos, "'"..methodtype.."': '"..method.."' is not a member function of "..tostring(info.handler)) tercio@0: end tercio@0: return info.handler[method](info.handler, info, ...) tercio@0: else tercio@0: assert(false) -- type should have already been checked on read tercio@0: end tercio@0: end tercio@0: tercio@0: -- callfunction() - call a given named function (e.g. "name", "desc") with given arguments tercio@0: tercio@0: local function callfunction(info, tab, methodtype, ...) tercio@0: local method = tab[methodtype] tercio@0: tercio@0: info.arg = tab.arg tercio@0: info.option = tab tercio@0: info.type = tab.type tercio@0: tercio@0: if type(method)=="function" then tercio@0: return method(info, ...) tercio@0: else tercio@0: assert(false) -- type should have already been checked on read tercio@0: end tercio@0: end tercio@0: tercio@0: -- do_final() - do the final step (set/execute) along with validation and confirmation tercio@0: tercio@0: local function do_final(info, inputpos, tab, methodtype, ...) tercio@0: if info.validate then tercio@0: local res = callmethod(info,inputpos,tab,"validate",...) tercio@0: if type(res)=="string" then tercio@0: usererr(info, inputpos, "'"..strsub(info.input, inputpos).."' - "..res) tercio@0: return tercio@0: end tercio@0: end tercio@0: -- console ignores .confirm tercio@0: tercio@0: callmethod(info,inputpos,tab,methodtype, ...) tercio@0: end tercio@0: tercio@0: tercio@0: -- getparam() - used by handle() to retreive and store "handler", "get", "set", etc tercio@0: tercio@0: local function getparam(info, inputpos, tab, depth, paramname, types, errormsg) tercio@0: local old,oldat = info[paramname], info[paramname.."_at"] tercio@0: local val=tab[paramname] tercio@0: if val~=nil then tercio@0: if val==false then tercio@0: val=nil tercio@0: elseif not types[type(val)] then tercio@0: err(info, inputpos, "'" .. paramname.. "' - "..errormsg) tercio@0: end tercio@0: info[paramname] = val tercio@0: info[paramname.."_at"] = depth tercio@0: end tercio@0: return old,oldat tercio@0: end tercio@0: tercio@0: tercio@0: -- iterateargs(tab) - custom iterator that iterates both t.args and t.plugins.* tercio@0: local dummytable={} tercio@0: tercio@0: local function iterateargs(tab) tercio@0: if not tab.plugins then tercio@0: return pairs(tab.args) tercio@0: end tercio@0: tercio@0: local argtabkey,argtab=next(tab.plugins) tercio@0: local v tercio@0: tercio@0: return function(_, k) tercio@0: while argtab do tercio@0: k,v = next(argtab, k) tercio@0: if k then return k,v end tercio@0: if argtab==tab.args then tercio@0: argtab=nil tercio@0: else tercio@0: argtabkey,argtab = next(tab.plugins, argtabkey) tercio@0: if not argtabkey then tercio@0: argtab=tab.args tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: local function checkhidden(info, inputpos, tab) tercio@0: if tab.cmdHidden~=nil then tercio@0: return tab.cmdHidden tercio@0: end tercio@0: local hidden = tab.hidden tercio@0: if type(hidden) == "function" or type(hidden) == "string" then tercio@0: info.hidden = hidden tercio@0: hidden = callmethod(info, inputpos, tab, 'hidden') tercio@0: info.hidden = nil tercio@0: end tercio@0: return hidden tercio@0: end tercio@0: tercio@0: local function showhelp(info, inputpos, tab, depth, noHead) tercio@0: if not noHead then tercio@0: print("|cff33ff99"..info.appName.."|r: Arguments to |cffffff78/"..info[0].."|r "..strsub(info.input,1,inputpos-1)..":") tercio@0: end tercio@0: tercio@0: local sortTbl = {} -- [1..n]=name tercio@0: local refTbl = {} -- [name]=tableref tercio@0: tercio@0: for k,v in iterateargs(tab) do tercio@0: if not refTbl[k] then -- a plugin overriding something in .args tercio@0: tinsert(sortTbl, k) tercio@0: refTbl[k] = v tercio@0: end tercio@0: end tercio@0: tercio@0: tsort(sortTbl, function(one, two) tercio@0: local o1 = refTbl[one].order or 100 tercio@0: local o2 = refTbl[two].order or 100 tercio@0: if type(o1) == "function" or type(o1) == "string" then tercio@0: info.order = o1 tercio@0: info[#info+1] = one tercio@0: o1 = callmethod(info, inputpos, refTbl[one], "order") tercio@0: info[#info] = nil tercio@0: info.order = nil tercio@0: end tercio@0: if type(o2) == "function" or type(o1) == "string" then tercio@0: info.order = o2 tercio@0: info[#info+1] = two tercio@0: o2 = callmethod(info, inputpos, refTbl[two], "order") tercio@0: info[#info] = nil tercio@0: info.order = nil tercio@0: end tercio@0: if o1<0 and o2<0 then return o1 4) and not _G["KEY_" .. text] then tercio@0: return false tercio@0: end tercio@0: local s = text tercio@0: if shift then tercio@0: s = "SHIFT-" .. s tercio@0: end tercio@0: if ctrl then tercio@0: s = "CTRL-" .. s tercio@0: end tercio@0: if alt then tercio@0: s = "ALT-" .. s tercio@0: end tercio@0: return s tercio@0: end tercio@0: tercio@0: -- handle() - selfrecursing function that processes input->optiontable tercio@0: -- - depth - starts at 0 tercio@0: -- - retfalse - return false rather than produce error if a match is not found (used by inlined groups) tercio@0: tercio@0: local function handle(info, inputpos, tab, depth, retfalse) tercio@0: tercio@0: if not(type(tab)=="table" and type(tab.type)=="string") then err(info,inputpos) end tercio@0: tercio@0: ------------------------------------------------------------------- tercio@0: -- Grab hold of handler,set,get,func,etc if set (and remember old ones) tercio@0: -- Note that we do NOT validate if method names are correct at this stage, tercio@0: -- the handler may change before they're actually used! tercio@0: tercio@0: local oldhandler,oldhandler_at = getparam(info,inputpos,tab,depth,"handler",handlertypes,handlermsg) tercio@0: local oldset,oldset_at = getparam(info,inputpos,tab,depth,"set",functypes,funcmsg) tercio@0: local oldget,oldget_at = getparam(info,inputpos,tab,depth,"get",functypes,funcmsg) tercio@0: local oldfunc,oldfunc_at = getparam(info,inputpos,tab,depth,"func",functypes,funcmsg) tercio@0: local oldvalidate,oldvalidate_at = getparam(info,inputpos,tab,depth,"validate",functypes,funcmsg) tercio@0: --local oldconfirm,oldconfirm_at = getparam(info,inputpos,tab,depth,"confirm",functypes,funcmsg) tercio@0: tercio@0: ------------------------------------------------------------------- tercio@0: -- Act according to .type of this table tercio@0: tercio@0: if tab.type=="group" then tercio@0: ------------ group -------------------------------------------- tercio@0: tercio@0: if type(tab.args)~="table" then err(info, inputpos) end tercio@0: if tab.plugins and type(tab.plugins)~="table" then err(info,inputpos) end tercio@0: tercio@0: -- grab next arg from input tercio@0: local _,nextpos,arg = (info.input):find(" *([^ ]+) *", inputpos) tercio@0: if not arg then tercio@0: showhelp(info, inputpos, tab, depth) tercio@0: return tercio@0: end tercio@0: nextpos=nextpos+1 tercio@0: tercio@0: -- loop .args and try to find a key with a matching name tercio@0: for k,v in iterateargs(tab) do tercio@0: if not(type(k)=="string" and type(v)=="table" and type(v.type)=="string") then err(info,inputpos, "options table child '"..tostring(k).."' is malformed") end tercio@0: tercio@0: -- is this child an inline group? if so, traverse into it tercio@0: if v.type=="group" and pickfirstset(v.cmdInline, v.inline, false) then tercio@0: info[depth+1] = k tercio@0: if handle(info, inputpos, v, depth+1, true)==false then tercio@0: info[depth+1] = nil tercio@0: -- wasn't found in there, but that's ok, we just keep looking down here tercio@0: else tercio@0: return -- done, name was found in inline group tercio@0: end tercio@0: -- matching name and not a inline group tercio@0: elseif strlower(arg)==strlower(k:gsub(" ", "_")) then tercio@0: info[depth+1] = k tercio@0: return handle(info,nextpos,v,depth+1) tercio@0: end tercio@0: end tercio@0: tercio@0: -- no match tercio@0: if retfalse then tercio@0: -- restore old infotable members and return false to indicate failure tercio@0: info.handler,info.handler_at = oldhandler,oldhandler_at tercio@0: info.set,info.set_at = oldset,oldset_at tercio@0: info.get,info.get_at = oldget,oldget_at tercio@0: info.func,info.func_at = oldfunc,oldfunc_at tercio@0: info.validate,info.validate_at = oldvalidate,oldvalidate_at tercio@0: --info.confirm,info.confirm_at = oldconfirm,oldconfirm_at tercio@0: return false tercio@0: end tercio@0: tercio@0: -- couldn't find the command, display error tercio@0: usererr(info, inputpos, "'"..arg.."' - " .. L["unknown argument"]) tercio@0: return tercio@0: end tercio@0: tercio@0: local str = strsub(info.input,inputpos); tercio@0: tercio@0: if tab.type=="execute" then tercio@0: ------------ execute -------------------------------------------- tercio@0: do_final(info, inputpos, tab, "func") tercio@0: tercio@0: tercio@0: tercio@0: elseif tab.type=="input" then tercio@0: ------------ input -------------------------------------------- tercio@0: tercio@0: local res = true tercio@0: if tab.pattern then tercio@0: if not(type(tab.pattern)=="string") then err(info, inputpos, "'pattern' - expected a string") end tercio@0: if not strmatch(str, tab.pattern) then tercio@0: usererr(info, inputpos, "'"..str.."' - " .. L["invalid input"]) tercio@0: return tercio@0: end tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", str) tercio@0: tercio@0: tercio@0: tercio@0: elseif tab.type=="toggle" then tercio@0: ------------ toggle -------------------------------------------- tercio@0: local b tercio@0: local str = strtrim(strlower(str)) tercio@0: if str=="" then tercio@0: b = callmethod(info, inputpos, tab, "get") tercio@0: tercio@0: if tab.tristate then tercio@0: --cycle in true, nil, false order tercio@0: if b then tercio@0: b = nil tercio@0: elseif b == nil then tercio@0: b = false tercio@0: else tercio@0: b = true tercio@0: end tercio@0: else tercio@0: b = not b tercio@0: end tercio@0: tercio@0: elseif str==L["on"] then tercio@0: b = true tercio@0: elseif str==L["off"] then tercio@0: b = false tercio@0: elseif tab.tristate and str==L["default"] then tercio@0: b = nil tercio@0: else tercio@0: if tab.tristate then tercio@0: usererr(info, inputpos, format(L["'%s' - expected 'on', 'off' or 'default', or no argument to toggle."], str)) tercio@0: else tercio@0: usererr(info, inputpos, format(L["'%s' - expected 'on' or 'off', or no argument to toggle."], str)) tercio@0: end tercio@0: return tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", b) tercio@0: tercio@0: tercio@0: elseif tab.type=="range" then tercio@0: ------------ range -------------------------------------------- tercio@0: local val = tonumber(str) tercio@0: if not val then tercio@0: usererr(info, inputpos, "'"..str.."' - "..L["expected number"]) tercio@0: return tercio@0: end tercio@0: if type(info.step)=="number" then tercio@0: val = val- (val % info.step) tercio@0: end tercio@0: if type(info.min)=="number" and valinfo.max then tercio@0: usererr(info, inputpos, val.." - "..format(L["must be equal to or lower than %s"], tostring(info.max)) ) tercio@0: return tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", val) tercio@0: tercio@0: tercio@0: elseif tab.type=="select" then tercio@0: ------------ select ------------------------------------ tercio@0: local str = strtrim(strlower(str)) tercio@0: tercio@0: local values = tab.values tercio@0: if type(values) == "function" or type(values) == "string" then tercio@0: info.values = values tercio@0: values = callmethod(info, inputpos, tab, "values") tercio@0: info.values = nil tercio@0: end tercio@0: tercio@0: if str == "" then tercio@0: local b = callmethod(info, inputpos, tab, "get") tercio@0: local fmt = "|cffffff78- [%s]|r %s" tercio@0: local fmt_sel = "|cffffff78- [%s]|r %s |cffff0000*|r" tercio@0: print(L["Options for |cffffff78"..info[#info].."|r:"]) tercio@0: for k, v in pairs(values) do tercio@0: if b == k then tercio@0: print(fmt_sel:format(k, v)) tercio@0: else tercio@0: print(fmt:format(k, v)) tercio@0: end tercio@0: end tercio@0: return tercio@0: end tercio@0: tercio@0: local ok tercio@0: for k,v in pairs(values) do tercio@0: if strlower(k)==str then tercio@0: str = k -- overwrite with key (in case of case mismatches) tercio@0: ok = true tercio@0: break tercio@0: end tercio@0: end tercio@0: if not ok then tercio@0: usererr(info, inputpos, "'"..str.."' - "..L["unknown selection"]) tercio@0: return tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", str) tercio@0: tercio@0: elseif tab.type=="multiselect" then tercio@0: ------------ multiselect ------------------------------------------- tercio@0: local str = strtrim(strlower(str)) tercio@0: tercio@0: local values = tab.values tercio@0: if type(values) == "function" or type(values) == "string" then tercio@0: info.values = values tercio@0: values = callmethod(info, inputpos, tab, "values") tercio@0: info.values = nil tercio@0: end tercio@0: tercio@0: if str == "" then tercio@0: local fmt = "|cffffff78- [%s]|r %s" tercio@0: local fmt_sel = "|cffffff78- [%s]|r %s |cffff0000*|r" tercio@0: print(L["Options for |cffffff78"..info[#info].."|r (multiple possible):"]) tercio@0: for k, v in pairs(values) do tercio@0: if callmethod(info, inputpos, tab, "get", k) then tercio@0: print(fmt_sel:format(k, v)) tercio@0: else tercio@0: print(fmt:format(k, v)) tercio@0: end tercio@0: end tercio@0: return tercio@0: end tercio@0: tercio@0: --build a table of the selections, checking that they exist tercio@0: --parse for =on =off =default in the process tercio@0: --table will be key = true for options that should toggle, key = [on|off|default] for options to be set tercio@0: local sels = {} tercio@0: for v in str:gmatch("[^ ]+") do tercio@0: --parse option=on etc tercio@0: local opt, val = v:match('(.+)=(.+)') tercio@0: --get option if toggling tercio@0: if not opt then tercio@0: opt = v tercio@0: end tercio@0: tercio@0: --check that the opt is valid tercio@0: local ok tercio@0: for k,v in pairs(values) do tercio@0: if strlower(k)==opt then tercio@0: opt = k -- overwrite with key (in case of case mismatches) tercio@0: ok = true tercio@0: break tercio@0: end tercio@0: end tercio@0: tercio@0: if not ok then tercio@0: usererr(info, inputpos, "'"..opt.."' - "..L["unknown selection"]) tercio@0: return tercio@0: end tercio@0: tercio@0: --check that if val was supplied it is valid tercio@0: if val then tercio@0: if val == L["on"] or val == L["off"] or (tab.tristate and val == L["default"]) then tercio@0: --val is valid insert it tercio@0: sels[opt] = val tercio@0: else tercio@0: if tab.tristate then tercio@0: usererr(info, inputpos, format(L["'%s' '%s' - expected 'on', 'off' or 'default', or no argument to toggle."], v, val)) tercio@0: else tercio@0: usererr(info, inputpos, format(L["'%s' '%s' - expected 'on' or 'off', or no argument to toggle."], v, val)) tercio@0: end tercio@0: return tercio@0: end tercio@0: else tercio@0: -- no val supplied, toggle tercio@0: sels[opt] = true tercio@0: end tercio@0: end tercio@0: tercio@0: for opt, val in pairs(sels) do tercio@0: local newval tercio@0: tercio@0: if (val == true) then tercio@0: --toggle the option tercio@0: local b = callmethod(info, inputpos, tab, "get", opt) tercio@0: tercio@0: if tab.tristate then tercio@0: --cycle in true, nil, false order tercio@0: if b then tercio@0: b = nil tercio@0: elseif b == nil then tercio@0: b = false tercio@0: else tercio@0: b = true tercio@0: end tercio@0: else tercio@0: b = not b tercio@0: end tercio@0: newval = b tercio@0: else tercio@0: --set the option as specified tercio@0: if val==L["on"] then tercio@0: newval = true tercio@0: elseif val==L["off"] then tercio@0: newval = false tercio@0: elseif val==L["default"] then tercio@0: newval = nil tercio@0: end tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", opt, newval) tercio@0: end tercio@0: tercio@0: tercio@0: elseif tab.type=="color" then tercio@0: ------------ color -------------------------------------------- tercio@0: local str = strtrim(strlower(str)) tercio@0: if str == "" then tercio@0: --TODO: Show current value tercio@0: return tercio@0: end tercio@0: tercio@0: local r, g, b, a tercio@0: tercio@0: local hasAlpha = tab.hasAlpha tercio@0: if type(hasAlpha) == "function" or type(hasAlpha) == "string" then tercio@0: info.hasAlpha = hasAlpha tercio@0: hasAlpha = callmethod(info, inputpos, tab, 'hasAlpha') tercio@0: info.hasAlpha = nil tercio@0: end tercio@0: tercio@0: if hasAlpha then tercio@0: if str:len() == 8 and str:find("^%x*$") then tercio@0: --parse a hex string tercio@0: r,g,b,a = tonumber(str:sub(1, 2), 16) / 255, tonumber(str:sub(3, 4), 16) / 255, tonumber(str:sub(5, 6), 16) / 255, tonumber(str:sub(7, 8), 16) / 255 tercio@0: else tercio@0: --parse seperate values tercio@0: r,g,b,a = str:match("^([%d%.]+) ([%d%.]+) ([%d%.]+) ([%d%.]+)$") tercio@0: r,g,b,a = tonumber(r), tonumber(g), tonumber(b), tonumber(a) tercio@0: end tercio@0: if not (r and g and b and a) then tercio@0: usererr(info, inputpos, format(L["'%s' - expected 'RRGGBBAA' or 'r g b a'."], str)) tercio@0: return tercio@0: end tercio@0: tercio@0: if r >= 0.0 and r <= 1.0 and g >= 0.0 and g <= 1.0 and b >= 0.0 and b <= 1.0 and a >= 0.0 and a <= 1.0 then tercio@0: --values are valid tercio@0: elseif r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255 and a >= 0 and a <= 255 then tercio@0: --values are valid 0..255, convert to 0..1 tercio@0: r = r / 255 tercio@0: g = g / 255 tercio@0: b = b / 255 tercio@0: a = a / 255 tercio@0: else tercio@0: --values are invalid tercio@0: usererr(info, inputpos, format(L["'%s' - values must all be either in the range 0..1 or 0..255."], str)) tercio@0: end tercio@0: else tercio@0: a = 1.0 tercio@0: if str:len() == 6 and str:find("^%x*$") then tercio@0: --parse a hex string tercio@0: r,g,b = tonumber(str:sub(1, 2), 16) / 255, tonumber(str:sub(3, 4), 16) / 255, tonumber(str:sub(5, 6), 16) / 255 tercio@0: else tercio@0: --parse seperate values tercio@0: r,g,b = str:match("^([%d%.]+) ([%d%.]+) ([%d%.]+)$") tercio@0: r,g,b = tonumber(r), tonumber(g), tonumber(b) tercio@0: end tercio@0: if not (r and g and b) then tercio@0: usererr(info, inputpos, format(L["'%s' - expected 'RRGGBB' or 'r g b'."], str)) tercio@0: return tercio@0: end tercio@0: if r >= 0.0 and r <= 1.0 and g >= 0.0 and g <= 1.0 and b >= 0.0 and b <= 1.0 then tercio@0: --values are valid tercio@0: elseif r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255 then tercio@0: --values are valid 0..255, convert to 0..1 tercio@0: r = r / 255 tercio@0: g = g / 255 tercio@0: b = b / 255 tercio@0: else tercio@0: --values are invalid tercio@0: usererr(info, inputpos, format(L["'%s' - values must all be either in the range 0-1 or 0-255."], str)) tercio@0: end tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", r,g,b,a) tercio@0: tercio@0: elseif tab.type=="keybinding" then tercio@0: ------------ keybinding -------------------------------------------- tercio@0: local str = strtrim(strlower(str)) tercio@0: if str == "" then tercio@0: --TODO: Show current value tercio@0: return tercio@0: end tercio@0: local value = keybindingValidateFunc(str:upper()) tercio@0: if value == false then tercio@0: usererr(info, inputpos, format(L["'%s' - Invalid Keybinding."], str)) tercio@0: return tercio@0: end tercio@0: tercio@0: do_final(info, inputpos, tab, "set", value) tercio@0: tercio@0: elseif tab.type=="description" then tercio@0: ------------ description -------------------- tercio@0: -- ignore description, GUI config only tercio@0: else tercio@0: err(info, inputpos, "unknown options table item type '"..tostring(tab.type).."'") tercio@0: end tercio@0: end tercio@0: tercio@0: --- Handle the chat command. tercio@0: -- This is usually called from a chat command handler to parse the command input as operations on an aceoptions table.\\ tercio@0: -- AceConfigCmd uses this function internally when a slash command is registered with `:CreateChatCommand` tercio@0: -- @param slashcmd The slash command WITHOUT leading slash (only used for error output) tercio@0: -- @param appName The application name as given to `:RegisterOptionsTable()` tercio@0: -- @param input The commandline input (as given by the WoW handler, i.e. without the command itself) tercio@0: -- @usage tercio@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceConsole-3.0") tercio@0: -- -- Use AceConsole-3.0 to register a Chat Command tercio@0: -- MyAddon:RegisterChatCommand("mychat", "ChatCommand") tercio@0: -- tercio@0: -- -- Show the GUI if no input is supplied, otherwise handle the chat input. tercio@0: -- function MyAddon:ChatCommand(input) tercio@0: -- -- Assuming "MyOptions" is the appName of a valid options table tercio@0: -- if not input or input:trim() == "" then tercio@0: -- LibStub("AceConfigDialog-3.0"):Open("MyOptions") tercio@0: -- else tercio@0: -- LibStub("AceConfigCmd-3.0").HandleCommand(MyAddon, "mychat", "MyOptions", input) tercio@0: -- end tercio@0: -- end tercio@0: function AceConfigCmd:HandleCommand(slashcmd, appName, input) tercio@0: tercio@0: local optgetter = cfgreg:GetOptionsTable(appName) tercio@0: if not optgetter then tercio@0: error([[Usage: HandleCommand("slashcmd", "appName", "input"): 'appName' - no options table "]]..tostring(appName)..[[" has been registered]], 2) tercio@0: end tercio@0: local options = assert( optgetter("cmd", MAJOR) ) tercio@0: tercio@0: local info = { -- Don't try to recycle this, it gets handed off to callbacks and whatnot tercio@0: [0] = slashcmd, tercio@0: appName = appName, tercio@0: options = options, tercio@0: input = input, tercio@0: self = self, tercio@0: handler = self, tercio@0: uiType = "cmd", tercio@0: uiName = MAJOR, tercio@0: } tercio@0: tercio@0: handle(info, 1, options, 0) -- (info, inputpos, table, depth) tercio@0: end tercio@0: tercio@0: --- Utility function to create a slash command handler. tercio@0: -- Also registers tab completion with AceTab tercio@0: -- @param slashcmd The slash command WITHOUT leading slash (only used for error output) tercio@0: -- @param appName The application name as given to `:RegisterOptionsTable()` tercio@0: function AceConfigCmd:CreateChatCommand(slashcmd, appName) tercio@0: if not AceConsole then tercio@0: AceConsole = LibStub(AceConsoleName) tercio@0: end tercio@0: if AceConsole.RegisterChatCommand(self, slashcmd, function(input) tercio@0: AceConfigCmd.HandleCommand(self, slashcmd, appName, input) -- upgradable tercio@0: end, tercio@0: true) then -- succesfully registered so lets get the command -> app table in tercio@0: commands[slashcmd] = appName tercio@0: end tercio@0: end tercio@0: tercio@0: --- Utility function that returns the options table that belongs to a slashcommand. tercio@0: -- Designed to be used for the AceTab interface. tercio@0: -- @param slashcmd The slash command WITHOUT leading slash (only used for error output) tercio@0: -- @return The options table associated with the slash command (or nil if the slash command was not registered) tercio@0: function AceConfigCmd:GetChatCommandOptions(slashcmd) tercio@0: return commands[slashcmd] tercio@0: end