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:
mpeterv
2015-11-23 15:54:31 +03:00
parent 79952deb42
commit 247ed9e874
2 changed files with 18 additions and 0 deletions

View File

@@ -23,6 +23,17 @@ describe("tests related to commands", function()
assert.has_error(function() parser:parse{"-q", "install"} end, "unknown option '-q'")
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()
local parser = Parser "name"
parser:flag "-v" "--verbose" {