Changed actions behaviour

Actions are now just triggers. Actions for arguments and root parser ae removed.
This commit is contained in:
mpeterv
2014-02-26 12:07:53 +04:00
parent 13790c9e85
commit a4b93b0833
3 changed files with 15 additions and 56 deletions

View File

@@ -615,9 +615,7 @@ Error: malformed argument 'baz'
### Actions
argparse can trigger a callback when an option, argument or command is processed. The callback can be set using `action` field.
Action are mostly useful for creating flags triggering immediate reaction regardless of whether the rest of command line arguments are correct.
argparse can trigger a callback when an option or a command is encountered. The callback can be set using `action` field. Actions are called regardless of whether the rest of command line arguments are correct.
```lua
parser:flag "-v" "--version"

View File

@@ -18,10 +18,7 @@ describe("tests related to actions", function()
local args = parser:parse{"-fnowhere", "--pair", "Alice", "Bob", "-p", "Emma", "John"}
assert.same({from = "nowhere", pair = {{"Alice", "Bob"}, {"Emma", "John"}}}, args)
assert.spy(action1).called(1)
assert.spy(action1).called_with("nowhere")
assert.spy(action2).called(2)
assert.spy(action2).called_with({"Alice", "Bob"})
assert.spy(action2).called_with({"Emma", "John"})
end)
it("properly calls actions for flags", function()
@@ -48,47 +45,20 @@ describe("tests related to actions", function()
assert.spy(action3).called(0)
end)
it("calls actions for arguments", function()
local action1 = spy.new(function(x) end)
local action2 = spy.new(function(x) end)
local parser = Parser()
parser:argument "input" {
action = action1
}
parser:argument "other" {
action = action2,
args = "*"
}
local args = parser:parse{"nothing"}
assert.same({input = "nothing", other = {}}, args)
assert.spy(action1).called(1)
assert.spy(action1).called_with("nothing")
assert.spy(action2).called(1)
assert.spy(action2).called_with({})
end)
it("calls actions for commands", function()
local action1 = spy.new(function(x) end)
local action2 = spy.new(function(x) end)
local action = spy.new(function(x) end)
local parser = Parser "name" {
action = action1
}
local parser = Parser "name"
parser:flag "-v" "--verbose" {
count = "0-3"
}
local add = parser:command "add" {
action = action2
action = action
}
add:argument "something"
local args = parser:parse{"add", "me"}
assert.same({add = true, verbose = 0, something = "me"}, args)
assert.spy(action1).called(1)
assert.spy(action1).called_with(args)
assert.spy(action2).called(1)
assert.spy(action2).called_with(args)
assert.spy(action).called(1)
end)
end)

View File

@@ -52,7 +52,7 @@ local Parser = class {
_add_help = true,
_fields = {
"name", "description", "epilog", "require_command",
"action", "usage", "help", "add_help"
"usage", "help", "add_help"
}
}:include(Declarative)
@@ -73,7 +73,7 @@ local Argument = class {
_fields = {
"name", "description", "target", "args",
"minargs", "maxargs", "default", "convert",
"action", "usage", "argname"
"usage", "argname"
}
}:include(Declarative)
@@ -515,7 +515,6 @@ function Parser:_parse(args, errhandler)
local result = {}
local invocations = {}
local passed = {}
local com_callbacks = {}
local cur_option
local cur_arg_i = 1
local cur_arg
@@ -620,24 +619,12 @@ function Parser:_parse(args, errhandler)
cur_arg_i = cur_arg_i+1
cur_arg = arguments[cur_arg_i]
end
if element._action then
if element.multicount or element.twodimensional then
element._action(result[element._target][#result[element._target]])
else
element._action(result[element._target])
end
end
end
end
local function switch(p)
parser = p:prepare()
if parser._action then
table.insert(com_callbacks, parser._action)
end
for _, option in ipairs(parser._options) do
table.insert(options, option)
@@ -675,6 +662,12 @@ function Parser:_parse(args, errhandler)
return assert_(opt_context[name], "unknown option '%s'%s", name, get_tip(opt_context, name))
end
local function do_action(element)
if element._action then
element._action()
end
end
local function handle_argument(data)
if cur_option then
pass(cur_option, data)
@@ -691,6 +684,7 @@ function Parser:_parse(args, errhandler)
end
else
result[com._target] = true
do_action(com)
switch(com)
end
end
@@ -702,6 +696,7 @@ function Parser:_parse(args, errhandler)
end
cur_option = opt_context[data]
do_action(cur_option)
invoke(cur_option)
end
@@ -785,10 +780,6 @@ function Parser:_parse(args, errhandler)
end
end
for _, callback in ipairs(com_callbacks) do
callback(result)
end
return result
end