mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-30 03:52:19 +00:00
Minor refactoring
This commit is contained in:
392
src/argparse.lua
392
src/argparse.lua
@@ -1,197 +1,201 @@
|
|||||||
local class = require "30log"
|
local Parser, Command, Argument, Option
|
||||||
|
|
||||||
local function parse_boundaries(boundaries)
|
do -- Create classes with setters
|
||||||
if tonumber(boundaries) then
|
local class = require "30log"
|
||||||
return tonumber(boundaries), tonumber(boundaries)
|
|
||||||
end
|
|
||||||
|
|
||||||
if boundaries == "*" then
|
local function add_setters(cl, fields)
|
||||||
return 0, math.huge
|
for field, setter in pairs(fields) do
|
||||||
end
|
cl[field] = function(self, value)
|
||||||
|
if setter then
|
||||||
|
setter(self, value)
|
||||||
|
end
|
||||||
|
|
||||||
if boundaries == "+" then
|
self["_"..field] = value
|
||||||
return 1, math.huge
|
return self
|
||||||
end
|
|
||||||
|
|
||||||
if boundaries == "?" then
|
|
||||||
return 0, 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if boundaries:match "^%d+%-%d+$" then
|
|
||||||
local min, max = boundaries:match "^(%d+)%-(%d+)$"
|
|
||||||
return tonumber(min), tonumber(max)
|
|
||||||
end
|
|
||||||
|
|
||||||
if boundaries:match "^%d+%+$" then
|
|
||||||
local min = boundaries:match "^(%d+)%+$"
|
|
||||||
return tonumber(min), math.huge
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function add_setters(cl, fields)
|
|
||||||
for field, setter in pairs(fields) do
|
|
||||||
cl[field] = function(self, value)
|
|
||||||
if setter then
|
|
||||||
setter(self, value)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self["_"..field] = value
|
|
||||||
return self
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
cl.__init = function(self, ...)
|
cl.__init = function(self, ...)
|
||||||
return self(...)
|
return self(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
cl.__call = function(self, ...)
|
cl.__call = function(self, ...)
|
||||||
local name_or_options
|
local name_or_options
|
||||||
|
|
||||||
for i=1, select("#", ...) do
|
for i=1, select("#", ...) do
|
||||||
name_or_options = select(i, ...)
|
name_or_options = select(i, ...)
|
||||||
|
|
||||||
if type(name_or_options) == "string" then
|
if type(name_or_options) == "string" then
|
||||||
if self._aliases then
|
if self._aliases then
|
||||||
table.insert(self._aliases, name_or_options)
|
table.insert(self._aliases, name_or_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not self._name then
|
if not self._name then
|
||||||
self._name = name_or_options
|
self._name = name_or_options
|
||||||
end
|
end
|
||||||
elseif type(name_or_options) == "table" then
|
elseif type(name_or_options) == "table" then
|
||||||
for field, setter in pairs(fields) do
|
for field, setter in pairs(fields) do
|
||||||
if name_or_options[field] ~= nil then
|
if name_or_options[field] ~= nil then
|
||||||
self[field](self, name_or_options[field])
|
self[field](self, name_or_options[field])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
return cl
|
||||||
end
|
end
|
||||||
|
|
||||||
return cl
|
local typecheck = setmetatable({}, {
|
||||||
end
|
__index = function(self, type_)
|
||||||
|
local typechecker_factory = function(field)
|
||||||
local typecheck = setmetatable({}, {
|
return function(_, value)
|
||||||
__index = function(self, type_)
|
if type(value) ~= type_ then
|
||||||
local typechecker_factory = function(field)
|
error(("bad field '%s' (%s expected, got %s)"):format(field, type_, type(value)))
|
||||||
return function(_, value)
|
end
|
||||||
if type(value) ~= type_ then
|
|
||||||
error(("bad field '%s' (%s expected, got %s)"):format(field, type_, type(value)))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self[type_] = typechecker_factory
|
||||||
|
return typechecker_factory
|
||||||
end
|
end
|
||||||
|
})
|
||||||
|
|
||||||
self[type_] = typechecker_factory
|
local function aliased_name(self, name)
|
||||||
return typechecker_factory
|
typecheck.string "name" (self, name)
|
||||||
|
|
||||||
|
table.insert(self._aliases, name)
|
||||||
end
|
end
|
||||||
})
|
|
||||||
|
|
||||||
local function aliased_name(self, name)
|
local function aliased_aliases(self, aliases)
|
||||||
typecheck.string "name" (self, name)
|
typecheck.table "aliases" (self, aliases)
|
||||||
|
|
||||||
table.insert(self._aliases, name)
|
if not self._name then
|
||||||
end
|
self._name = aliases[1]
|
||||||
|
|
||||||
local function aliased_aliases(self, aliases)
|
|
||||||
typecheck.table "aliases" (self, aliases)
|
|
||||||
|
|
||||||
if not self._name then
|
|
||||||
self._name = aliases[1]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function boundaries(field)
|
|
||||||
return function(self, value)
|
|
||||||
local min, max = parse_boundaries(value)
|
|
||||||
|
|
||||||
if not min then
|
|
||||||
error(("bad field '%s'"):format(field))
|
|
||||||
end
|
|
||||||
|
|
||||||
self["_min"..field], self["_max"..field] = min, max
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function convert(self, value)
|
|
||||||
if type(value) ~= "function" then
|
|
||||||
if type(value) ~= "table" then
|
|
||||||
error(("bad field 'convert' (function or table expected, got %s)"):format(type(value)))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function parse_boundaries(boundaries)
|
||||||
|
if tonumber(boundaries) then
|
||||||
|
return tonumber(boundaries), tonumber(boundaries)
|
||||||
|
end
|
||||||
|
|
||||||
|
if boundaries == "*" then
|
||||||
|
return 0, math.huge
|
||||||
|
end
|
||||||
|
|
||||||
|
if boundaries == "+" then
|
||||||
|
return 1, math.huge
|
||||||
|
end
|
||||||
|
|
||||||
|
if boundaries == "?" then
|
||||||
|
return 0, 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if boundaries:match "^%d+%-%d+$" then
|
||||||
|
local min, max = boundaries:match "^(%d+)%-(%d+)$"
|
||||||
|
return tonumber(min), tonumber(max)
|
||||||
|
end
|
||||||
|
|
||||||
|
if boundaries:match "^%d+%+$" then
|
||||||
|
local min = boundaries:match "^(%d+)%+$"
|
||||||
|
return tonumber(min), math.huge
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function boundaries(field)
|
||||||
|
return function(self, value)
|
||||||
|
local min, max = parse_boundaries(value)
|
||||||
|
|
||||||
|
if not min then
|
||||||
|
error(("bad field '%s'"):format(field))
|
||||||
|
end
|
||||||
|
|
||||||
|
self["_min"..field], self["_max"..field] = min, max
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function convert(self, value)
|
||||||
|
if type(value) ~= "function" then
|
||||||
|
if type(value) ~= "table" then
|
||||||
|
error(("bad field 'convert' (function or table expected, got %s)"):format(type(value)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Parser = add_setters(class {
|
||||||
|
__name = "Parser",
|
||||||
|
_arguments = {},
|
||||||
|
_options = {},
|
||||||
|
_commands = {},
|
||||||
|
_require_command = true
|
||||||
|
}, {
|
||||||
|
name = typecheck.string "name",
|
||||||
|
description = typecheck.string "description",
|
||||||
|
epilog = typecheck.string "epilog",
|
||||||
|
require_command = typecheck.boolean "require_command",
|
||||||
|
usage = typecheck.string "usage",
|
||||||
|
help = typecheck.string "help"
|
||||||
|
})
|
||||||
|
|
||||||
|
Command = add_setters(Parser:extends {
|
||||||
|
__name = "Command",
|
||||||
|
_aliases = {}
|
||||||
|
}, {
|
||||||
|
name = aliased_name,
|
||||||
|
aliases = aliased_aliases,
|
||||||
|
description = typecheck.string "description",
|
||||||
|
epilog = typecheck.string "epilog",
|
||||||
|
target = typecheck.string "target",
|
||||||
|
require_command = typecheck.boolean "require_command",
|
||||||
|
action = typecheck["function"] "action",
|
||||||
|
usage = typecheck.string "usage",
|
||||||
|
help = typecheck.string "help"
|
||||||
|
})
|
||||||
|
|
||||||
|
Argument = add_setters(class {
|
||||||
|
__name = "Argument",
|
||||||
|
_minargs = 1,
|
||||||
|
_maxargs = 1,
|
||||||
|
_mincount = 1,
|
||||||
|
_maxcount = 1,
|
||||||
|
_defmode = "unused"
|
||||||
|
}, {
|
||||||
|
name = typecheck.string "name",
|
||||||
|
description = typecheck.string "description",
|
||||||
|
target = typecheck.string "target",
|
||||||
|
args = boundaries "args",
|
||||||
|
default = typecheck.string "default",
|
||||||
|
defmode = typecheck.string "defmode",
|
||||||
|
convert = convert,
|
||||||
|
usage = typecheck.string "usage",
|
||||||
|
argname = typecheck.string "argname"
|
||||||
|
})
|
||||||
|
|
||||||
|
Option = add_setters(Argument:extends {
|
||||||
|
__name = "Option",
|
||||||
|
_aliases = {},
|
||||||
|
_mincount = 0,
|
||||||
|
_overwrite = true
|
||||||
|
}, {
|
||||||
|
name = aliased_name,
|
||||||
|
aliases = aliased_aliases,
|
||||||
|
description = typecheck.string "description",
|
||||||
|
target = typecheck.string "target",
|
||||||
|
args = boundaries "args",
|
||||||
|
count = boundaries "count",
|
||||||
|
default = typecheck.string "default",
|
||||||
|
defmode = typecheck.string "defmode",
|
||||||
|
convert = convert,
|
||||||
|
overwrite = typecheck.boolean "overwrite",
|
||||||
|
action = typecheck["function"] "action",
|
||||||
|
usage = typecheck.string "usage",
|
||||||
|
argname = typecheck.string "argname"
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
local Parser = add_setters(class {
|
|
||||||
__name = "Parser",
|
|
||||||
_arguments = {},
|
|
||||||
_options = {},
|
|
||||||
_commands = {},
|
|
||||||
_require_command = true
|
|
||||||
}, {
|
|
||||||
name = typecheck.string "name",
|
|
||||||
description = typecheck.string "description",
|
|
||||||
epilog = typecheck.string "epilog",
|
|
||||||
require_command = typecheck.boolean "require_command",
|
|
||||||
usage = typecheck.string "usage",
|
|
||||||
help = typecheck.string "help"
|
|
||||||
})
|
|
||||||
|
|
||||||
local Command = add_setters(Parser:extends {
|
|
||||||
__name = "Command",
|
|
||||||
_aliases = {}
|
|
||||||
}, {
|
|
||||||
name = aliased_name,
|
|
||||||
aliases = aliased_aliases,
|
|
||||||
description = typecheck.string "description",
|
|
||||||
epilog = typecheck.string "epilog",
|
|
||||||
target = typecheck.string "target",
|
|
||||||
require_command = typecheck.boolean "require_command",
|
|
||||||
action = typecheck["function"] "action",
|
|
||||||
usage = typecheck.string "usage",
|
|
||||||
help = typecheck.string "help"
|
|
||||||
})
|
|
||||||
|
|
||||||
local Argument = add_setters(class {
|
|
||||||
__name = "Argument",
|
|
||||||
_minargs = 1,
|
|
||||||
_maxargs = 1,
|
|
||||||
_mincount = 1,
|
|
||||||
_maxcount = 1,
|
|
||||||
_defmode = "unused"
|
|
||||||
}, {
|
|
||||||
name = typecheck.string "name",
|
|
||||||
description = typecheck.string "description",
|
|
||||||
target = typecheck.string "target",
|
|
||||||
args = boundaries "args",
|
|
||||||
default = typecheck.string "default",
|
|
||||||
defmode = typecheck.string "defmode",
|
|
||||||
convert = convert,
|
|
||||||
usage = typecheck.string "usage",
|
|
||||||
argname = typecheck.string "argname"
|
|
||||||
})
|
|
||||||
|
|
||||||
local Option = add_setters(Argument:extends {
|
|
||||||
__name = "Option",
|
|
||||||
_aliases = {},
|
|
||||||
_mincount = 0,
|
|
||||||
_overwrite = true
|
|
||||||
}, {
|
|
||||||
name = aliased_name,
|
|
||||||
aliases = aliased_aliases,
|
|
||||||
description = typecheck.string "description",
|
|
||||||
target = typecheck.string "target",
|
|
||||||
args = boundaries "args",
|
|
||||||
count = boundaries "count",
|
|
||||||
default = typecheck.string "default",
|
|
||||||
defmode = typecheck.string "defmode",
|
|
||||||
convert = convert,
|
|
||||||
overwrite = typecheck.boolean "overwrite",
|
|
||||||
action = typecheck["function"] "action",
|
|
||||||
usage = typecheck.string "usage",
|
|
||||||
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 = {}
|
||||||
@@ -290,6 +294,34 @@ function Option:_get_target()
|
|||||||
return self._name:sub(2)
|
return self._name:sub(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Parser:_get_fullname()
|
||||||
|
local parent = self._parent
|
||||||
|
local buf = {self._name}
|
||||||
|
|
||||||
|
while parent do
|
||||||
|
table.insert(buf, 1, parent._name)
|
||||||
|
parent = parent._parent
|
||||||
|
end
|
||||||
|
|
||||||
|
return table.concat(buf, " ")
|
||||||
|
end
|
||||||
|
|
||||||
|
function Parser:_update_charset(charset)
|
||||||
|
charset = charset or {}
|
||||||
|
|
||||||
|
for _, command in ipairs(self._commands) do
|
||||||
|
command:_update_charset(charset)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, option in ipairs(self._options) do
|
||||||
|
for _, alias in ipairs(option._aliases) do
|
||||||
|
charset[alias:sub(1, 1)] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return charset
|
||||||
|
end
|
||||||
|
|
||||||
function Parser:argument(...)
|
function Parser:argument(...)
|
||||||
local argument = Argument:new(...)
|
local argument = Argument:new(...)
|
||||||
table.insert(self._arguments, argument)
|
table.insert(self._arguments, argument)
|
||||||
@@ -344,34 +376,6 @@ function Parser:add_help(param)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:_get_fullname()
|
|
||||||
local parent = self._parent
|
|
||||||
local buf = {self._name}
|
|
||||||
|
|
||||||
while parent do
|
|
||||||
table.insert(buf, 1, parent._name)
|
|
||||||
parent = parent._parent
|
|
||||||
end
|
|
||||||
|
|
||||||
return table.concat(buf, " ")
|
|
||||||
end
|
|
||||||
|
|
||||||
function Parser:_update_charset(charset)
|
|
||||||
charset = charset or {}
|
|
||||||
|
|
||||||
for _, command in ipairs(self._commands) do
|
|
||||||
command:_update_charset(charset)
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, option in ipairs(self._options) do
|
|
||||||
for _, alias in ipairs(option._aliases) do
|
|
||||||
charset[alias:sub(1, 1)] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return charset
|
|
||||||
end
|
|
||||||
|
|
||||||
local max_usage_width = 70
|
local max_usage_width = 70
|
||||||
local usage_welcome = "Usage: "
|
local usage_welcome = "Usage: "
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user