Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
MainPage
Community portal
Recent changes
Random page
Help
Special pages
Donate
Search
Search
English
Appearance
Log in
Request account
Personal tools
Log in
Request account
Export translations
Translate
English
Language statistics
Message group statistics
Export
Tools
Tools
move to sidebar
hide
Actions
Language statistics
Message group statistics
Export
General
Printable version
In other projects
Appearance
move to sidebar
hide
Settings
Group
Category:Maintenance
Help:Blocking users
Help:Bots
Help:Categories
Help:Contents
Help:Deletion and undeletion
Help:Diff
Help:Editing pages
Help:ExpandTemplates
Help:Extension:ParserFunctions
Help:External searches
Help:Formatting
Help:Images
Help:Links
Help:Magic words
Help:Managing files
Help:Moving a page
Help:Namespaces
Help:Navigation
Help:Parser functions in templates
Help:Patrolled edits
Help:Preferences
Help:Protected pages
Help:Random page
Help:Random root page
Help:Range blocks
Help:Recent changes
Help:Redirects
Help:Skins
Help:Special pages
Help:Starting a new page
Help:Substitution
Help:System message
Help:Tables
Help:Talk pages
Help:TemplateData
Help:Templates
Help:TemplateStyles
Help:Tracking changes
Help:Undelete
Help:User contributions
Help:Watchlist
Help:What links here
Manual:Expr parser function syntax
Manual:Extending wiki markup
Manual:Hooks
Manual:Magic words
Manual:Messages API
Manual:Parser functions
Module:Arguments/doc
Module:Int/doc
Module:Message box/configuration/doc
Module:Message box/doc
Module:Template translation/doc
Module:Transcluder/doc
Module:Version/doc
Module:Yesno/doc
News
Parser extension tags
Parser function extensions
Project:Language policy
Template:Admin tip
Template:Advanced user manual
Template:Anontools
Template:ApiEx
Template:Autocat
Template:Auxiliary template common notice
Template:Branching
Template:Bugfix
Template:Bugzilla
Template:Button
Template:Candidate
Template:Caution
Template:Clarify
Template:Collapse top
Template:Component
Template:Deprecated-inline
Template:Description missing
Template:Distinguish
Template:Documentation
Template:Documentation subpage
Template:Done
Template:EmptyCatGood
Template:Escape template list
Template:Excerpt
Template:Extension development
Template:Fixtext
Template:For
Template:Further
Template:Help
Template:High-risk
Template:Historical
Template:I18n navigation
Template:In progress
Template:Intricate template
Template:Introduced-inline
Template:IRC
Template:Localized link
Template:Localized link/messagedoc
Template:Magic words
Template:Main
Template:MainPage
Template:Markup
Template:Markup/row
Template:Mbox templates
Template:Module rating
Template:Move
Template:MW version
Template:MW version/layout
Template:N/a
Template:Navbar
Template:Newarticletext
Template:No
Template:Not done
Template:Note
Template:Optional
Template:Parser functions nav
Template:Partially done
Template:PD Help Page
Template:Phabricator
Template:Ptag
Template:Purge
Template:Recentchangestext
Template:Removed-inline
Template:Required
Template:Rev
Template:Security
Template:See
Template:See also
Template:Sp-contributions-footer
Template:Sp-contributions-footer-anon
Template:Special page
Template:Template shortcut
Template:Template test cases notice
Template:TemplateData header
Template:Thankyou
Template:To do
Template:Tracked
Template:Translatable
Template:Translatable template name
Template:Unusedtemplatestext
Template:Update
Template:Used in system
Template:Watchlists navigation
Template:Why
Template:WikiNonStop News
Template:Yes
TemplateData
Language
aa - Afar
aae - Arbëresh
ab - Abkhazian
abr - Abron
abs - Ambonese Malay
ace - Acehnese
acf - Saint Lucian Creole
acm - Iraqi Arabic
ady - Adyghe
ady-cyrl - Adyghe (Cyrillic script)
aeb - Tunisian Arabic
aeb-arab - Tunisian Arabic (Arabic script)
aeb-latn - Tunisian Arabic (Latin script)
af - Afrikaans
aig - Antiguan and Barbudan Creole English
aln - Gheg Albanian
alt - Southern Altai
am - Amharic
ami - Amis
an - Aragonese
ang - Old English
ann - Obolo
anp - Angika
apc - Levantine Arabic
ar - Arabic
arc - Aramaic
arn - Mapuche
arq - Algerian Arabic
ary - Moroccan Arabic
arz - Egyptian Arabic
as - Assamese
ase - American Sign Language
ast - Asturian
atj - Atikamekw
av - Avaric
avk - Kotava
awa - Awadhi
ay - Aymara
az - Azerbaijani
azb - South Azerbaijani
ba - Bashkir
ban - Balinese
ban-bali - Balinese (Balinese script)
bar - Bavarian
bbc - Batak Toba
bbc-latn - Batak Toba (Latin script)
bcc - Southern Balochi
bci - Baoulé
bcl - Central Bikol
bdr - West Coast Bajau
be - Belarusian
be-tarask - Belarusian (Taraškievica orthography)
bew - Betawi
bg - Bulgarian
bgc - Haryanvi
bgn - Western Balochi
bh - Bhojpuri
bho - Bhojpuri
bi - Bislama
bjn - Banjar
blk - Pa'O
bm - Bambara
bn - Bangla
bo - Tibetan
bpy - Bishnupriya
bqi - Bakhtiari
br - Breton
brh - Brahui
bs - Bosnian
btm - Batak Mandailing
bto - Rinconada Bikol
bug - Buginese
bug-bugi - Buginese (Buginese script)
bxr - Russia Buriat
ca - Catalan
cbk-zam - Chavacano
ccp - Chakma
cdo - Mindong
cdo-hant - Mindong (Traditional Han script)
cdo-latn - Mindong (Latin script)
ce - Chechen
ceb - Cebuano
ch - Chamorro
chn - Chinook Jargon
cho - Choctaw
chr - Cherokee
chy - Cheyenne
ckb - Central Kurdish
co - Corsican
cop - Coptic
cps - Capiznon
cpx - Puxian
cpx-hans - Puxian (Simplified Han script)
cpx-hant - Puxian (Traditional Han script)
cpx-latn - Puxian (Latin script)
cr - Cree
crh - Crimean Tatar
crh-cyrl - Crimean Tatar (Cyrillic script)
crh-latn - Crimean Tatar (Latin script)
crh-ro - Dobrujan Tatar
cs - Czech
csb - Kashubian
cu - Church Slavic
cv - Chuvash
cy - Welsh
da - Danish
dag - Dagbani
de - German
de-at - Austrian German
de-ch - Swiss High German
de-formal - German (formal address)
dga - Southern Dagaare
din - Dinka
diq - Dimli
dlg - Dolgan
dsb - Lower Sorbian
dtp - Central Dusun
dty - Doteli
dua - Duala
dv - Divehi
dz - Dzongkha
ee - Ewe
efi - Efik
egl - Emilian
el - Greek
eml - Emiliano-Romagnolo
en - English
en-ca - Canadian English
en-gb - British English
eo - Esperanto
es - Spanish
es-419 - Latin American Spanish
es-formal - Spanish (formal address)
et - Estonian
eu - Basque
ext - Extremaduran
fa - Persian
fat - Fanti
ff - Fula
fi - Finnish
fit - Tornedalen Finnish
fj - Fijian
fo - Faroese
fon - Fon
fr - French
frc - Cajun French
frp - Arpitan
frr - Northern Frisian
fur - Friulian
fvr - Fur
fy - Western Frisian
ga - Irish
gaa - Ga
gag - Gagauz
gan - Gan
gan-hans - Gan (Simplified Han script)
gan-hant - Gan (Traditional Han script)
gcf - Guadeloupean Creole
gcr - Guianan Creole
gd - Scottish Gaelic
gl - Galician
gld - Nanai
glk - Gilaki
gn - Guarani
gom - Goan Konkani
gom-deva - Goan Konkani (Devanagari script)
gom-latn - Goan Konkani (Latin script)
gor - Gorontalo
got - Gothic
gpe - Ghanaian Pidgin
grc - Ancient Greek
gsw - Alemannic
gu - Gujarati
guc - Wayuu
gur - Frafra
guw - Gun
gv - Manx
ha - Hausa
hak - Hakka Chinese
hak-hans - Hakka (Simplified Han script)
hak-hant - Hakka (Traditional Han script)
hak-latn - Hakka (Latin script)
haw - Hawaiian
he - Hebrew
hi - Hindi
hif - Fiji Hindi
hif-latn - Fiji Hindi (Latin script)
hil - Hiligaynon
hke - Hunde
hno - Northern Hindko
ho - Hiri Motu
hoc-latn - Ho (Latin script)
hr - Croatian
hrx - Hunsrik
hsb - Upper Sorbian
hsn - Xiang
ht - Haitian Creole
hu - Hungarian
hu-formal - Hungarian (formal address)
hy - Armenian
hyw - Western Armenian
hz - Herero
ia - Interlingua
iba - Iban
ibb - Ibibio
id - Indonesian
ie - Interlingue
ig - Igbo
igl - Igala
ii - Sichuan Yi
ik - Inupiaq
ike-cans - Eastern Canadian (Aboriginal syllabics)
ike-latn - Eastern Canadian (Latin script)
ilo - Iloko
inh - Ingush
io - Ido
is - Icelandic
isv-cyrl - Interslavic (Cyrillic script)
isv-latn - Interslavic (Latin script)
it - Italian
iu - Inuktitut
ja - Japanese
jam - Jamaican Creole English
jbo - Lojban
jut - Jutish
jv - Javanese
ka - Georgian
kaa - Kara-Kalpak
kab - Kabyle
kai - Karekare
kaj - Jju
kbd - Kabardian
kbd-cyrl - Kabardian (Cyrillic script)
kbp - Kabiye
kcg - Tyap
kea - Kabuverdianu
kg - Kongo
kge - Komering
khw - Khowar
ki - Kikuyu
kiu - Kirmanjki
kj - Kuanyama
kjh - Khakas
kjp - Eastern Pwo
kk - Kazakh
kk-arab - Kazakh (Arabic script)
kk-cn - Kazakh (China)
kk-cyrl - Kazakh (Cyrillic script)
kk-kz - Kazakh (Kazakhstan)
kk-latn - Kazakh (Latin script)
kk-tr - Kazakh (Turkey)
kl - Kalaallisut
km - Khmer
kn - Kannada
knc - Central Kanuri
ko - Korean
ko-kp - Korean (North Korea)
koi - Komi-Permyak
kr - Kanuri
krc - Karachay-Balkar
kri - Krio
krj - Kinaray-a
krl - Karelian
ks - Kashmiri
ks-arab - Kashmiri (Arabic script)
ks-deva - Kashmiri (Devanagari script)
ksh - Colognian
ksw - S'gaw Karen
ku - Kurdish
ku-arab - Kurdish (Arabic script)
ku-latn - Kurdish (Latin script)
kum - Kumyk
kus - Kusaal
kv - Komi
kw - Cornish
ky - Kyrgyz
la - Latin
lad - Ladino
lb - Luxembourgish
lbe - Lak
lez - Lezghian
lfn - Lingua Franca Nova
lg - Ganda
li - Limburgish
lij - Ligurian
liv - Livonian
ljp - Lampung Api
lki - Laki
lld - Ladin
lmo - Lombard
ln - Lingala
lo - Lao
loz - Lozi
lrc - Northern Luri
lt - Lithuanian
ltg - Latgalian
lua - Luba-Lulua
lus - Mizo
luz - Southern Luri
lv - Latvian
lzh - Literary Chinese
lzz - Laz
mad - Madurese
mag - Magahi
mai - Maithili
map-bms - Banyumasan
mdf - Moksha
mg - Malagasy
mh - Marshallese
mhr - Eastern Mari
mi - Māori
min - Minangkabau
mk - Macedonian
ml - Malayalam
mn - Mongolian
mnc - Manchu
mnc-latn - Manchu (Latin script)
mnc-mong - Manchu (Mongolian script)
mni - Manipuri
mnw - Mon
mo - Moldovan
mos - Mossi
mr - Marathi
mrh - Mara
mrj - Western Mari
ms - Malay
ms-arab - Malay (Jawi script)
mt - Maltese
mui - Musi
mus - Muscogee
mwl - Mirandese
my - Burmese
myv - Erzya
mzn - Mazanderani
na - Nauru
nah - Nahuatl
nan - Minnan
nan-hant - Minnan (Traditional Han script)
nan-latn-pehoeji - Minnan (Pe̍h-ōe-jī)
nan-latn-tailo - Minnan (Tâi-lô)
nap - Neapolitan
nb - Norwegian Bokmål
nds - Low German
nds-nl - Low Saxon
ne - Nepali
new - Newari
ng - Ndonga
nia - Nias
nit - Southeastern Kolami
niu - Niuean
nl - Dutch
nl-informal - Dutch (informal address)
nmz - Nawdm
nn - Norwegian Nynorsk
no - Norwegian
nod - Northern Thai
nog - Nogai
nov - Novial
nqo - N’Ko
nr - South Ndebele
nrm - Norman
nso - Northern Sotho
nup - Nupe
nv - Navajo
ny - Nyanja
nyn - Nyankole
nyo - Nyoro
nys - Nyungar
oc - Occitan
ojb - Northwestern Ojibwa
olo - Livvi-Karelian
om - Oromo
or - Odia
os - Ossetic
pa - Punjabi
pag - Pangasinan
pam - Pampanga
pap - Papiamento
pap-aw - Papiamento (Aruba)
pcd - Picard
pcm - Nigerian Pidgin
pdc - Pennsylvania German
pdt - Plautdietsch
pfl - Palatine German
pi - Pali
pih - Pitcairn-Norfolk
pl - Polish
pms - Piedmontese
pnb - Western Punjabi
pnt - Pontic
prg - Prussian
ps - Pashto
pt - Portuguese
pt-br - Brazilian Portuguese
pwn - Paiwan
qqq - Message documentation
qu - Quechua
qug - Chimborazo Highland Quichua
rgn - Romagnol
rif - Riffian
rki - Arakanese
rm - Romansh
rmc - Carpathian Romani
rmy - Vlax Romani
rn - Rundi
ro - Romanian
roa-tara - Tarantino
rsk - Pannonian Rusyn
ru - Russian
rue - Rusyn
rup - Aromanian
ruq - Megleno-Romanian
ruq-cyrl - Megleno-Romanian (Cyrillic script)
ruq-latn - Megleno-Romanian (Latin script)
rut - Rutul
rw - Kinyarwanda
ryu - Okinawan
sa - Sanskrit
sah - Yakut
sas - Sasak
sat - Santali
sc - Sardinian
scn - Sicilian
sco - Scots
sd - Sindhi
sdc - Sassarese Sardinian
sdh - Southern Kurdish
se - Northern Sami
se-fi - Northern Sami (Finland)
se-no - Northern Sami (Norway)
se-se - Northern Sami (Sweden)
sei - Seri
ses - Koyraboro Senni
sg - Sango
sgs - Samogitian
sh - Serbo-Croatian
sh-cyrl - Serbo-Croatian (Cyrillic script)
sh-latn - Serbo-Croatian (Latin script)
shi - Tachelhit
shi-latn - Tachelhit (Latin script)
shi-tfng - Tachelhit (Tifinagh script)
shn - Shan
shy - Shawiya
shy-latn - Shawiya (Latin script)
si - Sinhala
simple - Simple English
sjd - Kildin Sami
sje - Pite Sami
sk - Slovak
skr - Saraiki
skr-arab - Saraiki (Arabic script)
sl - Slovenian
sli - Lower Silesian
sm - Samoan
sma - Southern Sami
smn - Inari Sami
sms - Skolt Sami
sn - Shona
so - Somali
sq - Albanian
sr - Serbian
sr-ec - Serbian (Cyrillic script)
sr-el - Serbian (Latin script)
srn - Sranan Tongo
sro - Campidanese Sardinian
ss - Swati
st - Southern Sotho
stq - Saterland Frisian
sty - Siberian Tatar
su - Sundanese
sv - Swedish
sw - Swahili
syl - Sylheti
szl - Silesian
szy - Sakizaya
ta - Tamil
tay - Atayal
tcy - Tulu
tdd - Tai Nuea
te - Telugu
tet - Tetum
tg - Tajik
tg-cyrl - Tajik (Cyrillic script)
tg-latn - Tajik (Latin script)
th - Thai
ti - Tigrinya
tig - Tigre
tk - Turkmen
tl - Tagalog
tly - Talysh
tly-cyrl - Talysh (Cyrillic script)
tn - Tswana
to - Tongan
tok - Toki Pona
tpi - Tok Pisin
tr - Turkish
tru - Turoyo
trv - Taroko
ts - Tsonga
tt - Tatar
tt-cyrl - Tatar (Cyrillic script)
tt-latn - Tatar (Latin script)
ttj - Tooro
tum - Tumbuka
tw - Twi
ty - Tahitian
tyv - Tuvinian
tzm - Central Atlas Tamazight
udm - Udmurt
ug - Uyghur
ug-arab - Uyghur (Arabic script)
ug-latn - Uyghur (Latin script)
uk - Ukrainian
ur - Urdu
uz - Uzbek
uz-cyrl - Uzbek (Cyrillic script)
uz-latn - Uzbek (Latin script)
ve - Venda
vec - Venetian
vep - Veps
vi - Vietnamese
vls - West Flemish
vmf - Main-Franconian
vmw - Makhuwa
vo - Volapük
vot - Votic
vro - Võro
wa - Walloon
wal - Wolaytta
war - Waray
wls - Wallisian
wlx - Wali
wo - Wolof
wuu - Wu
wuu-hans - Wu (Simplified Han script)
wuu-hant - Wu (Traditional Han script)
xal - Kalmyk
xh - Xhosa
xmf - Mingrelian
xsy - Saisiyat
yi - Yiddish
yo - Yoruba
yrl - Nheengatu
yua - Yucatec Maya
yue - Cantonese
yue-hans - Cantonese (Simplified Han script)
yue-hant - Cantonese (Traditional Han script)
za - Zhuang
zea - Zeelandic
zgh - Standard Moroccan Tamazight
zgh-latn - Standard Moroccan Tamazight (Latin script)
zh - Chinese
zh-cn - Chinese (China)
zh-hans - Simplified Chinese
zh-hant - Traditional Chinese
zh-hk - Chinese (Hong Kong)
zh-mo - Chinese (Macau)
zh-my - Chinese (Malaysia)
zh-sg - Chinese (Singapore)
zh-tw - Chinese (Taiwan)
zu - Zulu
Format
Export for off-line translation
Export in native format
Export in CSV format
Fetch
<noinclude><!-- -->{{#ifeq:{{SUBPAGENAME}}|doc||{{Documentation subpage}}}}<!-- --><languages /><!-- --></noinclude>{{#switch: | = <includeonly>{{Languages|Module:Arguments/doc}}</includeonly> <!-- Add categories where indicated at the bottom of this page and interwikis at Wikidata --> {{Used in system}} {{Module rating|release}} {{Module rating|protected}} <span lang="en" dir="ltr" class="mw-content-ltr">This module provides easy processing of arguments passed from {{Magic word|#invoke|...|ext=Scribunto|code=1|nowrap=1}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It is a meta-module, meant for use by other modules, and should not be called from {{tlc|<nowiki>#invoke:...</nowiki>}} directly.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Its features include:</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Easy trimming of arguments and removal of blank arguments.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Arguments can be passed by both the current frame and by the parent frame at the same time.</span> <span lang="en" dir="ltr" class="mw-content-ltr">(More details below.)</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Arguments can be passed in directly from another Lua module or from the debug console.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Arguments are fetched as needed, which can help avoid (some) problems with {{xtag|ref}} tags.</span> * <span lang="en" dir="ltr" class="mw-content-ltr">Most features can be customised.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> == Basic use == </div> <span lang="en" dir="ltr" class="mw-content-ltr">First, you need to load the module.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It contains one function, named <code>getArgs</code>.</span> <syntaxhighlight lang="lua"> local getArgs = require('Module:Arguments').getArgs </syntaxhighlight> <span lang="en" dir="ltr" class="mw-content-ltr">In the most basic scenario, you can use <code>getArgs</code> inside your main function.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The variable <code>args</code> is a table containing the arguments from {{tlc|<nowiki>#invoke:...</nowiki>}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">(See below for details.)</span> <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> <span lang="en" dir="ltr" class="mw-content-ltr">However, the recommended practice is to use a function just for processing arguments from {{tlc|<nowiki>#invoke:...</nowiki>}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This means that if someone calls your module from another Lua module you don't have to have a frame object available, which improves performance.</span> <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> <div lang="en" dir="ltr" class="mw-content-ltr"> If you want multiple functions to use the arguments, and you also want them to be accessible from {{tlc|<nowiki>#invoke:...</nowiki>}}, you can use a wrapper function. </div> <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> <div lang="en" dir="ltr" class="mw-content-ltr"> === Options === </div> <span lang="en" dir="ltr" class="mw-content-ltr">The following options are available.</span> <span lang="en" dir="ltr" class="mw-content-ltr">They are explained in the sections below.</span> <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> <div lang="en" dir="ltr" class="mw-content-ltr"> === Trimming and removing blanks === </div> <span lang="en" dir="ltr" class="mw-content-ltr">Blank arguments often trip up coders new to converting MediaWiki templates to Lua.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In template syntax, blank strings and strings consisting only of whitespace are considered <code>false</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, in Lua, blank strings and strings consisting of whitespace are considered <code>true</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This means that if you don't pay attention to such arguments when you write your Lua modules, you might treat something as <code>true</code> that should actually be treated as <code>false</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">To avoid this, by default this module removes all blank arguments.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Similarly, whitespace can cause problems when dealing with positional arguments.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Although whitespace is trimmed for named arguments coming from {{tlc|<nowiki>#invoke:...</nowiki>}}, it is preserved for positional arguments.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Most of the time this additional whitespace is not desired, so this module trims it off by default.</span> <span lang="en" dir="ltr" class="mw-content-ltr">However, sometimes you want to use blank arguments as input, and sometimes you want to keep additional whitespace.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This can be necessary to convert some templates exactly as they were written.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If you want to do this, you can set the <code>trim</code> and <code>removeBlanks</code> arguments to <code>false</code>.</span> <syntaxhighlight lang="lua"> local args = getArgs(frame, { trim = false, removeBlanks = false }) </syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> === Custom formatting of arguments === </div> <span lang="en" dir="ltr" class="mw-content-ltr">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.</span> <span lang="en" dir="ltr" class="mw-content-ltr">To do things like this you can use the <code>valueFunc</code> option.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The input to this option must be a function that takes two parameters, <code>key</code> and <code>value</code>, and returns a single value.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This value is what you will get when you access the field <code>key</code> in the <code>args</code> table.</span> '''<span lang="en" dir="ltr" class="mw-content-ltr">Example 1:</span>''' <span lang="en" dir="ltr" class="mw-content-ltr">This function preserves whitespace for the first positional argument, but trims all other arguments and removes all other blank arguments.</span> <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> '''<span lang="en" dir="ltr" class="mw-content-ltr">Example 2:</span>''' <span lang="en" dir="ltr" class="mw-content-ltr">This function removes blank arguments and converts all arguments to lower case, but doesn't trim whitespace from positional parameters.</span> <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|text= <span lang="en" dir="ltr" class="mw-content-ltr">The above functions will fail if passed input that is not of type <code>string</code> or <code>nil</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">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.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In this case, you will need to check the type of your input.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This is not a problem if you are using a function specially for arguments from {{tlc|<nowiki>#invoke:...</nowiki>}} (i.e. you have <code>p.main</code> and <code>p._main</code> functions, or something similar).</span> }} {{Collapse top|1=<span lang="en" dir="ltr" class="mw-content-ltr">Examples 1 and 2 with type checking</span>}} <div lang="en" dir="ltr" class="mw-content-ltr"> Example 1: </div> <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> <div lang="en" dir="ltr" class="mw-content-ltr"> Example 2: </div> <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> {{Collapse bottom}} <div lang="en" dir="ltr" class="mw-content-ltr"> Also, please note that the <code>valueFunc</code> function is called more or less every time an argument is requested from the <code>args</code> table, so if you care about performance you should make sure you aren't doing anything inefficient with your code. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Frames and parent frames === </div> <span lang="en" dir="ltr" class="mw-content-ltr">Arguments in the <code>args</code> table can be passed from the current frame or from its parent frame at the same time.</span> <span lang="en" dir="ltr" class="mw-content-ltr">To understand what this means, it is easiest to give an example.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Let's say that we have a module called <code>Module:ExampleArgs</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This module prints the first two positional arguments that it is passed.</span> {{Collapse top|1=<span lang="en" dir="ltr" class="mw-content-ltr">Module:ExampleArgs code</span>}} <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) local first = args[1] or '' local second = args[2] or '' return first .. ' ' .. second end return p </syntaxhighlight> {{Collapse bottom}} <span lang="en" dir="ltr" class="mw-content-ltr"><code>Module:ExampleArgs</code> is then called by <code>Template:ExampleArgs</code>, which contains the code <code><nowiki>{{#invoke:ExampleArgs|main|firstInvokeArg}}</nowiki></code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This produces the result "firstInvokeArg".</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Now if we were to call <code>Template:ExampleArgs</code>, the following would happen: </div> {{(!}} class="wikitable" style="width: 50em; max-width: 100%;" {{!}}- ! style="width: 60%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Code</span> ! style="width: 40%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Result</span> {{!}}- {{!}} <code><nowiki>{{ExampleArgs}}</nowiki></code> {{!}} firstInvokeArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg}}</nowiki></code> {{!}} firstInvokeArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg|secondTemplateArg}}</nowiki></code> {{!}} firstInvokeArg secondTemplateArg {{!)}} <span lang="en" dir="ltr" class="mw-content-ltr">There are three options you can set to change this behaviour: <code>frameOnly</code>, <code>parentOnly</code> and <code>parentFirst</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If you set <code>frameOnly</code> then only arguments passed from the current frame will be accepted; if you set <code>parentOnly</code> then only arguments passed from the parent frame will be accepted; and if you set <code>parentFirst</code> then arguments will be passed from both the current and parent frames, but the parent frame will have priority over the current frame.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Here are the results in terms of <code>Template:ExampleArgs</code>:</span> <dl> <dt><code>frameOnly</code></dt> <dd> {{(!}} class="wikitable" style="width: 50em; max-width: 100%;" {{!}}- ! style="width: 60%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Code</span> ! style="width: 40%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Result</span> {{!}}- {{!}} <code><nowiki>{{ExampleArgs}}</nowiki></code> {{!}} firstInvokeArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg}}</nowiki></code> {{!}} firstInvokeArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg|secondTemplateArg}}</nowiki></code> {{!}} firstInvokeArg {{!)}} </dd> <dt><code>parentOnly</code></dt> <dd> {{(!}} class="wikitable" style="width: 50em; max-width: 100%;" {{!}}- ! style="width: 60%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Code</span> ! style="width: 40%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Result</span> {{!}}- {{!}} <code><nowiki>{{ExampleArgs}}</nowiki></code> {{!}} {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg}}</nowiki></code> {{!}} firstTemplateArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg|secondTemplateArg}}</nowiki></code> {{!}} firstTemplateArg secondTemplateArg {{!)}} </dd> <dt><code>parentFirst</code></dt> <dd> {{(!}} class="wikitable" style="width: 50em; max-width: 100%;" {{!}}- ! style="width: 60%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Code</span> ! style="width: 40%;" {{!}} <span lang="en" dir="ltr" class="mw-content-ltr">Result</span> {{!}}- {{!}} <code><nowiki>{{ExampleArgs}}</nowiki></code> {{!}} firstInvokeArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg}}</nowiki></code> {{!}} firstTemplateArg {{!}}- {{!}} <code><nowiki>{{ExampleArgs|firstTemplateArg|secondTemplateArg}}</nowiki></code> {{!}} firstTemplateArg secondTemplateArg {{!)}} </dd> </dl> {{Note|text= {{Ordered list |1= <span lang="en" dir="ltr" class="mw-content-ltr">If you set both the <code>frameOnly</code> and <code>parentOnly</code> options, the module won't fetch any arguments at all from {{tlc|<nowiki>#invoke:...</nowiki>}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This is probably not what you want.</span> |2= <span lang="en" dir="ltr" class="mw-content-ltr">In some situations a parent frame may not be available, e.g. if <code>getArgs</code> is passed the parent frame rather than the current frame.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In this case, only the frame arguments will be used (unless <code>parentOnly</code> is set, in which case no arguments will be used) and the <code>parentFirst</code> and <code>frameOnly</code> options will have no effect.</span> }} }} <div lang="en" dir="ltr" class="mw-content-ltr"> === Wrappers === </div> <span lang="en" dir="ltr" class="mw-content-ltr">The <code>wrappers</code> option is used to specify a limited number of templates as ''wrapper templates'', that is, templates whose only purpose is to call a module.</span> <span lang="en" dir="ltr" class="mw-content-ltr">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 <code>getArgs</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This allows modules to be called by either {{tlc|<nowiki>#invoke:...</nowiki>}} 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.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For example, the only content of {{tlx|Navbox}} (excluding content in {{tag|noinclude}} tags) is <code><nowiki>{{#invoke:Navbox|navbox}}</nowiki></code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">There is no point in checking the arguments passed directly to the {{tlc|<nowiki>#invoke:...</nowiki>}} statement for this template, as no arguments will ever be specified there.</span> <span lang="en" dir="ltr" class="mw-content-ltr">We can avoid checking arguments passed to {{tlc|<nowiki>#invoke:...</nowiki>}} by using the <code>parentOnly</code> option, but if we do this then {{tlc|<nowiki>#invoke:...</nowiki>}} will not work from other pages either.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If this were the case, then {{tmpl|0={{para|text|$1}}|Some text}} in the code {{tmpl|0={{tlc|<nowiki>#invoke:Navbox</nowiki>|navbox|3=text=$1}}|Some text}} would be ignored completely, no matter what page it was used from.</span> <span lang="en" dir="ltr" class="mw-content-ltr">By using the <code>wrappers</code> option to specify [[Template:Navbox]] as a wrapper, we can make {{tmpl|0={{tlc|<nowiki>#invoke:Navbox</nowiki>|main|3=text=$1}}|Some text}} work from most pages, while still not requiring that the module check for arguments on the [[Template:Navbox]] page itself.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Wrappers can be specified either as a string, or as an array of strings. </div> <syntaxhighlight lang="lua"> local args = getArgs(frame, { wrappers = 'Template:Wrapper template' }) </syntaxhighlight> <syntaxhighlight lang="lua"> local args = getArgs(frame, { wrappers = { 'Template:Wrapper 1', 'Template:Wrapper 2', -- Any number of wrapper templates can be added here. } }) </syntaxhighlight> {{Note|text= {{Ordered list |1= <span lang="en" dir="ltr" class="mw-content-ltr">The module will automatically detect if it is being called from a wrapper template's <code>/sandbox</code> subpage, so there is no need to specify sandbox pages explicitly.</span> |2= <span lang="en" dir="ltr" class="mw-content-ltr">The <code>wrappers</code> option effectively changes the default of the <code>frameOnly</code> and <code>parentOnly</code> options.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If, for example, <code>parentOnly</code> were explicitly set to <code>false</code> with <code>wrappers</code> set, calls via wrapper templates would result in both frame and parent arguments being loaded, though calls not via wrapper templates would result in only frame arguments being loaded.</span> |3= <span lang="en" dir="ltr" class="mw-content-ltr">If the <code>wrappers</code> option is set and no parent frame is available, the module will always get the arguments from the frame passed to <code>getArgs</code>.</span> }} }} === Writing to the <code>args</code> table === <span lang="en" dir="ltr" class="mw-content-ltr">Sometimes it can be useful to write new values to the <code>args</code> table.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This is possible with the default settings of this module.</span> <span lang="en" dir="ltr" class="mw-content-ltr">(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 <code>args</code> table as needed.)</span> <syntaxhighlight lang="lua"> args.foo = 'some value' </syntaxhighlight> <span lang="en" dir="ltr" class="mw-content-ltr">It is possible to alter this behaviour with the <code>readOnly</code> and <code>noOverwrite</code> options.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If <code>readOnly</code> is set then it is not possible to write any values to the <code>args</code> table at all.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If <code>noOverwrite</code> 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 {{tlc|<nowiki>#invoke:...</nowiki>}}.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> === Ref tags === </div> <span lang="en" dir="ltr" class="mw-content-ltr">This module uses [[mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#Metatables|metatables]] to fetch arguments from {{tlc|<nowiki>#invoke:...</nowiki>}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This allows access to both the frame arguments and the parent frame arguments without using the <code>pairs()</code> function.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This can help if your module might be passed {{xtag|ref}} tags as input.</span> <span lang="en" dir="ltr" class="mw-content-ltr">As soon as {{xtag|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.</span> <span lang="en" dir="ltr" class="mw-content-ltr">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 no number that links to it.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This has been a problem with modules that use <code>pairs()</code> to detect whether to use the arguments from the frame or the parent frame, as those modules automatically process every available argument.</span> <span lang="en" dir="ltr" class="mw-content-ltr">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.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The problem will still occur if you use <code>pairs(args)</code> elsewhere in your module, however.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> === Known limitations === </div> <span lang="en" dir="ltr" class="mw-content-ltr">The use of metatables also has its downsides.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Most of the normal Lua table tools won't work properly on the <code>args</code> table, including the <code>#</code> operator, the <code>next()</code> function, and the functions in the <code>table</code> library.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If using these is important for your module, you should use your own argument processing function instead of this module.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> == Tests == </div> {{ModuleQuality}} <includeonly>{{Sandbox other|| <!-- Categories below this line; interwikis at Wikidata --> [[Category:Lua metamodules{{#translation:}}]] }}</includeonly><noinclude> [[Category:Module documentation pages{{#translation:}}]] </noinclude> | #default= {{#invoke:Template translation|renderTranslatedTemplate|template=Module:Arguments/doc|noshift=1|uselang={{int:lang}}}} }}
Search
Search
Export translations
Add topic