Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Module:Arguments/doc

From Ubuntu Wiki

This is the documentation page for Module:Arguments

This module provides a standardized interface for fetching and normalizing parameters passed to Scribunto Lua modules from frame and parent frame contexts.

Features

  • Trims leading and trailing whitespace from string parameters by default.
  • Converts empty string parameters to nil by default.
  • Merges parameters passed directly to #invoke with parameters passed to the parent template.
  • Uses lazy evaluation via metatables so parameters are only processed when accessed.

Usage

To use this module in another Lua module, require it and call getArgs:

local Arguments = require('Module:Arguments')

local p = {}

function p.main(frame)
    local args = Arguments.getArgs(frame)
    
    -- Access positional or named parameters directly
    local firstArg = args[1] or 'default value'
    local title = args.title or 'default title'
    
    return firstArg .. ' - ' .. title
end

return p

Functions

getArgs

Arguments.getArgs(frame, options)

Returns a table containing processed arguments from the given frame and/or parent frame.

frame
The Scribunto frame object passed by MediaWiki.
options
(Optional) A table of configuration options altering how parameters are retrieved and sanitized.

Configuration Options

Option Type Default Description
trim boolean true If true, trims leading and trailing whitespace from parameter strings.
removeBlanks boolean true If true, converts empty parameter strings (or whitespace-only strings) to nil.
parentOnly boolean false If true, fetches parameters only from the parent template context, ignoring direct #invoke parameters.
frameOnly boolean false If true, fetches parameters only from the #invoke frame context, ignoring parent template parameters.
parentFirst boolean false If true, parameters set on the parent template override parameters set on the #invoke frame when key names conflict.
valueFunc function nil A custom callback function(key, value) to preprocess, modify, or validate individual parameter values.

Examples

Retaining Whitespace and Blank Values

To preserve raw whitespace and empty string arguments:

local args = Arguments.getArgs(frame, {
    trim = false,
    removeBlanks = false
})

Using a Custom Value Function

To automatically convert numeric parameters to Lua numbers:

local args = Arguments.getArgs(frame, {
    valueFunc = function(key, val)
        if key == 'count' or key == 1 then
            return tonumber(val)
        end
        return val
    end
})

Unit Tests

Unit tests are maintained at Module:Arguments/testcases. To execute the suite on a page, insert:

{{#invoke:arguments/testcases|run}}