Module:User:Rua/parameters

local export = {}

function export.process(args, params) -- Process parameters for specific properties local required = {} local patterns = {} for name, param in pairs(params) do		if param.required then required[name] = true end if param.pattern then patterns["^" .. mw.ustring.gsub(param.pattern, "%%", "(%d+)") .. "$"] = name end end -- Process the arguments local args_new = {} for name, val in pairs(args) do -- Does this argument name match a pattern? local index = nil for pattern, pname in pairs(patterns) do			index = mw.ustring.match(name, pattern) -- It matches, so store the parameter name and the -- numeric index extracted from the argument name. if index then index = tonumber(index) name = pname break end end local param = params[name] -- If the argument is not in the list of parameters, trigger an error. if not params[name] then error("The parameter \"" .. name .. "\" does not exist.") end -- Empty string is equivalent to nil unless allow_empty is true. if val == "" and not param.allow_empty then val = nil end -- Convert to proper type if necessary. if param.type == "boolean" then val = not (not val or val == "" or val == "0" or val == "no" or val == "n" or val == "false") elseif param.type == "number" then val = tonumber(val) end -- Can't use "if val" alone, because val may be a boolean false. if val ~= nil then -- Mark it as no longer required, as it is present. required[name] = nil -- Store the argument value. if param.pattern then -- If the parameter has a pattern, then it becomes a list. if args_new[name] == nil then args_new[name] = {} end args_new[name][index or 1] = val else -- If the parameter is an alias of another, store it as the original, -- but avoid overwriting it; the original takes precedence. if not param.alias_of then args_new[name] = val elseif args[param.alias_of] == nil then args_new[param.alias_of] = val end end end end -- The required table should now be empty. -- If any entry remains, trigger an error, unless we're in the template namespace. if mw.title.getCurrentTitle.nsText ~= "Template" then for name, param in pairs(required) do error("The parameter \"" .. name .. "\" is required.") end end return args_new end

return export