Jump to content

Module:BananasArgs

د ويکيپېډيا، وړیا پوهنغونډ له خوا

-- Sample Module demonstrating how to access arguments. -- For more about the Frame object, see http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object -- Unit tests at Module:BananasArgs/testcases

local p = {} local converter = require("Module:Numeral converter")

-- No arguments, used like: Script error: No such module "BananasArgs". function p.hello_world() return "سلام، نړۍ!" end

-- One argument, used like: Script error: No such module "BananasArgs". function p.hello(frame) local name = frame.args[1] -- in this example, args[1] is the word Fred return "سلام، " .. name .. "!" -- .. name .. replace by the word Fred end

-- Two arguments, used like: Script error: No such module "BananasArgs". function p.add(frame) local num1 = tonumber(frame.args[1]) local num2 = tonumber(frame.args[2]) return converter.convert("fa", num1 + num2) end

-- Named arguments, used like: Script error: No such module "BananasArgs". function p.count_fruit(frame) local num_bananas = converter.convert("fa", frame.args.bananas) local num_apples = converter.convert("fa", frame.args.apples) return 'زه ' .. num_bananas .. ' کېله او ' .. num_apples .. ' مڼه لرم' end

-- Mixing regular args with named args and optional named args -- Used like: Script error: No such module "BananasArgs". function p.has_fruit(frame) local name = frame.args[1] local num_bananas = converter.convert("fa", frame.args.bananas) local num_apples = converter.convert("fa", frame.args.apples) local num_cherries = converter.convert("fa", frame.args.cherries)

local result = name .. ' لري:' if num_bananas then result = result .. ' ' .. num_bananas .. ' کېله' end if num_apples then result = result .. ' ' .. num_apples .. ' مڼه' end if num_cherries then result = result .. ' ' .. num_cherries .. ' ګيلاس' end return result end

-- Iterating over args, used like: Script error: No such module "BananasArgs". function p.custom_fruit(frame) local result = 'زه لرم:' for name, value in pairs(frame.args) do result = result .. ' ' .. converter.convert("fa", value) .. ' ' .. name end return result end

-- Iterating over args with separate mandatory args -- Used like: Script error: No such module "BananasArgs". function p.custom_fruit_2(frame) local name = frame.args[1] local result = name .. ' لري:' for name, value in pairs(frame.args) do if name ~= 1 then result = result .. ' ' .. converter.convert("fa", value) .. ' ' .. name end end return result end

return p