Module:Arguments/doc
This is the documentation page for Module:Arguments
Lua error: expandTemplate: template loop detected.
| <translate> This module is [[<tvar name=1>Special:MyLanguage/Category:Modules subject to page protection</tvar>|subject to {{<tvar name=2>#if:</tvar>|cascading|page}} protection]].</translate> <translate> It is a highly visible module in use by a very large number of pages.</translate> <translate> Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is [[<tvar name=1>Special:MyLanguage/Project:Protected page</tvar>|protected]] from editing.</translate> |
| File:Cascade-protection-shackle.svg | This page is fully protected with the cascading option enabled. Editing of this module may have been disabled either temporarily or permanently except for administrators. Changes can only be done by administrators, because it has been transcluded onto one or more cascade-protected pages. If you would like to make an edit, please use the {{editprotected}} template on their talk pages to get an administrator's attention. | File:Information.svg |
| In addition, this page is also move-protected. |
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 (for a module directly invocable by templates you might want to have a look at {{#invoke:params}}). 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.
<syntaxhighlight lang="lua"> local getArgs = require('Module:Arguments').getArgs </syntaxhighlight>
In the most basic scenario, you can use getArgs inside your main function. The variable args is a table containing the arguments from #invoke. (See below for details.)
<syntaxhighlight 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 </syntaxhighlight>
Recommended practice
However, the recommended practice is to use a separate function as the entry point from #invoke just for processing the arguments. This allows other Lua modules to call your core logic directly, improving performance by avoiding the overhead of interacting with the frame object.
<syntaxhighlight 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 </syntaxhighlight>
The way this is called from a template is {{#invoke:Example|main}} (optionally with some parameters like {{#invoke:Example|main|arg1=value1|arg2=value2}}), and the way this is called from a module is <syntaxhighlight lang=lua inline>require('Module:Example')._main({arg1 = 'value1', arg2 = value2, 'spaced arg3' = 'value3'})</syntaxhighlight>. What this second one does is construct a table with the arguments in it, then gives that table to the p._main(args) function, which uses it natively.
Multiple functions
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.
<syntaxhighlight lang="lua"> local getArgs = require('Module:Arguments').getArgs
local p = {}
local function makeInvokeFunc(funcName) return function (frame) local args = getArgs(frame) return p[funcName](args) end end
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 </syntaxhighlight>
Options
The following options are available. They are explained in the sections below.
<syntaxhighlight lang="lua"> local args = getArgs(frame, { trim = false, removeBlanks = false, valueFunc = function (key, value) -- Code for processing one argument end,
frameOnly = true, parentOnly = true, parentFirst = true,
wrappers = { 'Template:A wrapper template', 'Template:Another wrapper template' },
readOnly = true, noOverwrite = true }) </syntaxhighlight>
Trimming whitespace
MediaWiki trims whitespace for named arguments coming from #invoke or a template call, but preserves whitespace for positional arguments. By default, this module helps trim whitespace also for position arguments. To preserve whitespace for positional arguments, set the trim option to false.
<syntaxhighlight lang="lua"> local args = getArgs(frame, { trim = false }) </syntaxhighlight>
When the valueFunc option is given, the valueFunc function will be responsible for trimming whitespace, and the trim option will have no effect.
Removing blank arguments
"Blank arguments" are arguments from #invoke or template that are blank strings or consist of only whitespace. By default, this module removes all blank arguments. To preserve the blank arguments, set the removeBlanks option to false.
<syntaxhighlight lang="lua"> local args = getArgs(frame, { removeBlanks = false }) </syntaxhighlight>
This might be necessary for some templates' operation.
Note: When converting MediaWiki templates to Lua, keep in mind that in Lua, blank strings and strings consisting only of whitespace are considered true. If you don't pay attention to such blank arguments when you write your Lua modules, you might treat something as true that should actually be treated as false.
When the valueFunc option is given, the valueFunc function will be responsible for handling blank arguments, and the removeBlanks option will have no effect.
Custom formatting of arguments
Sometimes you want to remove some blank arguments but not others, or perhaps you might want to put all of the positional arguments in lower case. To do things like this you can use the valueFunc option. The input to this option must be a function that takes two parameters, key and value, and returns a single value. This value is what you will get when you access the field key in the args table.
Example 1: this function preserves whitespace for the first positional argument's value, but trims all other arguments' value and removes all other blank arguments. <syntaxhighlight lang="lua"> local args = getArgs(frame, { valueFunc = function (key, value) if key == 1 then return value elseif value then value = mw.text.trim(value) if value ~= then return value end end return nil end }) </syntaxhighlight>
Example 2: this function removes blank arguments and converts all argument values to lower case, but doesn't trim whitespace from positional parameters. <syntaxhighlight lang="lua"> local args = getArgs(frame, { valueFunc = function (key, value) if not value then return nil end value = mw.ustring.lower(value) if mw.ustring.find(value, '%S') then return value end return nil end }) </syntaxhighlight>
Note: the above functions will fail if passed input that is not of type string or nil. This might be the case if you use the getArgs function in the main function of your module, and that function is called by another Lua module. In this case, you will need to check the type of your input. This is not a problem if you are using a function specially for arguments from #invoke (i.e. you have p.main and p._main functions, or something similar).
| Examples 1 and 2 with type checking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Example 1: <syntaxhighlight lang="lua"> local args = getArgs(frame, { valueFunc = function (key, value) if key == 1 then return value elseif type(value) == 'string' then value = mw.text.trim(value) if value ~= then return value else return nil end else return value end end }) </syntaxhighlight> Example 2: <syntaxhighlight lang="lua"> local args = getArgs(frame, { valueFunc = function (key, value) if type(value) == 'string' then value = mw.ustring.lower(value) if mw.ustring.find(value, '%S') then return value else return nil end else return value end end }) </syntaxhighlight> Template:Cob Also, please note that the Frames and parent framesArguments in the
|