Attention Mobile version Mobile Users! Registration for Mobile 1.3 Beta Testing is live! Register here!

Module:Args

From Terraria Wiki
Jump to: navigation, search

Used for handling arguments and thus mainly used from within other modules.


return {
	getRaw = function (key)
		local frame = mw.getCurrentFrame()
		return frame.args[key] or frame:getParent().args[key] or nil
	end,

	-- return trimmed input value, if not keep_blank, it will return nil when input == ''(invoked with |arg= )
	get = function(key, keep_blank)
		local frame = mw.getCurrentFrame()
		local value = frame.args[key] or frame:getParent().args[key] or nil
		if value then
			value = mw.text.trim(value)
			if value ~= '' or keep_blank then
				return value
			end
		end
		return nil
	end,

	-- return a bool value, n/no/off = false, any other value(inluding blank input or no input) = true
	-- if strict = true, n/no/off = false, y/yes/on = true, any other value = nil.
	getFlag = function(key, strict)
		local frame = mw.getCurrentFrame()
		local value = frame.args[key] or frame:getParent().args[key] or nil
		if value then
			value = mw.text.trim(value):lower()
		end
		if strict then
			if value == 'y' or value == 'yes' or value == 'on' then
				return true
			end
			if value == 'n' or value == 'no' or value == 'off' then
				return false
			end
			return nil
		else
			if value == 'n' or value == 'no' or value == 'off' then
				return false
			else
				return true
			end
		end
	end,
	
	-- option = {trim=true, keep_blank=false}
	getAll = function(option)
		option = option or {}
		local trim = option['trim'] or true
		local keep_blank = option['keep_blank'] or false

		local frame = mw.getCurrentFrame()

		local args = frame.args
		local parent_args = frame:getParent().args
		
		for k, v in pairs(args) do
			parent_args[k] = v
		end
		
		result = {}
		for k, v in pairs(parent_args) do
			if trim then
				v = mw.text.trim(v)
			end
			if keep_blank then
				result[k] = v
			else
				if v ~= '' then
					result[k] = v
				end
			end
		end

		return result
	end,
}