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