False return from converter is now valid, only nil is signal for error

This commit is contained in:
mpeterv
2014-02-20 12:28:46 +04:00
parent 0434fb5395
commit 754788b986
2 changed files with 23 additions and 4 deletions

View File

@@ -12,6 +12,25 @@ describe("tests related to converters", function()
assert.same({numbers = {1, 2, 500}}, args) assert.same({numbers = {1, 2, 500}}, args)
end) end)
it("accepts false", function()
local function toboolean(x)
if x == "true" then
return true
elseif x == "false" then
return false
end
end
local parser = Parser()
parser:argument "booleans" {
convert = toboolean,
args = "+"
}
local args = parser:parse{"true", "false"}
assert.same({booleans = {true, false}}, args)
end)
it("raises an error when it can't convert", function() it("raises an error when it can't convert", function()
local parser = Parser() local parser = Parser()
parser:argument "numbers" { parser:argument "numbers" {

View File

@@ -509,11 +509,11 @@ function Parser:_parse(args, errhandler)
local function convert(element, data) local function convert(element, data)
if element._convert then if element._convert then
local ok, err = element._convert(data) local ok, err = element._convert(data)
assert_(ok ~= nil, "%s", err or "malformed argument '" .. data .. "'")
return assert_(ok, "%s", err or "malformed argument '" .. data .. "'") data = ok
else
return data
end end
return data
end end
local invoke, pass, close local invoke, pass, close