Skip to content

Commit

Permalink
Respect empty initial value in 'append' and 'concat' actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Oct 31, 2015
1 parent 6dc13c3 commit 79952de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spec/actions_spec.lua
Expand Up @@ -115,6 +115,21 @@ describe("actions", function()
assert.same({foo = false, bar = true}, args)
end)

it("'append' and 'concat' respect initial value", function()
local parser = Parser()
parser:option("-f"):count("*"):init(nil)
parser:option("-g"):args("*"):count("*"):action("concat"):init(nil)

local args = parser:parse{}
assert.same({}, args)

args = parser:parse{"-fabc", "-fdef", "-g"}
assert.same({f = {"abc", "def"}, g = {}}, args)

args = parser:parse{"-g", "abc", "def", "-g123", "-f123"}
assert.same({f = {"123"}, g = {"abc", "def", "123"}}, args)
end)

it("for options allow setting initial stored value as non-string argument to default", function()
local parser = Parser()
parser:flag("--no-foo", "Foo the bar.", true):target("foo"):action("store_false")
Expand Down
3 changes: 3 additions & 0 deletions src/argparse.lua
Expand Up @@ -368,6 +368,7 @@ function actions.count(result, target, _, overwrite)
end

function actions.append(result, target, argument, overwrite)
result[target] = result[target] or {}
table.insert(result[target], argument)

if overwrite then
Expand All @@ -380,6 +381,8 @@ function actions.concat(result, target, arguments, overwrite)
error("'concat' action can't handle too many invocations")
end

result[target] = result[target] or {}

for _, argument in ipairs(arguments) do
table.insert(result[target], argument)
end
Expand Down

0 comments on commit 79952de

Please sign in to comment.