יחידה:Arguments: הבדלים בין גרסאות בדף

create argument-processing module
 
return a table rather than a function, improve the comments
שורה 1: שורה 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 function getArguments(frame, options)
local arguments = {}
 
function arguments.getArgs(frame, options)
options = type(options) == 'table' and options or {}
options = type(options) == 'table' and options or {}


-- 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 assume arguments are passed directly in.
local fargs, pargs
local fargs, pargs
if frame == mw.getCurrentFrame() then
if frame == mw.getCurrentFrame() then
שורה 31: שורה 36:
end
end


-- Use a user-generated functions to tidy the values if specified.
local valueFunc = options.valueFunc
local valueFunc = options.valueFunc
if valueFunc then
if valueFunc then
שורה 54: שורה 60:
end
end


-- Set the order of precedence of frame args and parent args.
local firstArgs, secondArgs = fargs, pargs
local firstArgs, secondArgs = fargs, pargs
if options.parentFirst then
if options.parentFirst then
שורה 59: שורה 66:
end
end


-- Define metatable behaviour.
metatable.__index = function (t, key)
metatable.__index = function (t, key)
local val = metaArgs[key]
local val = metaArgs[key]
שורה 99: שורה 107:
end
end


return getArguments
return arguments