mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Convert can now be a table used as mapping
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user