Module:Arguments/doc: Difference between revisions

From Mariopedia, a wiki on Mario, Yoshi, Wario, Donkey Kong, Super Smash Bros., and more!
Jump to navigationJump to search
add valueFunc info
(add info on trimming and removing blanks)
(add valueFunc info)
Line 13:
</source>
 
In the most basic scenario, you can use getArgs inside your main function. The variable <code>args</code> is a table containing the arguments from #invoke. (See below for details.)
 
<source lang="lua">
Line 102:
local args = getArgs(frame, {
trim = false,
removeBlanks = false,
})
</source>
 
=== Custom trimming and blank removal ===
 
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 <code>valueFunc</code> option. The input to this option must be a function that takes two parameters, <code>key</code> <code>value</code>, and returns the value of the argument. This function is called every time an argument is requested from the <code>args</code> table.
 
Example 1: this function preserves whitespace for the first positional argument, but trims all other arguments and removes all other blank arguments.
<source lang="lua">
local args = getArgs(frame, {
valueFunc = function(key, value)
if key == 1 then
return value
else
value = mw.text.trim(value)
if value ~= '' then
return value
end
end
end
})
</source>
 
Example 2: this function removes blank arguments and converts all arguments to lower case, but doesn't trim whitespace from positional parameters.
<source lang="lua">
local args = getArgs(frame, {
valueFunc = function(key, value)
value = mw.ustring.lower(value)
if mw.ustring.find(value, '%S') then
return value
end
end
})
</source>
 
Note: if you are using the <code>getArgs</code> function in the main function of your module, be aware that if it is called from another Lua module then <code>value</code> might not always be a string. This is not a problem if you are using a function specially for arguments from #invoke (i.e. you have <code>p.main</code> and <code>p._main</code> functions, or something similar).
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu