mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Changed actions behaviour
Actions are now just triggers. Actions for arguments and root parser ae removed.
This commit is contained in:
@@ -615,9 +615,7 @@ Error: malformed argument 'baz'
|
|||||||
|
|
||||||
### Actions
|
### Actions
|
||||||
|
|
||||||
argparse can trigger a callback when an option, argument or command is processed. The callback can be set using `action` field.
|
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.
|
||||||
|
|
||||||
Action are mostly useful for creating flags triggering immediate reaction regardless of whether the rest of command line arguments are correct.
|
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
parser:flag "-v" "--version"
|
parser:flag "-v" "--version"
|
||||||
|
@@ -18,10 +18,7 @@ describe("tests related to actions", function()
|
|||||||
local args = parser:parse{"-fnowhere", "--pair", "Alice", "Bob", "-p", "Emma", "John"}
|
local args = parser:parse{"-fnowhere", "--pair", "Alice", "Bob", "-p", "Emma", "John"}
|
||||||
assert.same({from = "nowhere", pair = {{"Alice", "Bob"}, {"Emma", "John"}}}, args)
|
assert.same({from = "nowhere", pair = {{"Alice", "Bob"}, {"Emma", "John"}}}, args)
|
||||||
assert.spy(action1).called(1)
|
assert.spy(action1).called(1)
|
||||||
assert.spy(action1).called_with("nowhere")
|
|
||||||
assert.spy(action2).called(2)
|
assert.spy(action2).called(2)
|
||||||
assert.spy(action2).called_with({"Alice", "Bob"})
|
|
||||||
assert.spy(action2).called_with({"Emma", "John"})
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("properly calls actions for flags", function()
|
it("properly calls actions for flags", function()
|
||||||
@@ -48,47 +45,20 @@ describe("tests related to actions", function()
|
|||||||
assert.spy(action3).called(0)
|
assert.spy(action3).called(0)
|
||||||
end)
|
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()
|
it("calls actions for commands", function()
|
||||||
local action1 = spy.new(function(x) end)
|
local action = spy.new(function(x) end)
|
||||||
local action2 = spy.new(function(x) end)
|
|
||||||
|
|
||||||
local parser = Parser "name" {
|
local parser = Parser "name"
|
||||||
action = action1
|
|
||||||
}
|
|
||||||
parser:flag "-v" "--verbose" {
|
parser:flag "-v" "--verbose" {
|
||||||
count = "0-3"
|
count = "0-3"
|
||||||
}
|
}
|
||||||
local add = parser:command "add" {
|
local add = parser:command "add" {
|
||||||
action = action2
|
action = action
|
||||||
}
|
}
|
||||||
add:argument "something"
|
add:argument "something"
|
||||||
|
|
||||||
local args = parser:parse{"add", "me"}
|
local args = parser:parse{"add", "me"}
|
||||||
assert.same({add = true, verbose = 0, something = "me"}, args)
|
assert.same({add = true, verbose = 0, something = "me"}, args)
|
||||||
assert.spy(action1).called(1)
|
assert.spy(action).called(1)
|
||||||
assert.spy(action1).called_with(args)
|
|
||||||
assert.spy(action2).called(1)
|
|
||||||
assert.spy(action2).called_with(args)
|
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
@@ -52,7 +52,7 @@ local Parser = class {
|
|||||||
_add_help = true,
|
_add_help = true,
|
||||||
_fields = {
|
_fields = {
|
||||||
"name", "description", "epilog", "require_command",
|
"name", "description", "epilog", "require_command",
|
||||||
"action", "usage", "help", "add_help"
|
"usage", "help", "add_help"
|
||||||
}
|
}
|
||||||
}:include(Declarative)
|
}:include(Declarative)
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ local Argument = class {
|
|||||||
_fields = {
|
_fields = {
|
||||||
"name", "description", "target", "args",
|
"name", "description", "target", "args",
|
||||||
"minargs", "maxargs", "default", "convert",
|
"minargs", "maxargs", "default", "convert",
|
||||||
"action", "usage", "argname"
|
"usage", "argname"
|
||||||
}
|
}
|
||||||
}:include(Declarative)
|
}:include(Declarative)
|
||||||
|
|
||||||
@@ -515,7 +515,6 @@ function Parser:_parse(args, errhandler)
|
|||||||
local result = {}
|
local result = {}
|
||||||
local invocations = {}
|
local invocations = {}
|
||||||
local passed = {}
|
local passed = {}
|
||||||
local com_callbacks = {}
|
|
||||||
local cur_option
|
local cur_option
|
||||||
local cur_arg_i = 1
|
local cur_arg_i = 1
|
||||||
local cur_arg
|
local cur_arg
|
||||||
@@ -620,24 +619,12 @@ function Parser:_parse(args, errhandler)
|
|||||||
cur_arg_i = cur_arg_i+1
|
cur_arg_i = cur_arg_i+1
|
||||||
cur_arg = arguments[cur_arg_i]
|
cur_arg = arguments[cur_arg_i]
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local function switch(p)
|
local function switch(p)
|
||||||
parser = p:prepare()
|
parser = p:prepare()
|
||||||
|
|
||||||
if parser._action then
|
|
||||||
table.insert(com_callbacks, parser._action)
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, option in ipairs(parser._options) do
|
for _, option in ipairs(parser._options) do
|
||||||
table.insert(options, option)
|
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))
|
return assert_(opt_context[name], "unknown option '%s'%s", name, get_tip(opt_context, name))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function do_action(element)
|
||||||
|
if element._action then
|
||||||
|
element._action()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function handle_argument(data)
|
local function handle_argument(data)
|
||||||
if cur_option then
|
if cur_option then
|
||||||
pass(cur_option, data)
|
pass(cur_option, data)
|
||||||
@@ -691,6 +684,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
result[com._target] = true
|
result[com._target] = true
|
||||||
|
do_action(com)
|
||||||
switch(com)
|
switch(com)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -702,6 +696,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
end
|
end
|
||||||
|
|
||||||
cur_option = opt_context[data]
|
cur_option = opt_context[data]
|
||||||
|
do_action(cur_option)
|
||||||
invoke(cur_option)
|
invoke(cur_option)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -785,10 +780,6 @@ function Parser:_parse(args, errhandler)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, callback in ipairs(com_callbacks) do
|
|
||||||
callback(result)
|
|
||||||
end
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user