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

@@ -244,6 +244,7 @@ local Parser = class({
typechecked("require_command", "boolean"),
typechecked("handle_options", "boolean"),
typechecked("action", "function"),
typechecked("command_target", "string"),
add_help
})
@@ -260,6 +261,7 @@ local Command = class({
typechecked("require_command", "boolean"),
typechecked("handle_options", "boolean"),
typechecked("action", "function"),
typechecked("command_target", "string"),
add_help
}, Parser)
@@ -1013,6 +1015,11 @@ function ParseState:pass(arg)
else
local command = self:get_command(arg)
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)
end
end