mirror of
https://github.com/TangentFoxy/argparse.git
synced 2026-03-14 12:46:50 -06:00
added conversions
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
local argparse = require "argparse"
|
||||||
|
|
||||||
|
describe("tests related to converters", function()
|
||||||
|
it("converts arguments", function()
|
||||||
|
local parser = argparse.parser()
|
||||||
|
parser:argument "numbers" {
|
||||||
|
convert = tonumber,
|
||||||
|
args = "+"
|
||||||
|
}
|
||||||
|
|
||||||
|
local args = parser:parse{"1", "2", "500"}
|
||||||
|
assert.same({numbers = {1, 2, 500}}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("raises an error when it can't convert", function()
|
||||||
|
local parser = argparse.parser()
|
||||||
|
parser:argument "numbers" {
|
||||||
|
convert = tonumber,
|
||||||
|
args = "+"
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.has_error(function() parser:parse{"foo", "bar", "baz"} end, "malformed argument foo")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("second return value is used as error message", function()
|
||||||
|
local parser = argparse.parser()
|
||||||
|
parser:argument "numbers" {
|
||||||
|
convert = function(x) return tonumber(x), x .. " is not a number" end
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.has_error(function() parser:parse{"foo"} end, "foo is not a number")
|
||||||
|
end)
|
||||||
|
end)
|
||||||
+24
-4
@@ -397,6 +397,16 @@ function Parser:parse(args)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function convert(element, data)
|
||||||
|
if element.convert then
|
||||||
|
local ok, err = element.convert(data)
|
||||||
|
|
||||||
|
return parser:assert(ok, "%s", err or "malformed argument " .. data)
|
||||||
|
else
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function format()
|
local function format()
|
||||||
local invocations
|
local invocations
|
||||||
|
|
||||||
@@ -416,22 +426,32 @@ function Parser:parse(args)
|
|||||||
end
|
end
|
||||||
elseif element.maxargs == 1 and element.minargs == 1 then
|
elseif element.maxargs == 1 and element.minargs == 1 then
|
||||||
if #invocations > 0 then
|
if #invocations > 0 then
|
||||||
result[element.target] = invocations[1][1]
|
result[element.target] = convert(element, invocations[1][1])
|
||||||
else
|
else
|
||||||
result[element.target] = nil
|
result[element.target] = nil
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
result[element.target] = invocations[1]
|
result[element.target] = invocations[1]
|
||||||
|
|
||||||
|
if #invocations > 0 and element.convert then
|
||||||
|
for i=1, #result[element.target] do
|
||||||
|
result[element.target][i] = convert(element, result[element.target][i])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if element.maxargs == 0 then
|
if element.maxargs == 0 then
|
||||||
result[element.target] = #invocations
|
result[element.target] = #invocations
|
||||||
elseif element.maxargs == 1 and element.minargs == 1 then
|
elseif element.maxargs == 1 and element.minargs == 1 then
|
||||||
for i=1, #invocations do
|
for i=1, #invocations do
|
||||||
invocations[i] = invocations[i][1]
|
invocations[i] = convert(element, invocations[i][1])
|
||||||
|
end
|
||||||
|
elseif element.convert then
|
||||||
|
for _, invocation in ipairs(invocations) do
|
||||||
|
for i=1, #invocation do
|
||||||
|
invocation[i] = convert(element, invocation[i])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
|
||||||
result[element.target] = invocations
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user