From aa740c4270a59328d082473501a734481c1eb379 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 17 Mar 2018 14:53:12 +0300 Subject: [PATCH] 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. --- spec/convert_spec.lua | 18 ++++++++++++++++++ src/argparse.lua | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spec/convert_spec.lua b/spec/convert_spec.lua index 7d509f9..30d6c67 100644 --- a/spec/convert_spec.lua +++ b/spec/convert_spec.lua @@ -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, diff --git a/src/argparse.lua b/src/argparse.lua index 26ea7d3..ed517ac 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -781,7 +781,7 @@ function ElementState:error(fmt, ...) self.state:error(fmt, ...) end -function ElementState:convert(argument) +function ElementState:convert(argument, index) local converter = self.element._convert if converter then @@ -789,6 +789,8 @@ function ElementState:convert(argument) if type(converter) == "function" then ok, err = converter(argument) + elseif type(converter[index]) == "function" then + ok, err = converter[index](argument) else ok = converter[argument] end @@ -844,7 +846,7 @@ function ElementState:invoke(alias) end function ElementState:pass(argument) - argument = self:convert(argument) + argument = self:convert(argument, #self.args + 1) table.insert(self.args, argument) if #self.args >= self.element._maxargs then