mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-29 11:32:19 +00:00
Refactoring - removed :prepare calls for elements
This commit is contained in:
158
src/argparse.lua
158
src/argparse.lua
@@ -197,7 +197,7 @@ local Option = add_setters(Argument:extends {
|
|||||||
argname = typecheck.string "argname"
|
argname = typecheck.string "argname"
|
||||||
})
|
})
|
||||||
|
|
||||||
function Argument:get_arg_usage(argname)
|
function Argument:_get_arg_usage(argname)
|
||||||
argname = self._argname or argname
|
argname = self._argname or argname
|
||||||
local buf = {}
|
local buf = {}
|
||||||
local required_argname = argname
|
local required_argname = argname
|
||||||
@@ -229,78 +229,70 @@ function Argument:get_arg_usage(argname)
|
|||||||
return buf
|
return buf
|
||||||
end
|
end
|
||||||
|
|
||||||
function Argument:get_usage()
|
function Argument:_get_usage()
|
||||||
if not self._usage then
|
if self._usage then
|
||||||
self._usage = table.concat(self:get_arg_usage("<" .. self._name .. ">"), " ")
|
return self._usage
|
||||||
|
end
|
||||||
|
|
||||||
|
local usage = table.concat(self:_get_arg_usage("<" .. self._name .. ">"), " ")
|
||||||
|
|
||||||
if self._default and self._defmode:find "u" then
|
if self._default and self._defmode:find "u" then
|
||||||
if self._maxargs > 1 or (self._minargs == 1 and not self._defmode:find "a") then
|
if self._maxargs > 1 or (self._minargs == 1 and not self._defmode:find "a") then
|
||||||
self._usage = "[" .. self._usage .. "]"
|
usage = "[" .. usage .. "]"
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return self._usage
|
return usage
|
||||||
end
|
end
|
||||||
|
|
||||||
function Argument:make_target()
|
function Argument:_get_type()
|
||||||
if not self._target then
|
|
||||||
self._target = self._name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Argument:make_type()
|
|
||||||
if self._maxcount == 1 then
|
if self._maxcount == 1 then
|
||||||
if self._maxargs == 0 then
|
if self._maxargs == 0 then
|
||||||
self.flag = true
|
return "flag"
|
||||||
elseif self._maxargs == 1 and (self._minargs == 1 or self._mincount == 1) then
|
elseif self._maxargs == 1 and (self._minargs == 1 or self._mincount == 1) then
|
||||||
self.arg = true
|
return "arg"
|
||||||
else
|
else
|
||||||
self.multiarg = true
|
return "multiarg"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if self._maxargs == 0 then
|
if self._maxargs == 0 then
|
||||||
self.counter = true
|
return "counter"
|
||||||
elseif self._maxargs == 1 and self._minargs == 1 then
|
elseif self._maxargs == 1 and self._minargs == 1 then
|
||||||
self.multicount = true
|
return "multicount"
|
||||||
else
|
else
|
||||||
self.twodimensional = true
|
return "twodimensional"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Argument:prepare()
|
function Option:_get_usage()
|
||||||
self:make_target()
|
if self._usage then
|
||||||
self:make_type()
|
return self._usage
|
||||||
return self
|
end
|
||||||
end
|
|
||||||
|
|
||||||
function Option:get_usage()
|
local usage = self:_get_arg_usage("<" .. self:_get_target() .. ">")
|
||||||
if not self._usage then
|
table.insert(usage, 1, self._name)
|
||||||
self._usage = self:get_arg_usage("<" .. self._target .. ">")
|
usage = table.concat(usage, " ")
|
||||||
table.insert(self._usage, 1, self._name)
|
|
||||||
self._usage = table.concat(self._usage, " ")
|
|
||||||
|
|
||||||
if self._mincount == 0 or self._default then
|
if self._mincount == 0 or self._default then
|
||||||
self._usage = "[" .. self._usage .. "]"
|
usage = "[" .. usage .. "]"
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return self._usage
|
return usage
|
||||||
end
|
end
|
||||||
|
|
||||||
function Option:make_target()
|
function Option:_get_target()
|
||||||
if not self._target then
|
if self._target then
|
||||||
|
return self._target
|
||||||
|
end
|
||||||
|
|
||||||
for _, alias in ipairs(self._aliases) do
|
for _, alias in ipairs(self._aliases) do
|
||||||
if alias:sub(1, 1) == alias:sub(2, 2) then
|
if alias:sub(1, 1) == alias:sub(2, 2) then
|
||||||
self._target = alias:sub(3)
|
return alias:sub(3)
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self._target = self._target or self._aliases[1]:sub(2)
|
return self._name:sub(2)
|
||||||
self._name = self._name or self._aliases[1]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:argument(...)
|
function Parser:argument(...)
|
||||||
@@ -344,14 +336,7 @@ function Parser:prepare()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, elements in ipairs{self._arguments, self._options} do
|
|
||||||
for _, element in ipairs(elements) do
|
|
||||||
element:prepare()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, command in ipairs(self._commands) do
|
for _, command in ipairs(self._commands) do
|
||||||
command._target = command._target or command._name
|
|
||||||
command._fullname = self._fullname .. " " .. command._name
|
command._fullname = self._fullname .. " " .. command._name
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -378,7 +363,10 @@ local max_usage_width = 70
|
|||||||
local usage_welcome = "Usage: "
|
local usage_welcome = "Usage: "
|
||||||
|
|
||||||
function Parser:get_usage()
|
function Parser:get_usage()
|
||||||
if not self._usage then
|
if self._usage then
|
||||||
|
return self._usage
|
||||||
|
end
|
||||||
|
|
||||||
local lines = {usage_welcome .. self._fullname}
|
local lines = {usage_welcome .. self._fullname}
|
||||||
|
|
||||||
local function add(s)
|
local function add(s)
|
||||||
@@ -391,7 +379,7 @@ function Parser:get_usage()
|
|||||||
|
|
||||||
for _, elements in ipairs{self._options, self._arguments} do
|
for _, elements in ipairs{self._options, self._arguments} do
|
||||||
for _, element in ipairs(elements) do
|
for _, element in ipairs(elements) do
|
||||||
add(element:get_usage())
|
add(element:_get_usage())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -405,10 +393,7 @@ function Parser:get_usage()
|
|||||||
add("...")
|
add("...")
|
||||||
end
|
end
|
||||||
|
|
||||||
self._usage = table.concat(lines, "\r\n")
|
return table.concat(lines, "\r\n")
|
||||||
end
|
|
||||||
|
|
||||||
return self._usage
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local margin_len = 3
|
local margin_len = 3
|
||||||
@@ -453,7 +438,7 @@ local function make_name(option)
|
|||||||
local variant
|
local variant
|
||||||
|
|
||||||
for _, alias in ipairs(option._aliases) do
|
for _, alias in ipairs(option._aliases) do
|
||||||
variant = option:get_arg_usage("<" .. option._target .. ">")
|
variant = option:_get_arg_usage("<" .. option:_get_target() .. ">")
|
||||||
table.insert(variant, 1, alias)
|
table.insert(variant, 1, alias)
|
||||||
variant = table.concat(variant, " ")
|
variant = table.concat(variant, " ")
|
||||||
table.insert(variants, variant)
|
table.insert(variants, variant)
|
||||||
@@ -463,7 +448,10 @@ local function make_name(option)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Parser:get_help()
|
function Parser:get_help()
|
||||||
if not self._help then
|
if self._help then
|
||||||
|
return self._help
|
||||||
|
end
|
||||||
|
|
||||||
local blocks = {self:get_usage()}
|
local blocks = {self:get_usage()}
|
||||||
|
|
||||||
if self._description then
|
if self._description then
|
||||||
@@ -504,10 +492,7 @@ function Parser:get_help()
|
|||||||
table.insert(blocks, self._epilog)
|
table.insert(blocks, self._epilog)
|
||||||
end
|
end
|
||||||
|
|
||||||
self._help = table.concat(blocks, "\r\n\r\n")
|
return table.concat(blocks, "\r\n\r\n")
|
||||||
end
|
|
||||||
|
|
||||||
return self._help
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_tip(context, wrong_name)
|
local function get_tip(context, wrong_name)
|
||||||
@@ -583,6 +568,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
local cur_option
|
local cur_option
|
||||||
local cur_arg_i = 1
|
local cur_arg_i = 1
|
||||||
local cur_arg
|
local cur_arg
|
||||||
|
local targets = {}
|
||||||
|
|
||||||
local function error_(fmt, ...)
|
local function error_(fmt, ...)
|
||||||
return errhandler(parser, fmt:format(...))
|
return errhandler(parser, fmt:format(...))
|
||||||
@@ -625,24 +611,26 @@ function Parser:_parse(args, errhandler)
|
|||||||
end
|
end
|
||||||
|
|
||||||
passed[element] = 0
|
passed[element] = 0
|
||||||
|
local type_ = element:_get_type()
|
||||||
|
local target = targets[element]
|
||||||
|
|
||||||
if element.flag then
|
if type_ == "flag" then
|
||||||
result[element._target] = true
|
result[target] = true
|
||||||
elseif element.multiarg then
|
elseif type_ == "multiarg" then
|
||||||
result[element._target] = {}
|
result[target] = {}
|
||||||
elseif element.counter then
|
elseif type_ == "counter" then
|
||||||
if not overwrite then
|
if not overwrite then
|
||||||
result[element._target] = result[element._target]+1
|
result[target] = result[target]+1
|
||||||
end
|
end
|
||||||
elseif element.multicount then
|
elseif type_ == "multicount" then
|
||||||
if overwrite then
|
if overwrite then
|
||||||
table.remove(result[element._target], 1)
|
table.remove(result[target], 1)
|
||||||
end
|
end
|
||||||
elseif element.twodimensional then
|
elseif type_ == "twodimensional" then
|
||||||
table.insert(result[element._target], {})
|
table.insert(result[target], {})
|
||||||
|
|
||||||
if overwrite then
|
if overwrite then
|
||||||
table.remove(result[element._target], 1)
|
table.remove(result[target], 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -654,13 +642,15 @@ function Parser:_parse(args, errhandler)
|
|||||||
function pass(element, data)
|
function pass(element, data)
|
||||||
passed[element] = passed[element]+1
|
passed[element] = passed[element]+1
|
||||||
data = convert(element, data)
|
data = convert(element, data)
|
||||||
|
local type_ = element:_get_type()
|
||||||
|
local target = targets[element]
|
||||||
|
|
||||||
if element.arg then
|
if type_ == "arg" then
|
||||||
result[element._target] = data
|
result[target] = data
|
||||||
elseif element.multiarg or element.multicount then
|
elseif type_ == "multiarg" or type_ == "multicount" then
|
||||||
table.insert(result[element._target], data)
|
table.insert(result[target], data)
|
||||||
elseif element.twodimensional then
|
elseif type_ == "twodimensional" then
|
||||||
table.insert(result[element._target][#result[element._target]], data)
|
table.insert(result[target][#result[target]], data)
|
||||||
end
|
end
|
||||||
|
|
||||||
if passed[element] == element._maxargs then
|
if passed[element] == element._maxargs then
|
||||||
@@ -701,10 +691,13 @@ function Parser:_parse(args, errhandler)
|
|||||||
opt_context[alias] = option
|
opt_context[alias] = option
|
||||||
end
|
end
|
||||||
|
|
||||||
if option.counter then
|
local type_ = option:_get_type()
|
||||||
result[option._target] = 0
|
targets[option] = option:_get_target()
|
||||||
elseif option.multicount or option.twodimensional then
|
|
||||||
result[option._target] = {}
|
if type_ == "counter" then
|
||||||
|
result[targets[option]] = 0
|
||||||
|
elseif type_ == "multicount" or type_ == "twodimensional" then
|
||||||
|
result[targets[option]] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
invocations[option] = 0
|
invocations[option] = 0
|
||||||
@@ -713,6 +706,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
for _, argument in ipairs(parser._arguments) do
|
for _, argument in ipairs(parser._arguments) do
|
||||||
table.insert(arguments, argument)
|
table.insert(arguments, argument)
|
||||||
invocations[argument] = 0
|
invocations[argument] = 0
|
||||||
|
targets[argument] = argument._target or argument._name
|
||||||
invoke(argument)
|
invoke(argument)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -721,6 +715,8 @@ function Parser:_parse(args, errhandler)
|
|||||||
com_context = {}
|
com_context = {}
|
||||||
|
|
||||||
for _, command in ipairs(commands) do
|
for _, command in ipairs(commands) do
|
||||||
|
targets[command] = command._target or command._name
|
||||||
|
|
||||||
for _, alias in ipairs(command._aliases) do
|
for _, alias in ipairs(command._aliases) do
|
||||||
com_context[alias] = command
|
com_context[alias] = command
|
||||||
end
|
end
|
||||||
@@ -752,7 +748,7 @@ function Parser:_parse(args, errhandler)
|
|||||||
error_("too many arguments")
|
error_("too many arguments")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
result[com._target] = true
|
result[targets[com]] = true
|
||||||
do_action(com)
|
do_action(com)
|
||||||
switch(com)
|
switch(com)
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user