Module:Arguments

From Halopedia, the Halo wiki

Documentation for this module may be created at ModuleDoc:Arguments

local utils = require( 'Module:Utils' )

local arguments = {}

function arguments.preprocess( frame, args )
	
	-- TODO: Complete error messages, make sure that in the preprocessed version, the keys for numbered args are still numbered
	
	local parent = frame:getParent()
	
	local valid = {}
	local required = {}
	local recommended = {}
	local deprecated = {}
	
	for i, v in ipairs( args.valid or {} ) do
		local vl = mw.ustring.lower( tostring( v ) )
		valid[ vl ] = true
	end
	for i, v in ipairs( args.required or {} ) do
		local vl = mw.ustring.lower( tostring( v ) )
		valid[ vl ] = true
		required[ vl ] = true
	end
	for i, v in ipairs( args.recommended or {} ) do
		local vl = mw.ustring.lower( tostring( v ) )
		valid[ vl ] = true
		recommended[ vl ] = true
	end
	for i, v in ipairs( args.deprecated or {} ) do
		local vl = mw.ustring.lower( tostring( v ) )
		valid[ vl ] = true
		deprecated[ vl ] = true
	end
	for k, v in pairs( args.aliases or {} ) do
		local kl = mw.ustring.lower( tostring( k ) )
		local vl = mw.ustring.lower( tostring( v ) )
		if valid[vl] then
			valid[ kl ] = true
		end
	end
	
	local preprocessed = {}
	
	local missingRequired = required
	local missingRecommended = recommended
	local usingDeprecated = {}
	local usingInvalid = {}
	local clobbered = {}
	
	for k, v in pairs( parent.args ) do
		local kl = mw.ustring.lower( tostring( k ) )
		if deprecated[ kl ] then
			usingDeprecated[ kl ] = true
		end
		kl = args.aliases[ kl ] or kl
		missingRequired[ kl ] = nil
		missingRecommended[ kl ] = nil
		if not valid[ kl ] then
			usingInvalid[ kl ] = true
		end
		if deprecated[ kl ] then
			usingDeprecated[ kl ] = true
		end
		if not utils.empty( preprocessed[ kl ] ) then
			clobbered[ kl ] = true
		end
		preprocessed[ kl ] = v
	end
	for i, v in ipairs( parent.args ) do
		local k = args.aliases[ i ] or i
		missingRequired[ k ] = nil
		missingRecommended[ k ] = nil
		if not valid[ k ] then
			usingInvalid[ k ] = true
		end
		if deprecated[ k ] then
			usingDeprecated[ k ] = true
		end
		if not utils.empty( preprocessed[ kl ] ) then
			clobbered[ kl ] = true
		end
		preprocessed[ k ] = v
	end
	
	local out = ''
	if next( missingRequired ) then
		local list = ''
		for k, v in pairs( missingRequired ) do
			list = list .. ', "' .. k .. '"'
		end
		list = mw.ustring.sub( list, 3 )
		out = out .. utils.error(
			'Template missing required fields: ' .. list,
			'template',
			'Pages containing missing template parameter errors')
	end
	if next( missingRecommended ) then
		local list = ''
		for k, v in pairs( missingRecommended ) do
			list = list .. ', "' .. k .. '"'
		end
		list = mw.ustring.sub( list, 3 )
		out = out .. utils.warning(
			'Template missing recommended fields: ' .. list,
			'template',
			'Pages containing missing template parameter warnings')
	end
	if next( usingInvalid ) then
		local list = ''
		for k, v in pairs( usingInvalid ) do
			list = list .. ', "' .. k .. '"'
		end
		list = mw.ustring.sub( list, 3 )
		out = out .. utils.error(
			'Template using invalid/disallowed fields: ' .. list,
			'template',
			'Pages containing invalid template parameter errors')
	end
	if next( usingDeprecated ) then
		local list = ''
		for k, v in pairs( usingDeprecated ) do
			list = list .. ', "' .. k .. '"'
		end
		list = mw.ustring.sub( list, 3 )
		out = out .. utils.warning(
			'Template using deprecated fields: ' .. list,
			'template',
			'Pages containing deprecated template parameter warnings')
	end
	if next( clobbered ) then
		local list = ''
		for k, v in pairs( clobbered ) do
			list = list .. ', "' .. k .. '"'
		end
		list = mw.ustring.sub( list, 3 )
		out = out .. utils.error(
			'Template using multiple aliases for the same field(s): ' .. list,
			'template',
			'Pages containing overwritten template parameter errors')
	end
	
	return preprocessed, out
end

return arguments