mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Remove bundled 30log
This commit is contained in:
25
LICENSE
25
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2013 - 2014 Peter Melnichenko
|
Copyright (c) 2013 - 2015 Peter Melnichenko
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
@@ -18,26 +18,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|||||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
argparse includes 30log by Roland Yonaba.
|
|
||||||
|
|
||||||
Copyright (c) 2012 - 2014 Roland Yonaba
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
copy of this software and associated documentation files (the
|
|
||||||
"Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included
|
|
||||||
in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
140
src/argparse.lua
140
src/argparse.lua
@@ -1,114 +1,40 @@
|
|||||||
local Parser, Command, Argument, Option
|
local Parser, Command, Argument, Option
|
||||||
|
|
||||||
local function require_30log()
|
-- Create classes with setters
|
||||||
-------------------------------------------------------------------------------
|
do
|
||||||
local assert = assert
|
local function deep_update(t1, t2)
|
||||||
local pairs = pairs
|
for k, v in pairs(t2) do
|
||||||
local type = type
|
if type(v) == "table" then
|
||||||
local tostring = tostring
|
v = deep_update({}, v)
|
||||||
local setmetatable = setmetatable
|
|
||||||
|
|
||||||
local baseMt = {}
|
|
||||||
local _instances = setmetatable({},{__mode='k'})
|
|
||||||
local _classes = setmetatable({},{__mode='k'})
|
|
||||||
local class
|
|
||||||
|
|
||||||
local function deep_copy(t, dest, aType)
|
|
||||||
local t = t or {}
|
|
||||||
local r = dest or {}
|
|
||||||
|
|
||||||
for k,v in pairs(t) do
|
|
||||||
if aType and type(v)==aType then
|
|
||||||
r[k] = v
|
|
||||||
elseif not aType then
|
|
||||||
if type(v) == 'table' and k ~= "__index" then
|
|
||||||
r[k] = deep_copy(v)
|
|
||||||
else
|
|
||||||
r[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return r
|
t1[k] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
local function instantiate(self,...)
|
return t1
|
||||||
assert(_classes[self], 'new() should be called from a class.')
|
|
||||||
local instance = deep_copy(self)
|
|
||||||
_instances[instance] = tostring(instance)
|
|
||||||
setmetatable(instance,self)
|
|
||||||
|
|
||||||
if self.__init then
|
|
||||||
if type(self.__init) == 'table' then
|
|
||||||
deep_copy(self.__init, instance)
|
|
||||||
else
|
|
||||||
self.__init(instance, ...)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return instance
|
local class_metatable = {}
|
||||||
|
|
||||||
|
function class_metatable.__call(cls, ...)
|
||||||
|
return setmetatable(deep_update({}, cls.__proto), cls)(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function extends(self,extra_params)
|
function class_metatable.__index(cls, key)
|
||||||
local heir = {}
|
return cls.__parent and cls.__parent[key]
|
||||||
_classes[heir] = tostring(heir)
|
|
||||||
deep_copy(extra_params, deep_copy(self, heir))
|
|
||||||
heir.__index = heir
|
|
||||||
heir.super = self
|
|
||||||
return setmetatable(heir,self)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
baseMt = {
|
local function class(proto)
|
||||||
__call = function (self,...)
|
local cls = setmetatable({__proto = proto, __parent = {}}, class_metatable)
|
||||||
return self:new(...)
|
cls.__index = cls
|
||||||
end,
|
return cls
|
||||||
|
|
||||||
__tostring = function(self,...)
|
|
||||||
if _instances[self] then
|
|
||||||
return
|
|
||||||
('object(of %s):<%s>')
|
|
||||||
:format((rawget(getmetatable(self),'__name') or '?'), _instances[self])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return
|
local function extend(cls, proto)
|
||||||
_classes[self] and
|
local new_cls = class(deep_update(deep_update({}, cls.__proto), proto))
|
||||||
('class(%s):<%s>')
|
new_cls.__parent = cls
|
||||||
:format((rawget(self,'__name') or '?'),_classes[self]) or
|
return new_cls
|
||||||
self
|
|
||||||
end
|
end
|
||||||
}
|
|
||||||
|
|
||||||
class = function(attr)
|
|
||||||
local c = deep_copy(attr)
|
|
||||||
_classes[c] = tostring(c)
|
|
||||||
c.include = function(self,include)
|
|
||||||
assert(_classes[self], 'Mixins can only be used on classes.')
|
|
||||||
return deep_copy(include, self, 'function')
|
|
||||||
end
|
|
||||||
|
|
||||||
c.new = instantiate
|
|
||||||
c.extends = extends
|
|
||||||
c.__index = c
|
|
||||||
c.__call = baseMt.__call
|
|
||||||
c.__tostring = baseMt.__tostring
|
|
||||||
|
|
||||||
c.is = function(self, kind)
|
|
||||||
local super
|
|
||||||
while true do
|
|
||||||
super = getmetatable(super or self)
|
|
||||||
if super == kind or super == nil then break end
|
|
||||||
end
|
|
||||||
return kind and (super == kind)
|
|
||||||
end
|
|
||||||
return setmetatable(c, baseMt)
|
|
||||||
end
|
|
||||||
|
|
||||||
return class
|
|
||||||
-------------------------------------------------------------------------------
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- Create classes with setters
|
|
||||||
local class = require_30log()
|
|
||||||
|
|
||||||
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
|
||||||
@@ -119,10 +45,6 @@ do -- Create classes with setters
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cl.__init = function(self, ...)
|
|
||||||
return self(...)
|
|
||||||
end
|
|
||||||
|
|
||||||
cl.__call = function(self, ...)
|
cl.__call = function(self, ...)
|
||||||
local name_or_options
|
local name_or_options
|
||||||
|
|
||||||
@@ -260,7 +182,6 @@ do -- Create classes with setters
|
|||||||
end
|
end
|
||||||
|
|
||||||
Parser = add_setters(class {
|
Parser = add_setters(class {
|
||||||
__name = "Parser",
|
|
||||||
_arguments = {},
|
_arguments = {},
|
||||||
_options = {},
|
_options = {},
|
||||||
_commands = {},
|
_commands = {},
|
||||||
@@ -276,10 +197,9 @@ do -- Create classes with setters
|
|||||||
add_help = add_help
|
add_help = add_help
|
||||||
})
|
})
|
||||||
|
|
||||||
Command = add_setters(Parser:extends {
|
Command = add_setters(extend(Parser, {
|
||||||
__name = "Command",
|
|
||||||
_aliases = {}
|
_aliases = {}
|
||||||
}, {
|
}), {
|
||||||
name = aliased_name,
|
name = aliased_name,
|
||||||
aliases = aliased_aliases,
|
aliases = aliased_aliases,
|
||||||
description = typecheck.string "description",
|
description = typecheck.string "description",
|
||||||
@@ -293,7 +213,6 @@ do -- Create classes with setters
|
|||||||
})
|
})
|
||||||
|
|
||||||
Argument = add_setters(class {
|
Argument = add_setters(class {
|
||||||
__name = "Argument",
|
|
||||||
_minargs = 1,
|
_minargs = 1,
|
||||||
_maxargs = 1,
|
_maxargs = 1,
|
||||||
_mincount = 1,
|
_mincount = 1,
|
||||||
@@ -312,12 +231,11 @@ do -- Create classes with setters
|
|||||||
show_default = typecheck.boolean "show_default"
|
show_default = typecheck.boolean "show_default"
|
||||||
})
|
})
|
||||||
|
|
||||||
Option = add_setters(Argument:extends {
|
Option = add_setters(extend(Argument, {
|
||||||
__name = "Option",
|
|
||||||
_aliases = {},
|
_aliases = {},
|
||||||
_mincount = 0,
|
_mincount = 0,
|
||||||
_overwrite = true
|
_overwrite = true
|
||||||
}, {
|
}), {
|
||||||
name = aliased_name,
|
name = aliased_name,
|
||||||
aliases = aliased_aliases,
|
aliases = aliased_aliases,
|
||||||
description = typecheck.string "description",
|
description = typecheck.string "description",
|
||||||
@@ -513,13 +431,13 @@ function Parser:_update_charset(charset)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Parser:argument(...)
|
function Parser:argument(...)
|
||||||
local argument = Argument:new(...)
|
local argument = Argument(...)
|
||||||
table.insert(self._arguments, argument)
|
table.insert(self._arguments, argument)
|
||||||
return argument
|
return argument
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:option(...)
|
function Parser:option(...)
|
||||||
local option = Option:new(...)
|
local option = Option(...)
|
||||||
|
|
||||||
if self._has_help then
|
if self._has_help then
|
||||||
table.insert(self._options, #self._options, option)
|
table.insert(self._options, #self._options, option)
|
||||||
@@ -535,7 +453,7 @@ function Parser:flag(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Parser:command(...)
|
function Parser:command(...)
|
||||||
local command = Command:new():add_help(true)(...)
|
local command = Command():add_help(true)(...)
|
||||||
command._parent = self
|
command._parent = self
|
||||||
table.insert(self._commands, command)
|
table.insert(self._commands, command)
|
||||||
return command
|
return command
|
||||||
|
Reference in New Issue
Block a user