Skip to content

Commit

Permalink
Add actions for arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Oct 30, 2015
1 parent babe715 commit decd204
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/actions_spec.lua
Expand Up @@ -2,6 +2,30 @@ local Parser = require "argparse"
getmetatable(Parser()).error = function(_, msg) error(msg) end

describe("actions", function()
it("for arguments are called", function()
local parser = Parser()
local foo
parser:argument("foo"):action(function(_, _, passed_foo)
foo = passed_foo
end)
local baz
parser:argument("baz"):args("*"):action(function(_, _, passed_baz)
baz = passed_baz
end)

parser:parse{"a"}
assert.equals("a", foo)
assert.same({}, baz)

parser:parse{"b", "c"}
assert.equals("b", foo)
assert.same({"c"}, baz)

parser:parse{"d", "e", "f"}
assert.equals("d", foo)
assert.same({"e", "f"}, baz)
end)

it("for options are called", function()
local action1 = spy.new(function(_, _, arg)
assert.equal("nowhere", arg)
Expand Down
3 changes: 2 additions & 1 deletion src/argparse.lua
Expand Up @@ -256,7 +256,8 @@ local Argument = class({
typechecked("target", "string"),
typechecked("defmode", "string"),
typechecked("show_default", "boolean"),
typechecked("argname", "string", "table")
typechecked("argname", "string", "table"),
typechecked("action", "function")
})

local Option = class({
Expand Down

0 comments on commit decd204

Please sign in to comment.