Jump to content
Wikipedia The Free Encyclopedia

Module:GetParameters

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 2,200,000 pages, or roughly 3% of all pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected.
Page protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.

Usage

getParameters

Takes 2 required arguments, frame_args and arg_list. Parses a frame's arguments, returning either the provided named arguments in arg_list if found or the positional parameters instead if not. This is designed to work around the stripping of values that takes place for defined parameters which could be important.

As an example, the calls getParameters({"a","b","c"},{"x","y"}) and getParameters({x="a",y="b",z="c"},{"x","y"}) would both give back {x="a",y="b"}.

getBoolean

Takes 1 required argument boolean_str. Turns the input into a true/false boolean value based on the input. Will error if given anything other than a string or boolean value.

defined

To be invoked from inside a template instead of a module. Determines if a certain parameter is defined in the parent frame's arguments.

See also

The above documentation is transcluded from Module:GetParameters/doc. (edit | history)
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.

 localp={}

 --[[
 Helper function that populates the argument list given that user may need to use a mix of
 named and unnamed parameters. This is relevant because named parameters are not
 identical to unnamed parameters due to string trimming, and when dealing with strings
 we sometimes want to either preserve or remove that whitespace depending on the application.
 ]]
 functionp.getParameters(frame_args,arg_list)
 localnew_args={};
 localindex=1;
 localvalue;

 fori,arginipairs(arg_list)do
 value=frame_args[arg]
 ifvalue==nilthen
 value=frame_args[index];
 index=index+1;
 end
 new_args[arg]=value;
 end

 returnnew_args;
 end

 --[[
 Helper Function to interpret boolean strings
 ]]
 functionp.getBoolean(boolean_str)
 localboolean_value;

 iftype(boolean_str)=='string'then
 boolean_str=boolean_str:lower();
 ifboolean_str=='false'orboolean_str=='no'orboolean_str=='0'
 orboolean_str==''then
 boolean_value=false;
 else
 boolean_value=true;
 end
 elseiftype(boolean_str)=='boolean'then
 boolean_value=boolean_str;
 else
 error('No boolean value found');
 end
 returnboolean_value
 end

 functionp.defined(frame)
 localarg=mw.text.trim(frame.args[1])
 --if arg == tostring(tonumber(arg)) then -- undesired result for '-0'
 --	arg = tonumber(arg)
 --end
 --if mw.ustring.find(arg, '^%s*-?[1-9][0-9]*%s*$') ~= nil or arg == '0' then
 --	arg = tonumber(arg)
 --end
 ifmw.ustring.find(arg,'^-?[1-9][0-9]*$')~=nilthen
 arg=tonumber(arg)
 elseifarg=='0'then
 arg=0
 end
 returnframe:getParent().args[arg]~=nil
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /