Module:User:Mr. Stradivarius/sandbox5 explained

local mArguments = require('Module:Arguments')

local validFields =

local p =

local function err(msg) return string.format('Error: %s', msg)end

function p.main(frame) local args = mArguments.getArgs(frame) return p._main(args)end

function p._main(args) local input = args[1] local field = args[2] or args.field local showCityData = args.citydata

'yes' -- Validate input. if not input then return err('no input code specified') elseif not field then return err('no field specified') end -- Parse the input string. local substrings = mw.text.split(input, '_') local data = for i, substring in ipairs(substrings) do local key, value = mw.ustring.match(substring, '^(.-):(.*)$') if key and validFields[key] then data[key] = value end end -- Fetch the result. if field

'error' then -- Check for missing data. local missingFields = for validField in pairs(validFields) do if not data[validField] then table.insert(missingFields, validField) end end if #missingFields > 0 then local msg = 'the ' .. mw.text.listToText(missingFields) .. ' fields are missing' return err(msg) else return end else -- Return the specified field, or the blank string if it is missing. local result = data[field] or if field

'type' then -- Check for type values like "city(20000)". local city, population = mw.ustring.match(result, '^(city)%((.-)%)$') if city then if showCityData then return population else return city end end end return result endend

return p