Module:Arguments: Difference between revisions

use an array to hold argument tables rather than firstArgs and secondArgs variables
(check fargs and pargs before writing new values to args if options.noOverwrite is set)
(use an array to hold argument tables rather than firstArgs and secondArgs variables)
Line 8:
 
-- 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 assumeput argumentsthe areargs passedin directlya inspecial table so we can differentiate them.
local fargs, pargs, luaArgs
if frame == mw.getCurrentFrame() then
fargs = frame.args
pargs = frame:getParent().args
else
fargsluaArgs = type(frame) == 'table' and frame or {}
pargs = {}
end
 
-- 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 = {}, {}, {}
setmetatable(args, metatable)
Line 36 ⟶ 37:
end
 
-- Use a user-generated functionsfunction to tidy the values if specified.
local valueFunc = options.valueFunc
if valueFunc then
Line 47 ⟶ 48:
end
 
local function mergeArgs(iterator, ...tables)
-- Accepts multiple tables as input and merges their keys and values into one table using the specified iterator.
-- If a value is already present it is not overwritten; tables listed earlier have precendence.
local tables = {...}
for _, t in ipairs(tables) do
for key, val in iterator(t) do
Line 60:
end
 
-- Set the order of precedence of framethe argsargument andtables. parentIf args.the variables are nil, nothing will be added to the table,
-- which is how we avoid clashes between the frame/parent args and the Lua args.
local firstArgs, secondArgs = fargs, pargs
local tablesargTables = {...}
if options.parentFirst then
table.insert(argTables, pargs)
firstArgs, secondArgs = pargs, fargs
table.insert(argTables, fargs)
else
table.insert(argTables, fargs)
table.insert(argTables, pargs)
end
table.insert(argTables, luaArgs)
 
--[[
-- Define metatable behaviour. Arguments are stored in the metaArgs table, and are only fetched from the
-- fargs and pargsargument tables once. Also, we keep a record in the metatable of when pairs and ipairs have been called,
-- been called, so we do not run pairs and ipairs on fargs and pargs more than once. We also do not run ipairs on fargs
-- ipairs on fargs and pargs if pairs has already been run, as all the arguments will already have been copied over.
-- copied over.
--]]
metatable.__index = function (t, key)
Line 78 ⟶ 83:
return val
else
for i, argTable in ipairs(argTables) do
local firstValargTableVal = tidyVal(key, firstArgsargTable[key])
if firstValargTableVal ~= nil then
return firstVal
metaArgs[key] = argTableVal
else
return tidyVal(key, secondArgs[key])argTableVal
end
end
end
Line 95 ⟶ 101:
metatable.__pairs = function ()
if not metatable.donePairs then
mergeArgs(pairs, firstArgs, secondArgsargTables)
metatable.donePairs = true
metatable.doneIpairs = true
Line 104 ⟶ 110:
metatable.__ipairs = function ()
if not metatable.doneIpairs then
mergeArgs(ipairs, firstArgs, secondArgsargTables)
metatable.doneIpairs = true
end