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)
|
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" {
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user