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
Content deleted Content added
put frame examples in tables for easier comparison |
→Custom formatting of arguments: tweak valueFunc examples so that they don't break with nil arguments, and make all code paths return explicit nils |
||
Line 120: | Line 120: | ||
if key == 1 then |
if key == 1 then |
||
return value |
return value |
||
elseif value then |
|||
⚫ | |||
value = mw.text.trim(value) |
value = mw.text.trim(value) |
||
if value ~= '' then |
if value ~= '' then |
||
Line 126: | Line 126: | ||
end |
end |
||
end |
end |
||
return nil |
|||
end |
end |
||
}) |
}) |
||
Line 134: | Line 135: | ||
local args = getArgs(frame, { |
local args = getArgs(frame, { |
||
valueFunc = function (key, value) |
valueFunc = function (key, value) |
||
if not value then |
|||
return nil |
|||
end |
|||
value = mw.ustring.lower(value) |
value = mw.ustring.lower(value) |
||
if mw.ustring.find(value, '%S') then |
if mw.ustring.find(value, '%S') then |
||
return value |
return value |
||
end |
end |
||
return nil |
|||
end |
end |
||
}) |
}) |
||
</source> |
</source> |
||
Note: the above functions will fail if passed input that is not of type <code>string</code>. This might be the case if you use the <code>getArgs</code> 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 <code>p.main</code> and <code>p._main</code> functions, or something similar). |
Note: the above functions will fail if passed input that is not of type <code>string</code> or <code>nil</code>. This might be the case if you use the <code>getArgs</code> 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 <code>p.main</code> and <code>p._main</code> functions, or something similar). |
||
{{cot|Examples 1 and 2 with type checking}} |
{{cot|Examples 1 and 2 with type checking}} |
||
Line 155: | Line 160: | ||
if value ~= '' then |
if value ~= '' then |
||
return value |
return value |
||
⚫ | |||
return nil |
|||
end |
end |
||
else |
else |
||
Line 171: | Line 178: | ||
if mw.ustring.find(value, '%S') then |
if mw.ustring.find(value, '%S') then |
||
return value |
return value |
||
else |
|||
return nil |
|||
end |
end |
||
else |
else |