Jump to content

Module:Arguments/doc: Difference between revisions

From Fifth Empire Wiki
start the documentation
 
add wrapper function info
Line 32: Line 32:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end


function p._main(args)
function p._main(args)
Line 37: Line 42:
end
end


function p.main(frame)
return p
local args = getArgs(frame)
</source>
return p._main(args)
 
If you want multiple functions to use the arguments, and you also want them to be accessible from #invoke, you can use a wrapper function.
 
<source lang="lua">
local getArgs = require('Module:Arguments').getArgs
 
local function makeInvokeFunc(funcName)
return function (frame)
local args = getArgs(frame)
return p[funcName](args)
end
end
 
local p = {}
 
p.func1 = makeInvokeFunc('_func1')
 
function p._func1(args)
-- Code for the first function goes here.
end
 
p.func2 = makeInvokeFunc('_func2')
 
function p._func2(args)
-- Code for the second function goes here.
end
end


return p
return p
</source>
</source>

Revision as of 04:58, 9 December 2013

This module provides easy processing of arguments passed from #invoke. It is a meta-module, meant for use by other modules, and should not be called from #invoke directly. Its features include:

  • Easy trimming of arguments and removal of blank arguments.
  • Arguments can be passed by both the current frame and by the parent frame at the same time. (More details below.)
  • Arguments can be passed in directly from another Lua module or from the debug console.
  • Most features can be customized.

Basic use

First, you need to load the module. It contains one function, named getArgs.

<source lang="lua"> local getArgs = require('Module:Arguments').getArgs </source>

In the most basic scenario, you can use getArgs inside your main function.

<source lang="lua"> local getArgs = require('Module:Arguments').getArgs local p = {}

function p.main(frame) local args = getArgs(frame) -- Main module code goes here. end

return p </source>

However, the recommended practice is to use a function just for processing arguments from #invoke. This means that if someone calls your module from another Lua module you don't have to have a frame object available, which improves performance.

<source lang="lua"> local getArgs = require('Module:Arguments').getArgs local p = {}

function p.main(frame) local args = getArgs(frame) return p._main(args) end

function p._main(args) -- Main module code goes here. end

return p </source>

If you want multiple functions to use the arguments, and you also want them to be accessible from #invoke, you can use a wrapper function.

<source lang="lua"> local getArgs = require('Module:Arguments').getArgs

local function makeInvokeFunc(funcName) return function (frame) local args = getArgs(frame) return p[funcName](args) end end

local p = {}

p.func1 = makeInvokeFunc('_func1')

function p._func1(args) -- Code for the first function goes here. end

p.func2 = makeInvokeFunc('_func2')

function p._func2(args) -- Code for the second function goes here. end

return p </source>