mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Allow using separate converters for each argument
When converting an argument in a position, if there is a function in the same position in provided converer array, use it as a converter function. This allows using an array of functions as the value of `convert` property, with each function applied to corresponding argument of a multi-argument option. Ref #14.
This commit is contained in:
@@ -13,6 +13,24 @@ describe("tests related to converters", function()
|
||||
assert.same({numbers = {1, 2, 500}}, args)
|
||||
end)
|
||||
|
||||
it("accepts an array of converters", function()
|
||||
local function tocoords(str)
|
||||
local x, y = str:match("^([^,]*),([^,]*)$")
|
||||
x = tonumber(x)
|
||||
y = tonumber(y)
|
||||
return x and y and {x, y}
|
||||
end
|
||||
|
||||
local parser = Parser()
|
||||
parser:option "-c --circle" {
|
||||
convert = {tonumber, tocoords},
|
||||
args = 2
|
||||
}
|
||||
|
||||
local args = parser:parse{"-c", "123", "456,567"}
|
||||
assert.same({circle = {123, {456, 567}}}, args)
|
||||
end)
|
||||
|
||||
it("converts arguments using mapping", function()
|
||||
local choice = {
|
||||
foo = 1,
|
||||
|
Reference in New Issue
Block a user