mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
False return from converter is now valid, only nil is signal for error
This commit is contained in:
@@ -12,6 +12,25 @@ describe("tests related to converters", function()
|
||||
assert.same({numbers = {1, 2, 500}}, args)
|
||||
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()
|
||||
local parser = Parser()
|
||||
parser:argument "numbers" {
|
||||
|
@@ -509,11 +509,11 @@ function Parser:_parse(args, errhandler)
|
||||
local function convert(element, data)
|
||||
if element._convert then
|
||||
local ok, err = element._convert(data)
|
||||
|
||||
return assert_(ok, "%s", err or "malformed argument '" .. data .. "'")
|
||||
else
|
||||
return data
|
||||
assert_(ok ~= nil, "%s", err or "malformed argument '" .. data .. "'")
|
||||
data = ok
|
||||
end
|
||||
|
||||
return data
|
||||
end
|
||||
|
||||
local invoke, pass, close
|
||||
|
Reference in New Issue
Block a user