Minor refactoring

This commit is contained in:
mpeterv
2014-03-02 02:10:46 +04:00
parent f24cfe9627
commit 9441804c3f

View File

@@ -1,33 +1,8 @@
local Parser, Command, Argument, Option
do -- Create classes with setters
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)
for field, setter in pairs(fields) do
cl[field] = function(self, value)
@@ -102,6 +77,34 @@ local function aliased_aliases(self, aliases)
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)
@@ -122,7 +125,7 @@ local function convert(self, value)
end
end
local Parser = add_setters(class {
Parser = add_setters(class {
__name = "Parser",
_arguments = {},
_options = {},
@@ -137,7 +140,7 @@ local Parser = add_setters(class {
help = typecheck.string "help"
})
local Command = add_setters(Parser:extends {
Command = add_setters(Parser:extends {
__name = "Command",
_aliases = {}
}, {
@@ -152,7 +155,7 @@ local Command = add_setters(Parser:extends {
help = typecheck.string "help"
})
local Argument = add_setters(class {
Argument = add_setters(class {
__name = "Argument",
_minargs = 1,
_maxargs = 1,
@@ -171,7 +174,7 @@ local Argument = add_setters(class {
argname = typecheck.string "argname"
})
local Option = add_setters(Argument:extends {
Option = add_setters(Argument:extends {
__name = "Option",
_aliases = {},
_mincount = 0,
@@ -191,6 +194,7 @@ local Option = add_setters(Argument:extends {
usage = typecheck.string "usage",
argname = typecheck.string "argname"
})
end
function Argument:_get_arg_usage(argname)
argname = self._argname or argname
@@ -290,6 +294,34 @@ function Option:_get_target()
return self._name:sub(2)
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(...)
local argument = Argument:new(...)
table.insert(self._arguments, argument)
@@ -344,34 +376,6 @@ function Parser:add_help(param)
return self
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 usage_welcome = "Usage: "