Arguments is an argument extractor for Scribunto module invocations. It is intended for use by other Lua modules, and should not be called from an invocation (
#invoke) directly. This module supports the following features:
- Trimming and blank argument removal.
- Argument inheritance between child and parent frames.
- Argument extraction for external modules and console input.
- Options to customise argument extraction behaviour.
Installation
Documentation
Package items
arguments.getArgs(frame, options)(function)- Main argument extraction utility. Arguments are memoized once fetched for optimal performance, as with the
frame.argsmetatable in Scribunto core. - The default argument lookup behaviour uses the child frame arguments first, then the parent frame arguments. There are numerous frame options to change this behaviour.
- The default value tidying behaviour trims parameter values if they are defined strings and treats blank strings as
nil. This can be customised in thegetArgsoptions. - Parameters:
frameScribunto frame object or Lua arguments table, passed from an invocation or Lua logic such asframe:getParent(). If this parameter does not have anargsfield and agetParentmethod,frameis assumed to be a Lua arguments table, such as the arguments from a named arguments call. (frame|table)optionsExtraction/processing options. (table; optional)options.trimWhether to trim the blank arguments present in the arguments table. Acceptsfalseonly. Default:true. (boolean; optional)options.removeBlanksWhether to remove blank arguments from the arguments table. Does not shift sequential arguments removed by the processing stage. Acceptsfalseonly. Default:true. (boolean; optional)options.valueFuncCustom value tidying function for use if thetrimandremoveBlanksoptions don't cover the developer's argument processing use case. (function; optional)options.frameOnlyOnly read arguments from child frame (theframeparameter - usually invocation frame). (boolean; optional)options.parentOnlyOnly read arguments fromframeparent (theframeparameter - usually template frame). (boolean; optional)options.parentFirstArgument lookup in theframeparent first, prioritised over the invocation frame arguments. (boolean; optional)options.wrappersIndividual value or array of values, listing wrapper title name(s) or article ID(s) to permit parent argument lookup from. (table; optional)options.wrapperAlias ofoptions.wrappers- contains title name or article ID to permit parent argument lookup from. (string|number; optional)options.readOnlyWhether to restrict write permissions to the arguments table. When set to a truthy value, an error will be thrown on any write attempt. (boolean; optional)options.noOverwriteWhether to restrict overwrite attempts on existing argument keys in the arguments table. When set to a truthy value, an error will be thrown on any write attempt that would result in an existing argument being overwritten. (boolean; optional)options.translateMap of parameter name aliases to their canonical argument parameter names. (table; optional)options.backtranslateMap of canonical parameter names to their argument parameter aliases. Supersedesoptions.translateif both options are in use. (table; optional)
- Errors:
- 'bad value assigned to option "valueFunc" (function expected, got $type)' (line 317; optional)
- 'could not write to argument table key "$key"; the table is read-only' (line 407; optional)
- 'could not write to argument table key "$key"; overwriting existing arguments is not permitted' (line 409; optional)
- Returns: Arguments extracted from invocation. The argument data is embedded as a metatable in the exported table and cannot be accessed with the
#operator ortablelibrary methods. However, the exported table can be written to if theoptions.readOnlyflag parameter is not truthy. (table) - Note: Reference tags in the form of
<ref>will generate phantom references when calling thepairsiterator on the arguments table, IF the<ref>tag does not appear in the dependent module's wikitext output. - Usage:
local getArgs = require('Module:Arguments').getArgs function p.main(frame) local args = getArgs(frame, { wrapper = 'Template:<TEMPLATE>' }) -- Use the args table here. -- A common paradigm is <code><nowiki>return p._main(args)</nowiki></code>. -- This allows other Lua modules to access the -- main logic in a performant manner without a -- frame object. end
Notes
- The
argstable from thearguments.getArgsfunction is a metatable for performance reasons. Thus, the table will not permit Lua table methods such as#args,next(args), andtablelibrary functions. - This module will eventually be adapted as a library in MediaWiki core, called as
require('getArgs'). The core library will removeoptions.parentOnly.