Allow using multiple constructor arguments for configuring elements

Disable undocumented ability to specify aliases as arguments
for constructors, e.g. parser:option("-f", "--foo"), and instead
order properties and pass constructor arguments to them.
E.g. parser:argument("foo", "A foo that bars") sets argument
name to foo and description to "A foo that bars".

TODO: remove "aliases" property, instead allow setting several
names in one string by separating them using space.
TODO: reorder properties so that most useful ones could be used
as constructor arguments.
This commit is contained in:
mpeterv
2015-06-09 21:54:18 +03:00
parent 476ad19de8
commit ff9abac990
5 changed files with 303 additions and 305 deletions

View File

@@ -34,30 +34,30 @@ describe("tests related to positional arguments", function()
it("handles multi-argument correctly", function() it("handles multi-argument correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo", { parser:argument "foo" {
args = "*" args = "*"
}) }
local args = parser:parse({"bar", "baz", "qu"}) local args = parser:parse({"bar", "baz", "qu"})
assert.same({foo = {"bar", "baz", "qu"}}, args) assert.same({foo = {"bar", "baz", "qu"}}, args)
end) end)
it("handles restrained multi-argument correctly", function() it("handles restrained multi-argument correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo", { parser:argument "foo" {
args = "2-4" args = "2-4"
}) }
local args = parser:parse({"bar", "baz"}) local args = parser:parse({"bar", "baz"})
assert.same({foo = {"bar", "baz"}}, args) assert.same({foo = {"bar", "baz"}}, args)
end) end)
it("handles several multi-arguments correctly", function() it("handles several multi-arguments correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo1", { parser:argument "foo1" {
args = "1-2" args = "1-2"
}) }
parser:argument("foo2", { parser:argument "foo2" {
args = "*" args = "*"
}) }
local args = parser:parse({"bar"}) local args = parser:parse({"bar"})
assert.same({foo1 = {"bar"}, foo2 = {}}, args) assert.same({foo1 = {"bar"}, foo2 = {}}, args)
args = parser:parse({"bar", "baz", "qu"}) args = parser:parse({"bar", "baz", "qu"})
@@ -134,31 +134,31 @@ describe("tests related to positional arguments", function()
it("handles too few arguments with multi-argument correctly", function() it("handles too few arguments with multi-argument correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo", { parser:argument "foo" {
args = "2-4" args = "2-4"
}) }
assert.has_error(function() parser:parse{"foo"} end, "too few arguments") assert.has_error(function() parser:parse{"foo"} end, "too few arguments")
end) end)
it("handles too many arguments with several multi-arguments correctly", function() it("handles too many arguments with several multi-arguments correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo1", { parser:argument "foo1" {
args = "1-2" args = "1-2"
}) }
parser:argument("foo2", { parser:argument "foo2" {
args = "0-1" args = "0-1"
}) }
assert.has_error(function() parser:parse{"foo", "bar", "baz", "qu"} end, "too many arguments") assert.has_error(function() parser:parse{"foo", "bar", "baz", "qu"} end, "too many arguments")
end) end)
it("handles too few arguments with several multi-arguments correctly", function() it("handles too few arguments with several multi-arguments correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo1", { parser:argument "foo1" {
args = "1-2" args = "1-2"
}) }
parser:argument("foo2", { parser:argument "foo2" {
args = "*" args = "*"
}) }
assert.has_error(function() parser:parse{} end, "too few arguments") assert.has_error(function() parser:parse{} end, "too few arguments")
end) end)
end) end)

View File

@@ -15,31 +15,31 @@ describe("tests related to default values", function()
it("handles default argument for multi-argument correctly", function() it("handles default argument for multi-argument correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo", { parser:argument "foo" {
args = 3, args = 3,
default = "bar", default = "bar",
defmode = "arg" defmode = "arg"
}) }
local args = parser:parse{"baz"} local args = parser:parse{"baz"}
assert.same({foo = {"baz", "bar", "bar"}}, args) assert.same({foo = {"baz", "bar", "bar"}}, args)
end) end)
it("handles default value for multi-argument correctly", function() it("handles default value for multi-argument correctly", function()
local parser = Parser() local parser = Parser()
parser:argument("foo", { parser:argument "foo" {
args = 3, args = 3,
default = "bar" default = "bar"
}) }
local args = parser:parse{} local args = parser:parse{}
assert.same({foo = {"bar", "bar", "bar"}}, args) assert.same({foo = {"bar", "bar", "bar"}}, args)
end) end)
it("does not use default values if not needed", function() it("does not use default values if not needed", function()
local parser = Parser() local parser = Parser()
parser:argument("foo", { parser:argument "foo" {
args = "1-2", args = "1-2",
default = "bar" default = "bar"
}) }
local args = parser:parse({"baz"}) local args = parser:parse({"baz"})
assert.same({foo = {"baz"}}, args) assert.same({foo = {"baz"}}, args)
end) end)
@@ -60,20 +60,20 @@ describe("tests related to default values", function()
it("handles option with default value for multi-argument option correctly", function() it("handles option with default value for multi-argument option correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--several", { parser:option "-s" "--several" {
default = "foo", default = "foo",
args = "2-3" args = "2-3"
}) }
local args = parser:parse{} local args = parser:parse{}
assert.same({several = {"foo", "foo"}}, args) assert.same({several = {"foo", "foo"}}, args)
end) end)
it("handles option with default value and argument", function() it("handles option with default value and argument", function()
local parser = Parser() local parser = Parser()
parser:option("-o", "--output", { parser:option "-o" "--output" {
default = "a.out", default = "a.out",
defmode = "arg+count" defmode = "arg+count"
}) }
local args = parser:parse{} local args = parser:parse{}
assert.same({output = "a.out"}, args) assert.same({output = "a.out"}, args)
args = parser:parse{"-o"} args = parser:parse{"-o"}
@@ -94,43 +94,43 @@ describe("tests related to default values", function()
it("doesn't use default argument if option is not invoked", function() it("doesn't use default argument if option is not invoked", function()
local parser = Parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option "-f" "--foo" {
default = "bar", default = "bar",
defmode = "arg" defmode = "arg"
}) }
local args = parser:parse{} local args = parser:parse{}
assert.same({}, args) assert.same({}, args)
end) end)
it("handles default multi-argument correctly", function() it("handles default multi-argument correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option "-f" "--foo" {
args = 3, args = 3,
default = "bar", default = "bar",
defmode = "arg" defmode = "arg"
}) }
local args = parser:parse({"--foo=baz"}) local args = parser:parse({"--foo=baz"})
assert.same({foo = {"baz", "bar", "bar"}}, args) assert.same({foo = {"baz", "bar", "bar"}}, args)
end) end)
it("does not use default values if not needed", function() it("does not use default values if not needed", function()
local parser = Parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option "-f" "--foo" {
args = "1-2", args = "1-2",
default = "bar", default = "bar",
defmode = "arg" defmode = "arg"
}) }
local args = parser:parse({"-f", "baz"}) local args = parser:parse({"-f", "baz"})
assert.same({foo = {"baz"}}, args) assert.same({foo = {"baz"}}, args)
end) end)
it("handles multi-count options with default value correctly", function() it("handles multi-count options with default value correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option "-f" "--foo" {
count = "*", count = "*",
default = "bar", default = "bar",
defmode = "arg + count" defmode = "arg + count"
}) }
local args = parser:parse({"-f", "--foo=baz", "--foo"}) local args = parser:parse({"-f", "--foo=baz", "--foo"})
assert.same({foo = {"bar", "baz", "bar"}}, args) assert.same({foo = {"bar", "baz", "bar"}}, args)
end) end)

View File

@@ -53,8 +53,7 @@ An epilog. ]], parser:get_help())
:args "2" :args "2"
parser:argument "maybe-fourth" parser:argument "maybe-fourth"
:args "?" :args "?"
parser:argument "others" parser:argument("others", "Optional. ")
:description "Optional. "
:args "*" :args "*"
assert.equal([[ assert.equal([[

View File

@@ -5,21 +5,21 @@ describe("tests related to options", function()
describe("passing correct options", function() describe("passing correct options", function()
it("handles no options passed correctly", function() it("handles no options passed correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
local args = parser:parse({}) local args = parser:parse({})
assert.same({}, args) assert.same({}, args)
end) end)
it("handles one option correctly", function() it("handles one option correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
local args = parser:parse({"--server", "foo"}) local args = parser:parse({"--server", "foo"})
assert.same({server = "foo"}, args) assert.same({server = "foo"}, args)
end) end)
it("normalizes default target", function() it("normalizes default target", function()
local parser = Parser() local parser = Parser()
parser:option("--from-server") parser:option "--from-server"
local args = parser:parse({"--from-server", "foo"}) local args = parser:parse({"--from-server", "foo"})
assert.same({from_server = "foo"}, args) assert.same({from_server = "foo"}, args)
end) end)
@@ -34,39 +34,39 @@ describe("tests related to options", function()
it("handles GNU-style long options", function() it("handles GNU-style long options", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
local args = parser:parse({"--server=foo"}) local args = parser:parse({"--server=foo"})
assert.same({server = "foo"}, args) assert.same({server = "foo"}, args)
end) end)
it("handles GNU-style long options even when it could take more arguments", function() it("handles GNU-style long options even when it could take more arguments", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server", { parser:option "-s" "--server" {
args = "*" args = "*"
}) }
local args = parser:parse({"--server=foo"}) local args = parser:parse({"--server=foo"})
assert.same({server = {"foo"}}, args) assert.same({server = {"foo"}}, args)
end) end)
it("handles GNU-style long options for multi-argument options", function() it("handles GNU-style long options for multi-argument options", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server", { parser:option "-s" "--server" {
args = "1-2" args = "1-2"
}) }
local args = parser:parse({"--server=foo", "bar"}) local args = parser:parse({"--server=foo", "bar"})
assert.same({server = {"foo", "bar"}}, args) assert.same({server = {"foo", "bar"}}, args)
end) end)
it("handles short option correclty", function() it("handles short option correclty", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
local args = parser:parse({"-s", "foo"}) local args = parser:parse({"-s", "foo"})
assert.same({server = "foo"}, args) assert.same({server = "foo"}, args)
end) end)
it("handles flag correclty", function() it("handles flag correclty", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet") parser:flag "-q" "--quiet"
local args = parser:parse({"--quiet"}) local args = parser:parse({"--quiet"})
assert.same({quiet = true}, args) assert.same({quiet = true}, args)
args = parser:parse({}) args = parser:parse({})
@@ -75,23 +75,23 @@ describe("tests related to options", function()
it("handles combined flags correclty", function() it("handles combined flags correclty", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet") parser:flag "-q" "--quiet"
parser:flag("-f", "--fast") parser:flag "-f" "--fast"
local args = parser:parse({"-qf"}) local args = parser:parse({"-qf"})
assert.same({quiet = true, fast = true}, args) assert.same({quiet = true, fast = true}, args)
end) end)
it("handles short options without space between option and argument", function() it("handles short options without space between option and argument", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
local args = parser:parse({"-sfoo"}) local args = parser:parse({"-sfoo"})
assert.same({server = "foo"}, args) assert.same({server = "foo"}, args)
end) end)
it("handles flags combined with short option correclty", function() it("handles flags combined with short option correclty", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet") parser:flag "-q" "--quiet"
parser:option("-s", "--server") parser:option "-s" "--server"
local args = parser:parse({"-qsfoo"}) local args = parser:parse({"-qsfoo"})
assert.same({quiet = true, server = "foo"}, args) assert.same({quiet = true, server = "foo"}, args)
end) end)
@@ -152,27 +152,27 @@ describe("tests related to options", function()
describe("Options with optional argument", function() describe("Options with optional argument", function()
it("handles emptiness correctly", function() it("handles emptiness correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-p", "--password", { parser:option "-p" "--password" {
args = "?" args = "?"
}) }
local args = parser:parse({}) local args = parser:parse({})
assert.same({}, args) assert.same({}, args)
end) end)
it("handles option without argument correctly", function() it("handles option without argument correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-p", "--password", { parser:option "-p" "--password" {
args = "?" args = "?"
}) }
local args = parser:parse({"-p"}) local args = parser:parse({"-p"})
assert.same({password = {}}, args) assert.same({password = {}}, args)
end) end)
it("handles option with argument correctly", function() it("handles option with argument correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-p", "--password", { parser:option "-p" "--password" {
args = "?" args = "?"
}) }
local args = parser:parse({"-p", "password"}) local args = parser:parse({"-p", "password"})
assert.same({password = {"password"}}, args) assert.same({password = {"password"}}, args)
end) end)
@@ -180,9 +180,9 @@ describe("tests related to options", function()
it("handles multi-argument options correctly", function() it("handles multi-argument options correctly", function()
local parser = Parser() local parser = Parser()
parser:option("--pair", { parser:option "--pair" {
args = 2 args = 2
}) }
local args = parser:parse({"--pair", "Alice", "Bob"}) local args = parser:parse({"--pair", "Alice", "Bob"})
assert.same({pair = {"Alice", "Bob"}}, args) assert.same({pair = {"Alice", "Bob"}}, args)
end) end)
@@ -190,55 +190,55 @@ describe("tests related to options", function()
describe("Multi-count options", function() describe("Multi-count options", function()
it("handles multi-count option correctly", function() it("handles multi-count option correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-e", "--exclude", { parser:option "-e" "--exclude" {
count = "*" count = "*"
}) }
local args = parser:parse({"-efoo", "--exclude=bar", "-e", "baz"}) local args = parser:parse({"-efoo", "--exclude=bar", "-e", "baz"})
assert.same({exclude = {"foo", "bar", "baz"}}, args) assert.same({exclude = {"foo", "bar", "baz"}}, args)
end) end)
it("handles not used multi-count option correctly", function() it("handles not used multi-count option correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-e", "--exclude", { parser:option "-e" "--exclude" {
count = "*" count = "*"
}) }
local args = parser:parse({}) local args = parser:parse({})
assert.same({exclude = {}}, args) assert.same({exclude = {}}, args)
end) end)
it("handles multi-count multi-argument option correctly", function() it("handles multi-count multi-argument option correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-e", "--exclude", { parser:option "-e" "--exclude" {
count = "*", count = "*",
args = 2 args = 2
}) }
local args = parser:parse({"-e", "Alice", "Bob", "-e", "Emma", "Jacob"}) local args = parser:parse({"-e", "Alice", "Bob", "-e", "Emma", "Jacob"})
assert.same({exclude = {{"Alice", "Bob"}, {"Emma", "Jacob"}}}, args) assert.same({exclude = {{"Alice", "Bob"}, {"Emma", "Jacob"}}}, args)
end) end)
it("handles multi-count flag correctly", function() it("handles multi-count flag correctly", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet", { parser:flag "-q" "--quiet" {
count = "*" count = "*"
}) }
local args = parser:parse({"-qq", "--quiet"}) local args = parser:parse({"-qq", "--quiet"})
assert.same({quiet = 3}, args) assert.same({quiet = 3}, args)
end) end)
it("overwrites old invocations", function() it("overwrites old invocations", function()
local parser = Parser() local parser = Parser()
parser:option("-u", "--user", { parser:option "-u" "--user" {
count = "0-2" count = "0-2"
}) }
local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"}) local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"})
assert.same({user = {"Bob", "John"}}, args) assert.same({user = {"Bob", "John"}}, args)
end) end)
it("handles not used multi-count flag correctly", function() it("handles not used multi-count flag correctly", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet", { parser:flag "-q" "--quiet" {
count = "*" count = "*"
}) }
local args = parser:parse({}) local args = parser:parse({})
assert.same({quiet = 0}, args) assert.same({quiet = 0}, args)
end) end)
@@ -248,7 +248,7 @@ describe("tests related to options", function()
describe("passing incorrect options", function() describe("passing incorrect options", function()
it("handles lack of required argument correctly", function() it("handles lack of required argument correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
assert.has_error(function() parser:parse{"--server"} end, "too few arguments") assert.has_error(function() parser:parse{"--server"} end, "too few arguments")
end) end)
@@ -264,30 +264,30 @@ describe("tests related to options", function()
it("handles too many arguments correctly", function() it("handles too many arguments correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-s", "--server") parser:option "-s" "--server"
assert.has_error(function() parser:parse{"-sfoo", "bar"} end, "too many arguments") assert.has_error(function() parser:parse{"-sfoo", "bar"} end, "too many arguments")
end) end)
it("doesn't accept GNU-like long options when it doesn't need arguments", function() it("doesn't accept GNU-like long options when it doesn't need arguments", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet") parser:flag "-q" "--quiet"
assert.has_error(function() parser:parse{"--quiet=very_quiet"} end, "option '--quiet' does not take arguments") assert.has_error(function() parser:parse{"--quiet=very_quiet"} end, "option '--quiet' does not take arguments")
end) end)
it("handles too many invocations correctly", function() it("handles too many invocations correctly", function()
local parser = Parser() local parser = Parser()
parser:flag("-q", "--quiet", { parser:flag "-q" "--quiet" {
count = 1, count = 1,
overwrite = false overwrite = false
}) }
assert.has_error(function() parser:parse{"-qq"} end, "option '-q' must be used at most 1 time") assert.has_error(function() parser:parse{"-qq"} end, "option '-q' must be used at most 1 time")
end) end)
it("handles too few invocations correctly", function() it("handles too few invocations correctly", function()
local parser = Parser() local parser = Parser()
parser:option("-f", "--foo", { parser:option "-f" "--foo" {
count = "3-4" count = "3-4"
}) }
assert.has_error(function() parser:parse{"-fFOO", "-fBAR"} end, "option '-f' must be used at least 3 times") assert.has_error(function() parser:parse{"-fFOO", "-fBAR"} end, "option '-f' must be used at least 3 times")
end) end)
end) end)

View File

@@ -1,257 +1,256 @@
local Parser, Command, Argument, Option local function deep_update(t1, t2)
for k, v in pairs(t2) do
-- Create classes with setters if type(v) == "table" then
do v = deep_update({}, v)
local function deep_update(t1, t2)
for k, v in pairs(t2) do
if type(v) == "table" then
v = deep_update({}, v)
end
t1[k] = v
end end
return t1 t1[k] = v
end end
local class_metatable = {} return t1
end
function class_metatable.__call(cls, ...) -- A property is a tuple {name, callback}.
return setmetatable(deep_update({}, cls.__proto), cls)(...) local function new_class(prototype, properties, parent)
-- Class is the metatable of its instances.
local class = {}
class.__index = class
if parent then
class.__prototype = deep_update(deep_update({}, parent.__prototype), prototype)
else
class.__prototype = prototype
end end
function class_metatable.__index(cls, key) local names = {}
return cls.__parent and cls.__parent[key]
end
local function class(proto) -- Create setter methods and fill set of property names.
local cls = setmetatable({__proto = proto, __parent = {}}, class_metatable) for _, property in ipairs(properties) do
cls.__index = cls local name, callback = property[1], property[2]
return cls
end
local function extend(cls, proto) class[name] = function(self, value)
local new_cls = class(deep_update(deep_update({}, cls.__proto), proto)) if not callback(self, value) then
new_cls.__parent = cls self["_" .. name] = value
return new_cls
end
local function add_setters(cl, fields)
for field, setter in pairs(fields) do
cl[field] = function(self, value)
setter(self, value)
self["_"..field] = value
return self
end
end
cl.__call = function(self, ...)
local name_or_options
for i=1, select("#", ...) do
name_or_options = select(i, ...)
if type(name_or_options) == "string" then
if self._aliases then
table.insert(self._aliases, name_or_options)
end
if not self._aliases or not self._name then
self._name = name_or_options
end
elseif type(name_or_options) == "table" then
for field in pairs(fields) do
if name_or_options[field] ~= nil then
self[field](self, name_or_options[field])
end
end
end
end end
return self return self
end end
return cl names[name] = true
end end
local typecheck = setmetatable({}, { function class.__call(self, ...)
__index = function(self, type_) -- When calling an object, if the first argument is a table,
local typechecker_factory = function(field) -- interpret keys as property names, else delegate arguments
return function(_, value) -- to corresponding setters in order.
if type(value) ~= type_ then if type((...)) == "table" then
error(("bad field '%s' (%s expected, got %s)"):format(field, type_, type(value))) for name, value in pairs((...)) do
end if names[name] then
self[name](self, value)
end end
end end
else
local nargs = select("#", ...)
self[type_] = typechecker_factory for i, property in ipairs(properties) do
return typechecker_factory if i > nargs then
end break
}) end
local function aliased_name(self, name) local arg = select(i, ...)
typecheck.string "name" (self, name)
table.insert(self._aliases, name) if arg ~= nil then
end self[property[1]](self, arg)
end
local function aliased_aliases(self, aliases)
typecheck.table "aliases" (self, aliases)
if not self._name then
self._name = aliases[1]
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(_, 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
return self
end end
local function argname(_, value) -- If indexing class fails, fallback to its parent.
if type(value) ~= "string" then local class_metatable = {}
if type(value) ~= "table" then class_metatable.__index = parent
error(("bad field 'argname' (string or table expected, got %s)"):format(type(value)))
end function class_metatable.__call(self, ...)
end -- Calling a class returns its instance.
-- Arguments are delegated to the instance.
local object = deep_update({}, self.__prototype)
setmetatable(object, self)
return object(...)
end end
local function add_help(self, param) return setmetatable(class, class_metatable)
if self._has_help then
table.remove(self._options)
self._has_help = false
end
if param then
local help = self:flag()
:description "Show this help message and exit."
:action(function()
io.stdout:write(self:get_help() .. "\n")
os.exit(0)
end)(param)
if not help._name then
help "-h" "--help"
end
self._has_help = true
end
end
Parser = add_setters(class {
_arguments = {},
_options = {},
_commands = {},
_mutexes = {},
_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",
add_help = add_help
})
Command = add_setters(extend(Parser, {
_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",
add_help = add_help
})
Argument = add_setters(class {
_minargs = 1,
_maxargs = 1,
_mincount = 1,
_maxcount = 1,
_defmode = "unused",
_show_default = true
}, {
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,
argname = argname,
show_default = typecheck.boolean "show_default"
})
Option = add_setters(extend(Argument, {
_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",
argname = argname,
show_default = typecheck.boolean "show_default"
})
end end
local function typecheck(name, types, value)
for _, type_ in ipairs(types) do
if type(value) == type_ then
return true
end
end
error(("bad property '%s' (%s expected, got %s)"):format(name, table.concat(types, " or "), type(value)))
end
local function typechecked(name, ...)
local types = {...}
return {name, function(_, value) typecheck(name, types, value) end}
end
local aliased_name = {"name", function(self, value)
typecheck("name", {"string"}, value)
table.insert(self._aliases, value)
-- Do not set _name to value if there is a name already.
return self._name
end}
local aliased_aliases = {"aliases", function(self, value)
typecheck("aliases", {"table"}, value)
if not self._name then
self._name = value[1]
end
end}
local function parse_boundaries(str)
if tonumber(str) then
return tonumber(str), tonumber(str)
end
if str == "*" then
return 0, math.huge
end
if str == "+" then
return 1, math.huge
end
if str == "?" then
return 0, 1
end
if str:match "^%d+%-%d+$" then
local min, max = str:match "^(%d+)%-(%d+)$"
return tonumber(min), tonumber(max)
end
if str:match "^%d+%+$" then
local min = str:match "^(%d+)%+$"
return tonumber(min), math.huge
end
end
local function boundaries(name)
return {name, function(self, value)
typecheck(name, {"number", "string"}, value)
local min, max = parse_boundaries(value)
if not min then
error(("bad property '%s'"):format(name))
end
self["_min" .. name], self["_max" .. name] = min, max
end}
end
local add_help = {"add_help", function(self, value)
if self._has_help then
table.remove(self._options)
self._has_help = false
end
if value then
local help = self:flag()
:description "Show this help message and exit."
:action(function()
print(self:get_help())
os.exit(0)
end)
if value ~= true then
help = help(value)
end
if not help._name then
help "-h" "--help"
end
self._has_help = true
end
end}
local Parser = new_class({
_arguments = {},
_options = {},
_commands = {},
_mutexes = {},
_require_command = true
}, {
typechecked("name", "string"),
typechecked("description", "string"),
typechecked("epilog", "string"),
typechecked("usage", "string"),
typechecked("help", "string"),
typechecked("require_command", "boolean"),
add_help
})
local Command = new_class({
_aliases = {}
}, {
aliased_name,
aliased_aliases,
typechecked("description", "string"),
typechecked("epilog", "string"),
typechecked("target", "string"),
typechecked("usage", "string"),
typechecked("help", "string"),
typechecked("require_command", "boolean"),
typechecked("action", "function"),
add_help
}, Parser)
local Argument = new_class({
_minargs = 1,
_maxargs = 1,
_mincount = 1,
_maxcount = 1,
_defmode = "unused",
_show_default = true
}, {
typechecked("name", "string"),
typechecked("description", "string"),
typechecked("target", "string"),
boundaries("args"),
typechecked("default", "string"),
typechecked("defmode", "string"),
typechecked("show_default", "boolean"),
typechecked("argname", "string", "table"),
typechecked("convert", "function", "table")
})
local Option = new_class({
_aliases = {},
_mincount = 0,
_overwrite = true
}, {
aliased_name,
aliased_aliases,
typechecked("description", "string"),
typechecked("target", "string"),
boundaries("args"),
boundaries("count"),
typechecked("default", "string"),
typechecked("defmode", "string"),
typechecked("show_default", "boolean"),
typechecked("overwrite", "boolean"),
typechecked("argname", "string", "table"),
typechecked("convert", "function", "table"),
typechecked("action", "function")
}, Argument)
function Argument:_get_argument_list() function Argument:_get_argument_list()
local buf = {} local buf = {}
local i = 1 local i = 1