mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-29 11:32:19 +00:00
Minor refactoring
This commit is contained in:
124
src/argparse.lua
124
src/argparse.lua
@@ -1,33 +1,8 @@
|
|||||||
|
local Parser, Command, Argument, Option
|
||||||
|
|
||||||
|
do -- Create classes with setters
|
||||||
local class = require "30log"
|
local class = require "30log"
|
||||||
|
|
||||||
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 add_setters(cl, fields)
|
local function add_setters(cl, fields)
|
||||||
for field, setter in pairs(fields) do
|
for field, setter in pairs(fields) do
|
||||||
cl[field] = function(self, value)
|
cl[field] = function(self, value)
|
||||||
@@ -102,6 +77,34 @@ local function aliased_aliases(self, aliases)
|
|||||||
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)
|
local function boundaries(field)
|
||||||
return function(self, value)
|
return function(self, value)
|
||||||
local min, max = parse_boundaries(value)
|
local min, max = parse_boundaries(value)
|
||||||
@@ -122,7 +125,7 @@ local function convert(self, value)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local Parser = add_setters(class {
|
Parser = add_setters(class {
|
||||||
__name = "Parser",
|
__name = "Parser",
|
||||||
_arguments = {},
|
_arguments = {},
|
||||||
_options = {},
|
_options = {},
|
||||||
@@ -137,7 +140,7 @@ local Parser = add_setters(class {
|
|||||||
help = typecheck.string "help"
|
help = typecheck.string "help"
|
||||||
})
|
})
|
||||||
|
|
||||||
local Command = add_setters(Parser:extends {
|
Command = add_setters(Parser:extends {
|
||||||
__name = "Command",
|
__name = "Command",
|
||||||
_aliases = {}
|
_aliases = {}
|
||||||
}, {
|
}, {
|
||||||
@@ -152,7 +155,7 @@ local Command = add_setters(Parser:extends {
|
|||||||
help = typecheck.string "help"
|
help = typecheck.string "help"
|
||||||
})
|
})
|
||||||
|
|
||||||
local Argument = add_setters(class {
|
Argument = add_setters(class {
|
||||||
__name = "Argument",
|
__name = "Argument",
|
||||||
_minargs = 1,
|
_minargs = 1,
|
||||||
_maxargs = 1,
|
_maxargs = 1,
|
||||||
@@ -171,7 +174,7 @@ local Argument = add_setters(class {
|
|||||||
argname = typecheck.string "argname"
|
argname = typecheck.string "argname"
|
||||||
})
|
})
|
||||||
|
|
||||||
local Option = add_setters(Argument:extends {
|
Option = add_setters(Argument:extends {
|
||||||
__name = "Option",
|
__name = "Option",
|
||||||
_aliases = {},
|
_aliases = {},
|
||||||
_mincount = 0,
|
_mincount = 0,
|
||||||
@@ -191,6 +194,7 @@ local Option = add_setters(Argument:extends {
|
|||||||
usage = typecheck.string "usage",
|
usage = typecheck.string "usage",
|
||||||
argname = typecheck.string "argname"
|
argname = typecheck.string "argname"
|
||||||
})
|
})
|
||||||
|
end
|
||||||
|
|
||||||
function Argument:_get_arg_usage(argname)
|
function Argument:_get_arg_usage(argname)
|
||||||
argname = self._argname or argname
|
argname = self._argname or argname
|
||||||
@@ -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