Bước tới nội dung

Mô đun:parameters/lite

Từ điển mở Wiktionary
local export = {}

local function clean(x) return x and #x > 0 and x or nil end

-- This modifies params!
-- Only supports required, aliases (a list of alternative names)
function export.process(args, params)
	for key, options in pairs(params) do
		local value = clean(args[key])
		local required
		
		if type(options) == "boolean" then
			required = options
		elseif type(options) == "table" then
			if not value and options.aliases then
				for _, alias in ipairs(options.aliases) do
					value = clean(args[alias])
					if value then break end
				end
			end
			required = options.required
		end
		
		if value then
			value = mw.text.trim(value)
		elseif required then
			error("Parameter " .. key .. " is required")
		end
		
		params[key] = value
	end

	return params
end

return export