added actions

This commit is contained in:
mpeterv
2014-01-20 20:34:44 +04:00
parent d97a6d8285
commit 75b01c8b38

View File

@@ -41,7 +41,8 @@ local Parser = class {
commands = {}, commands = {},
require_command = false, require_command = false,
fields = { fields = {
"name", "description", "target", "require_command" "name", "description", "target", "require_command",
"action"
} }
}:include(Declarative) }:include(Declarative)
@@ -56,7 +57,8 @@ local Argument = class {
count = 1, count = 1,
fields = { fields = {
"name", "description", "target", "args", "name", "description", "target", "args",
"minargs", "maxargs", "default", "convert" "minargs", "maxargs", "default", "convert",
"action"
} }
}:include(Declarative) }:include(Declarative)
@@ -69,7 +71,7 @@ local Option = Argument:extends {
"name", "aliases", "description", "target", "name", "aliases", "description", "target",
"args", "minargs", "maxargs", "count", "args", "minargs", "maxargs", "count",
"mincount", "maxcount", "default", "convert", "mincount", "maxcount", "default", "convert",
"overwrite" "overwrite", "action"
} }
} }
@@ -255,6 +257,7 @@ function Parser:parse(args)
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
@@ -337,13 +340,21 @@ function Parser:parse(args)
else else
parser:error("too few arguments") parser:error("too few arguments")
end end
end else
if element == cur_option then
cur_option = nil
elseif element == cur_arg then
cur_arg_i = cur_arg_i+1
cur_arg = arguments[cur_arg_i]
end
if element == cur_option then if element.action then
cur_option = nil if element.type == "multi-count" or element.type == "multi-count multi-arg" then
elseif element == cur_arg then element.action(result[element.target][#result[element.target]])
cur_arg_i = cur_arg_i+1 else
cur_arg = arguments[cur_arg_i] element.action(result[element.target])
end
end
end end
end end
@@ -351,6 +362,10 @@ function Parser:parse(args)
parser = p:prepare() parser = p:prepare()
charset = p.charset charset = p.charset
if p.action then
table.insert(com_callbacks, p.action)
end
for _, option in ipairs(p.options) do for _, option in ipairs(p.options) do
table.insert(options, option) table.insert(options, option)
@@ -490,6 +505,10 @@ function Parser:parse(args)
) )
end end
for _, callback in ipairs(com_callbacks) do
callback(result)
end
return result return result
end end