Module:Arguments: Difference between revisions
(Created page with "-- This module provides easy processing of arguments passed to Scribunto from #invoke. -- It is intended for use by other Lua modules, and should not be called from #invoke di...") |
m>Mr. Stradivarius (fix undefined next() behaviour bug by checking for metatable.donePairs in the __index metamethod; also, format the module so it fits into 80 characters) |
||
| Line 1: | Line 1: | ||
-- This module provides easy processing of arguments passed to Scribunto from #invoke. | -- This module provides easy processing of arguments passed to Scribunto from | ||
-- #invoke. It is intended for use by other Lua modules, and should not be | |||
-- called from #invoke directly. | |||
local libraryUtil = require('libraryUtil') | local libraryUtil = require('libraryUtil') | ||
local checkType = libraryUtil.checkType | local checkType = libraryUtil.checkType | ||
local arguments = {} | local arguments = {} | ||
local nilArg = {} -- Used for memoizing nil arguments in metaArgs. | local nilArg = {} -- Used for memoizing nil arguments in metaArgs. | ||
-- Generate four different tidyVal functions, so that we don't have to check the options every time we call it. | -- Generate four different tidyVal functions, so that we don't have to check the | ||
-- options every time we call it. | |||
local function tidyValDefault(key, val) | local function tidyValDefault(key, val) | ||
if type(val) == 'string' then | if type(val) == 'string' then | ||
| Line 23: | Line 25: | ||
end | end | ||
end | end | ||
local function tidyValTrimOnly(key, val) | local function tidyValTrimOnly(key, val) | ||
if type(val) == 'string' then | if type(val) == 'string' then | ||
| Line 31: | Line 33: | ||
end | end | ||
end | end | ||
local function tidyValRemoveBlanksOnly(key, val) | local function tidyValRemoveBlanksOnly(key, val) | ||
if type(val) == 'string' then | if type(val) == 'string' then | ||
| Line 43: | Line 45: | ||
end | end | ||
end | end | ||
local function tidyValNoChange(key, val) | local function tidyValNoChange(key, val) | ||
return val | return val | ||
end | end | ||
function arguments.getArgs(frame, options) | function arguments.getArgs(frame, options) | ||
checkType('getArgs', 1, frame, 'table', true) | checkType('getArgs', 1, frame, 'table', true) | ||
| Line 53: | Line 55: | ||
frame = frame or {} | frame = frame or {} | ||
options = options or {} | options = options or {} | ||
-- Get the arguments from the frame object if available. If the frame object is not available, we are being called | --[[ | ||
-- Get the arguments from the frame object if available. If the frame object | |||
-- is not available, we are being called from another Lua module or from the | |||
-- debug console, so assign the args to a new variable so we can | |||
-- differentiate them. | |||
--]] | |||
local fargs, pargs, luaArgs | local fargs, pargs, luaArgs | ||
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then | if type(frame.args) == 'table' and type(frame.getParent) == 'function' then | ||
| Line 70: | Line 76: | ||
luaArgs = frame | luaArgs = frame | ||
end | end | ||
-- Set up the args and metaArgs tables. args will be the one accessed from functions, and metaArgs will hold the actual arguments. | -- Set up the args and metaArgs tables. args will be the one accessed from | ||
-- | -- functions, and metaArgs will hold the actual arguments. The metatable | ||
-- connects the two together. | |||
local args, metaArgs, metatable = {}, {}, {} | local args, metaArgs, metatable = {}, {}, {} | ||
setmetatable(args, metatable) | setmetatable(args, metatable) | ||
-- Generate the tidyVal function. If it has been specified by the user, we use that; if not, we choose one of four functions | --[[ | ||
-- | -- Generate the tidyVal function. If it has been specified by the user, we | ||
-- use that; if not, we choose one of four functions depending on the | |||
-- options chosen. This is so that we don't have to call the options table | |||
-- every time the function is called. | |||
--]] | |||
local tidyVal = options.valueFunc | local tidyVal = options.valueFunc | ||
if tidyVal then | if tidyVal then | ||
if type(tidyVal) ~= 'function' then | if type(tidyVal) ~= 'function' then | ||
error("bad value assigned to option 'valueFunc' (function expected, got | error( | ||
"bad value assigned to option 'valueFunc'" | |||
.. '(function expected, got ' | |||
.. type(tidyVal) | |||
.. ')', | |||
2 | |||