Implement command actions

This commit is contained in:
mpeterv
2015-10-31 15:50:23 +03:00
parent decd2040fa
commit 4f9841dec6
2 changed files with 42 additions and 1 deletions

View File

@@ -221,6 +221,7 @@ local Parser = class({
typechecked("help", "string"),
typechecked("require_command", "boolean"),
typechecked("handle_options", "boolean"),
typechecked("action", "function"),
add_help
})
@@ -236,6 +237,7 @@ local Command = class({
typechecked("help", "string"),
typechecked("require_command", "boolean"),
typechecked("handle_options", "boolean"),
typechecked("action", "function"),
add_help
}, Parser)
@@ -840,7 +842,8 @@ local ParseState = class({
arguments = {},
argument_i = 1,
element_to_mutexes = {},
mutex_to_used_option = {}
mutex_to_used_option = {},
command_actions = {}
})
function ParseState:__call(parser, error_handler)
@@ -858,6 +861,10 @@ end
function ParseState:switch(parser)
self.parser = parser
if parser._action then
table.insert(self.command_actions, parser._action)
end
for _, option in ipairs(parser._options) do
option = ElementState(self, option)
table.insert(self.options, option)
@@ -1004,6 +1011,10 @@ function ParseState:finalize()
end
end
end
for i = #self.command_actions, 1, -1 do
self.command_actions[i](self.result)
end
end
function ParseState:parse(args)