모듈:BananasArgs

Wikipedia>Jesusmas님의 2013년 9월 15일 (일) 20:55 판 (새 문서: -- 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 -- ...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

예제

예제 결과
{{#invoke:BananasArgs|hello_world}} Hello, world!
{{#invoke:BananasArgs|hello|민준}} Hello, 민준!
{{#invoke:BananasArgs|add|5|3}} 8
{{#invoke:BananasArgs|count_fruit|바나나=5|사과=3}} 29번째 줄에서 Lua 오류: attempt to concatenate local 'num_apples' (a nil value).
{{#invoke:BananasArgs|has_fruit|민준|바나나=5|체리=7}} 민준 has:
{{#invoke:BananasArgs|custom_fruit|파인애플=10|키위=5}} I have: 10 파인애플 5 키위
{{#invoke:BananasArgs|custom_fruit_2|민준|파인애플=10|키위=5}} 민준 has: 5 키위 10 파인애플

같이 보기


-- 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/tests
 
local p = {}
 
-- No arguments, used like: {{#invoke:BananasArgs|hello_world}}
function p.hello_world()
    return "Hello, world!"
end
 
-- One argument, used like: {{#invoke:BananasArgs|hello|Fred}}
function p.hello(frame)
    local name = frame.args[1]
    return "Hello, " .. name .. "!"
end
 
-- Two arguments, used like: {{#invoke:BananasArgs|add|5|3}}
function p.add(frame)
    local num1 = tonumber(frame.args[1])
    local num2 = tonumber(frame.args[2])
    return num1 + num2
end
 
-- Named arguments, used like: {{#invoke:BananasArgs|count_fruit|bananas=5|apples=3}}
function p.count_fruit(frame)
    local num_bananas = frame.args['bananas']
    local num_apples = frame.args['apples']
    return 'I have ' .. num_bananas .. ' bananas and ' .. num_apples .. ' apples'
end
 
-- Mixing regular args with named args and optional named args
-- Used like: {{#invoke:BananasArgs|has_fruit|Fred|bananas=5|cherries=7}}
function p.has_fruit(frame)
    local name = frame.args[1]
    local num_bananas = frame.args['bananas']
    local num_apples = frame.args['apples']
    local num_cherries = frame.args['cherries']
 
    local result = name .. ' has:'
    if num_bananas then result = result .. ' ' .. num_bananas .. ' bananas' end
    if num_apples then result = result .. ' ' .. num_apples .. ' apples' end
    if num_cherries then result = result .. ' ' .. num_cherries .. ' cherries' end
    return result
end
 
-- Iterating over args, used like: {{#invoke:BananasArgs|custom_fruit|pineapples=10|kiwis=5}}
function p.custom_fruit(frame)
    local result = 'I have:'
    for name, value in pairs(frame.args) do
        result = result .. ' ' .. value .. ' ' .. name
    end
    return result
end
 
-- Iterating over args with separate mandatory args
-- Used like: {{#invoke:BananasArgs|custom_fruit_2|Fred|pineapples=10|kiwis=5}}
function p.custom_fruit_2(frame)
    local name = frame.args[1]
    local result = name .. ' has:'
    for name, value in pairs(frame.args) do
        if name ~= 1 then
            result = result .. ' ' .. value .. ' ' .. name
        end
    end
    return result
end
 
return p