Anonymous user
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
local fargs, pargs, luaArgs
if frame == mw.getCurrentFrame() then
fargs = frame.args
pargs = frame:getParent().args
else
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
local valueFunc = options.valueFunc
if valueFunc then
Line 47 ⟶ 48:
end
local function mergeArgs(iterator,
-- 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
-- which is how we avoid clashes between the frame/parent args and the Lua args.
if options.parentFirst then
table.insert(argTables, pargs)
table.insert(argTables, fargs)
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
--
--
--
--]]
metatable.__index = function (t, key)
Line 78 ⟶ 83:
return val
else
for i, argTable in ipairs(argTables) do
local
if
metaArgs[key] = argTableVal
▲ else
return
end
end
end
Line 95 ⟶ 101:
metatable.__pairs = function ()
if not metatable.donePairs then
mergeArgs(pairs,
metatable.donePairs = true
metatable.doneIpairs = true
Line 104 ⟶ 110:
metatable.__ipairs = function ()
if not metatable.doneIpairs then
mergeArgs(ipairs,
metatable.doneIpairs = true
end
|