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:
Peter Melnichenko
2018-03-17 14:53:12 +03:00
parent 7a8236751f
commit aa740c4270
2 changed files with 22 additions and 2 deletions

View File

@@ -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,