mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Add actions for arguments
This commit is contained in:
@@ -2,6 +2,30 @@ local Parser = require "argparse"
|
|||||||
getmetatable(Parser()).error = function(_, msg) error(msg) end
|
getmetatable(Parser()).error = function(_, msg) error(msg) end
|
||||||
|
|
||||||
describe("actions", function()
|
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()
|
it("for options are called", function()
|
||||||
local action1 = spy.new(function(_, _, arg)
|
local action1 = spy.new(function(_, _, arg)
|
||||||
assert.equal("nowhere", arg)
|
assert.equal("nowhere", arg)
|
||||||
|
@@ -256,7 +256,8 @@ local Argument = class({
|
|||||||
typechecked("target", "string"),
|
typechecked("target", "string"),
|
||||||
typechecked("defmode", "string"),
|
typechecked("defmode", "string"),
|
||||||
typechecked("show_default", "boolean"),
|
typechecked("show_default", "boolean"),
|
||||||
typechecked("argname", "string", "table")
|
typechecked("argname", "string", "table"),
|
||||||
|
typechecked("action", "function")
|
||||||
})
|
})
|
||||||
|
|
||||||
local Option = class({
|
local Option = class({
|
||||||
|
Reference in New Issue
Block a user