mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
New Parser/Command property 'command_target'
Allows saving name of used command in a field of result target. Helpful when command callbacks are stored in a table with names as keys or when they are in submodules of some namespace. Example: local parser = argparse():command_target("command") -- Add commands... local args = parser:parse() require("namespace." .. args.command).run(args)
This commit is contained in:
@@ -23,6 +23,17 @@ describe("tests related to commands", function()
|
|||||||
assert.has_error(function() parser:parse{"-q", "install"} end, "unknown option '-q'")
|
assert.has_error(function() parser:parse{"-q", "install"} end, "unknown option '-q'")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("uses command_target property to save command name", function()
|
||||||
|
local parser = Parser "name"
|
||||||
|
:add_help(false)
|
||||||
|
:command_target("command")
|
||||||
|
local install = parser:command "install"
|
||||||
|
install:flag "-q" "--quiet"
|
||||||
|
|
||||||
|
local args = parser:parse{"install", "-q"}
|
||||||
|
assert.same({install = true, quiet = true, command = "install"}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
it("allows to continue passing old options", function()
|
it("allows to continue passing old options", function()
|
||||||
local parser = Parser "name"
|
local parser = Parser "name"
|
||||||
parser:flag "-v" "--verbose" {
|
parser:flag "-v" "--verbose" {
|
||||||
|
@@ -244,6 +244,7 @@ local Parser = class({
|
|||||||
typechecked("require_command", "boolean"),
|
typechecked("require_command", "boolean"),
|
||||||
typechecked("handle_options", "boolean"),
|
typechecked("handle_options", "boolean"),
|
||||||
typechecked("action", "function"),
|
typechecked("action", "function"),
|
||||||
|
typechecked("command_target", "string"),
|
||||||
add_help
|
add_help
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -260,6 +261,7 @@ local Command = class({
|
|||||||
typechecked("require_command", "boolean"),
|
typechecked("require_command", "boolean"),
|
||||||
typechecked("handle_options", "boolean"),
|
typechecked("handle_options", "boolean"),
|
||||||
typechecked("action", "function"),
|
typechecked("action", "function"),
|
||||||
|
typechecked("command_target", "string"),
|
||||||
add_help
|
add_help
|
||||||
}, Parser)
|
}, Parser)
|
||||||
|
|
||||||
@@ -1013,6 +1015,11 @@ function ParseState:pass(arg)
|
|||||||
else
|
else
|
||||||
local command = self:get_command(arg)
|
local command = self:get_command(arg)
|
||||||
self.result[command._target or command._name] = true
|
self.result[command._target or command._name] = true
|
||||||
|
|
||||||
|
if self.parser._command_target then
|
||||||
|
self.result[self.parser._command_target] = command._name
|
||||||
|
end
|
||||||
|
|
||||||
self:switch(command)
|
self:switch(command)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user