Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Module:Arguments/doc

From Wikipedia, the free encyclopedia
<Module:Arguments
iconThis is adocumentation subpage forModule:Arguments.
It may contain usage information,categories and other content that is not part of the original module page.
WarningThis Lua module is used in system messages, and onapproximately 36,200,000 pages, or roughly 56% of all pages.
Changes to it can cause immediate changes to the Wikipedia user interface.
To avoid major disruption and server load, any changes should be tested in the module's/sandbox or/testcases subpages, or in your ownmodule sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on thetalk page before implementing them.
Thismodule is rated asready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned onhelp pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed throughsandbox testing rather than repeated trial-and-error editing.
This module can only be edited byadministrators because it istranscluded onto one or morecascade-protected pages.

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:

  • Easytrimming 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

[edit]

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

localgetArgs=require('Module:Arguments').getArgs

In the most basic scenario, you can use getArgs inside your main function. The variableargs is a table containing the arguments from #invoke. (See below for details.)

localgetArgs=require('Module:Arguments').getArgslocalp={}functionp.main(frame)localargs=getArgs(frame)-- Main module code goes here.endreturnp

Recommended practice

[edit]

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 theframe object.

localgetArgs=require('Module:Arguments').getArgslocalp={}functionp.main(frame)localargs=getArgs(frame)returnp._main(args)endfunctionp._main(args)-- Main module code goes here.endreturnp

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 isrequire('Module:Example')._main({arg1='value1',arg2=value2,'spaced arg3'='value3'}). 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

[edit]

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.

localgetArgs=require('Module:Arguments').getArgslocalp={}localfunctionmakeInvokeFunc(funcName)returnfunction(frame)localargs=getArgs(frame)returnp[funcName](args)endendp.func1=makeInvokeFunc('_func1')functionp._func1(args)-- Code for the first function goes here.endp.func2=makeInvokeFunc('_func2')functionp._func2(args)-- Code for the second function goes here.endreturnp

Options

[edit]

The following options are available. They are explained in the sections below.

localargs=getArgs(frame,{trim=false,removeBlanks=false,valueFunc=function(key,value)-- Code for processing one argumentend,frameOnly=true,parentOnly=true,parentFirst=true,wrappers={'Template:A wrapper template','Template:Another wrapper template'},readOnly=true,noOverwrite=true})

Trimming whitespace

[edit]

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 thetrim option tofalse.

localargs=getArgs(frame,{trim=false})

When thevalueFunc option is given, thevalueFunc function will be responsible for trimming whitespace, and thetrim option will have no effect.

Removing blank arguments

[edit]

"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 theremoveBlanks option tofalse.

localargs=getArgs(frame,{removeBlanks=false})

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 thevalueFunc option is given, thevalueFunc function will be responsible for handling blank arguments, and theremoveBlanks option will have no effect.

Custom formatting of arguments

[edit]

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 thevalueFunc option. The input to this option must be a function that takes two parameters,key andvalue, and returns a single value. This value is what you will get when you access the fieldkey in theargs 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.

localargs=getArgs(frame,{valueFunc=function(key,value)ifkey==1thenreturnvalueelseifvaluethenvalue=mw.text.trim(value)ifvalue~=''thenreturnvalueendendreturnnilend})

Example 2: this function removes blank arguments and converts all argument values to lower case, but doesn't trim whitespace from positional parameters.

localargs=getArgs(frame,{valueFunc=function(key,value)ifnotvaluethenreturnnilendvalue=mw.ustring.lower(value)ifmw.ustring.find(value,'%S')thenreturnvalueendreturnnilend})

Note: the above functions will fail if passed input that is not of typestring ornil. This might be the case if you use thegetArgs 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 havep.main andp._main functions, or something similar).

Examples 1 and 2 with type checking

Example 1:

localargs=getArgs(frame,{valueFunc=function(key,value)ifkey==1thenreturnvalueelseiftype(value)=='string'thenvalue=mw.text.trim(value)ifvalue~=''thenreturnvalueelsereturnnilendelsereturnvalueendend})

Example 2:

localargs=getArgs(frame,{valueFunc=function(key,value)iftype(value)=='string'thenvalue=mw.ustring.lower(value)ifmw.ustring.find(value,'%S')thenreturnvalueelsereturnnilendelsereturnvalueendend})

Also, please note that thevalueFunc function is called more or less every time an argument is requested from theargs table, so if you care about performance you should make sure you aren't doing anything inefficient with your code.

Frames and parent frames

[edit]

Arguments in theargs table can be passed from the current frame or from its parent frame at the same time. To understand what this means, it is easiest to give an example. Let's say that we have a module calledModule:ExampleArgs. This module prints the first two positional arguments that it is passed.

Module:ExampleArgs code
localgetArgs=require('Module:Arguments').getArgslocalp={}functionp.main(frame)localargs=getArgs(frame)returnp._main(args)endfunctionp._main(args)localfirst=args[1]or''localsecond=args[2]or''returnfirst..' '..secondendreturnp

Template:ExampleArgs contains the code{{#invoke:ExampleArgs|main|''firstInvokeArg''}}.

Now if we were to callTemplate:ExampleArgs, the following would happen:

CodeResult
{{#invoke:ExampleArgs|main|''firstInvokeArg''}}(call #invoke directly without template)
firstInvokeArg

(call #invoke directly without template)

{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg secondTemplateArg

There are three options you can set to change this behaviour:frameOnly,parentOnly andparentFirst. If you setframeOnly then only arguments passed from the current frame will be accepted; if you setparentOnly then only arguments passed from the parent frame will be accepted; and if you setparentFirst then arguments will be passed from both the current and parent frames, but the parent frame will have priority over the current frame. Here are the results in terms ofTemplate:ExampleArgs:

frameOnly
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg
parentOnly
CodeResult
{{ExampleArgs}}
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg
parentFirst
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg

Notes:

  1. If you set both theframeOnly andparentOnly options, the module won't fetch any arguments at all from #invoke. This is probably not what you want.
  2. In some situations a parent frame may not be available, e.g. if getArgs is passed the parent frame rather than the current frame. In this case, only the frame arguments will be used (unless parentOnly is set, in which case no arguments will be used) and theparentFirst andframeOnly options will have no effect.

Wrappers

[edit]

Thewrappers option is used to specify a limited number of templates aswrapper templates, that is, templates whose only purpose is to call a module. If the module detects that it is being called from a wrapper template, it will only check for arguments in the parent frame; otherwise it will only check for arguments in the frame passed to getArgs. This allows modules to be called by either #invoke or through a wrapper template without the loss of performance associated with having to check both the frame and the parent frame for each argument lookup.

For example, the only content ofTemplate:Side box (excluding content in<noinclude>...</noinclude> tags) is{{#invoke:Side box|main}}. There is no point in checking the arguments passed directly to the #invoke statement for this template, as no arguments will ever be specified there. We can avoid checking arguments passed to #invoke by using theparentOnly option, but if we do this then #invoke will not work from other pages either. If this were the case, the|text=Some text in the code{{#invoke:Side box|main|text=Some text}} would be ignored completely, no matter what page it was used from. By using thewrappers option to specify 'Template:Side box' as a wrapper, we can make{{#invoke:Side box|main|text=Some text}} work from most pages, while still not requiring that the module check for arguments on theTemplate:Side box page itself.

Wrappers can be specified either as a string, or as an array of strings.

localargs=getArgs(frame,{wrappers='Template:Wrapper template'})


localargs=getArgs(frame,{wrappers={'Template:Wrapper 1','Template:Wrapper 2',-- Any number of wrapper templates can be added here.}})

Thewrappers option changes the default behaviors of theframeOnly andparentOnly options.

Behaviors offrameOnly andparentOnly in relations with wrapper templates
IfTemplate:ExampleArgs is specified as a wrapper template
parentOnly is true or not set

The frame arguments will not be used at all.

CodeResult
{{ExampleArgs}}
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg
parentOnly is false,parentFirst is false or not set
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg secondTemplateArg
parentOnly is false,parentFirst is true
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg
Ifwrappers is set butTemplate:ExampleArgs is not in thewrappers list
frameOnly is true or not set
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg
frameOnly is false,parentFirst is false or not set
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstInvokeArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstInvokeArg secondTemplateArg
frameOnly is false,parentFirst is true
CodeResult
{{ExampleArgs}}firstInvokeArg
{{ExampleArgs|firstTemplateArg}}firstTemplateArg
{{ExampleArgs|firstTemplateArg|secondTemplateArg}}firstTemplateArg secondTemplateArg

Notes:

  1. The module will automatically detect if it is being called from a wrapper template's /sandbox subpage, so there is no need to specify sandbox pages explicitly.
  2. If thewrappers option is set and no parent frame is available, the module will always get the arguments from the frame passed togetArgs.

Writing to the args table

[edit]

Sometimes it can be useful to write new values to the args table. This is possible with the default settings of this module. (However, bear in mind that it is usually better coding style to create a new table with your new values and copy arguments from the args table as needed.)

args.foo='some value'

It is possible to alter this behaviour with thereadOnly andnoOverwrite options. IfreadOnly is set then it is not possible to write any values to the args table at all. IfnoOverwrite is set, then it is possible to add new values to the table, but it is not possible to add a value if it would overwrite any arguments that are passed from #invoke.

Notes

[edit]

Ref tags

[edit]

This module usesmetatables to fetch arguments from #invoke. This allows access to both the frame arguments and the parent frame arguments without using thepairs() function. This can help if your module might be passed<ref>...</ref> tags as input.

As soon as<ref>...</ref> tags are accessed from Lua, they are processed by the MediaWiki software and the reference will appear in the reference list at the bottom of the article. If your module proceeds to omit the reference tag from the output, you will end up with a phantom reference – a reference that appears in the reference list but without any number linking to it. This has been a problem with modules that usepairs() to detect whether to use the arguments from the frame or the parent frame, as those modules automatically process every available argument.

This module solves this problem by allowing access to both frame and parent frame arguments, while still only fetching those arguments when it is necessary. The problem will still occur if you usepairs(args) elsewhere in your module, however.

Known limitations

[edit]

The use of metatables also has its downsides. Most of the normal Lua table tools won't work properly on the args table, including the# operator, thenext() function, and the functions in the table library. If using these is important for your module, you should use your own argument processing function instead of this module.

See also

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:Arguments/doc&oldid=1323386385"
Hidden category:

[8]ページ先頭

©2009-2026 Movatter.jp