Convert can now be a table used as mapping

This commit is contained in:
mpeterv
2014-02-20 12:34:02 +04:00
parent 754788b986
commit 8bc02caae6
2 changed files with 24 additions and 1 deletions

View File

@@ -12,6 +12,22 @@ describe("tests related to converters", function()
assert.same({numbers = {1, 2, 500}}, args)
end)
it("converts arguments using mapping", function()
local choice = {
foo = 1,
bar = 2
}
local parser = Parser()
parser:argument "choice" {
convert = choice,
args = "+"
}
local args = parser:parse{"foo", "bar"}
assert.same({choice = {1, 2}}, args)
end)
it("accepts false", function()
local function toboolean(x)
if x == "true" then

View File

@@ -508,7 +508,14 @@ function Parser:_parse(args, errhandler)
local function convert(element, data)
if element._convert then
local ok, err = element._convert(data)
local ok, err
if type(element._convert) == "function" then
ok, err = element._convert(data)
else
ok, err = element._convert[data]
end
assert_(ok ~= nil, "%s", err or "malformed argument '" .. data .. "'")
data = ok
end