From ff9abac990248ff991d530d6b483a305204a1d2d Mon Sep 17 00:00:00 2001 From: mpeterv Date: Tue, 9 Jun 2015 21:54:18 +0300 Subject: [PATCH] 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. --- spec/arguments_spec.lua | 36 ++-- spec/default_spec.lua | 36 ++-- spec/help_spec.lua | 3 +- spec/options_spec.lua | 84 ++++---- src/argparse.lua | 449 ++++++++++++++++++++-------------------- 5 files changed, 303 insertions(+), 305 deletions(-) diff --git a/spec/arguments_spec.lua b/spec/arguments_spec.lua index 7857537..bc38eac 100644 --- a/spec/arguments_spec.lua +++ b/spec/arguments_spec.lua @@ -34,30 +34,30 @@ describe("tests related to positional arguments", function() it("handles multi-argument correctly", function() local parser = Parser() - parser:argument("foo", { + parser:argument "foo" { args = "*" - }) + } local args = parser:parse({"bar", "baz", "qu"}) assert.same({foo = {"bar", "baz", "qu"}}, args) end) it("handles restrained multi-argument correctly", function() local parser = Parser() - parser:argument("foo", { + parser:argument "foo" { args = "2-4" - }) + } local args = parser:parse({"bar", "baz"}) assert.same({foo = {"bar", "baz"}}, args) end) it("handles several multi-arguments correctly", function() local parser = Parser() - parser:argument("foo1", { + parser:argument "foo1" { args = "1-2" - }) - parser:argument("foo2", { + } + parser:argument "foo2" { args = "*" - }) + } local args = parser:parse({"bar"}) assert.same({foo1 = {"bar"}, foo2 = {}}, args) 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() local parser = Parser() - parser:argument("foo", { + parser:argument "foo" { args = "2-4" - }) + } assert.has_error(function() parser:parse{"foo"} end, "too few arguments") end) it("handles too many arguments with several multi-arguments correctly", function() local parser = Parser() - parser:argument("foo1", { + parser:argument "foo1" { args = "1-2" - }) - parser:argument("foo2", { + } + parser:argument "foo2" { args = "0-1" - }) + } assert.has_error(function() parser:parse{"foo", "bar", "baz", "qu"} end, "too many arguments") end) it("handles too few arguments with several multi-arguments correctly", function() local parser = Parser() - parser:argument("foo1", { + parser:argument "foo1" { args = "1-2" - }) - parser:argument("foo2", { + } + parser:argument "foo2" { args = "*" - }) + } assert.has_error(function() parser:parse{} end, "too few arguments") end) end) diff --git a/spec/default_spec.lua b/spec/default_spec.lua index 746d141..da8fd5b 100644 --- a/spec/default_spec.lua +++ b/spec/default_spec.lua @@ -15,31 +15,31 @@ describe("tests related to default values", function() it("handles default argument for multi-argument correctly", function() local parser = Parser() - parser:argument("foo", { + parser:argument "foo" { args = 3, default = "bar", defmode = "arg" - }) + } local args = parser:parse{"baz"} assert.same({foo = {"baz", "bar", "bar"}}, args) end) it("handles default value for multi-argument correctly", function() local parser = Parser() - parser:argument("foo", { + parser:argument "foo" { args = 3, default = "bar" - }) + } local args = parser:parse{} assert.same({foo = {"bar", "bar", "bar"}}, args) end) it("does not use default values if not needed", function() local parser = Parser() - parser:argument("foo", { + parser:argument "foo" { args = "1-2", default = "bar" - }) + } local args = parser:parse({"baz"}) assert.same({foo = {"baz"}}, args) end) @@ -60,20 +60,20 @@ describe("tests related to default values", function() it("handles option with default value for multi-argument option correctly", function() local parser = Parser() - parser:option("-s", "--several", { + parser:option "-s" "--several" { default = "foo", args = "2-3" - }) + } local args = parser:parse{} assert.same({several = {"foo", "foo"}}, args) end) it("handles option with default value and argument", function() local parser = Parser() - parser:option("-o", "--output", { + parser:option "-o" "--output" { default = "a.out", defmode = "arg+count" - }) + } local args = parser:parse{} assert.same({output = "a.out"}, args) 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() local parser = Parser() - parser:option("-f", "--foo", { + parser:option "-f" "--foo" { default = "bar", defmode = "arg" - }) + } local args = parser:parse{} assert.same({}, args) end) it("handles default multi-argument correctly", function() local parser = Parser() - parser:option("-f", "--foo", { + parser:option "-f" "--foo" { args = 3, default = "bar", defmode = "arg" - }) + } local args = parser:parse({"--foo=baz"}) assert.same({foo = {"baz", "bar", "bar"}}, args) end) it("does not use default values if not needed", function() local parser = Parser() - parser:option("-f", "--foo", { + parser:option "-f" "--foo" { args = "1-2", default = "bar", defmode = "arg" - }) + } local args = parser:parse({"-f", "baz"}) assert.same({foo = {"baz"}}, args) end) it("handles multi-count options with default value correctly", function() local parser = Parser() - parser:option("-f", "--foo", { + parser:option "-f" "--foo" { count = "*", default = "bar", defmode = "arg + count" - }) + } local args = parser:parse({"-f", "--foo=baz", "--foo"}) assert.same({foo = {"bar", "baz", "bar"}}, args) end) diff --git a/spec/help_spec.lua b/spec/help_spec.lua index bdefb96..487ce28 100644 --- a/spec/help_spec.lua +++ b/spec/help_spec.lua @@ -53,8 +53,7 @@ An epilog. ]], parser:get_help()) :args "2" parser:argument "maybe-fourth" :args "?" - parser:argument "others" - :description "Optional. " + parser:argument("others", "Optional. ") :args "*" assert.equal([[ diff --git a/spec/options_spec.lua b/spec/options_spec.lua index 76498e6..74f1f02 100644 --- a/spec/options_spec.lua +++ b/spec/options_spec.lua @@ -5,21 +5,21 @@ describe("tests related to options", function() describe("passing correct options", function() it("handles no options passed correctly", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" local args = parser:parse({}) assert.same({}, args) end) it("handles one option correctly", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" local args = parser:parse({"--server", "foo"}) assert.same({server = "foo"}, args) end) it("normalizes default target", function() local parser = Parser() - parser:option("--from-server") + parser:option "--from-server" local args = parser:parse({"--from-server", "foo"}) assert.same({from_server = "foo"}, args) end) @@ -34,39 +34,39 @@ describe("tests related to options", function() it("handles GNU-style long options", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" local args = parser:parse({"--server=foo"}) assert.same({server = "foo"}, args) end) it("handles GNU-style long options even when it could take more arguments", function() local parser = Parser() - parser:option("-s", "--server", { + parser:option "-s" "--server" { args = "*" - }) + } local args = parser:parse({"--server=foo"}) assert.same({server = {"foo"}}, args) end) it("handles GNU-style long options for multi-argument options", function() local parser = Parser() - parser:option("-s", "--server", { + parser:option "-s" "--server" { args = "1-2" - }) + } local args = parser:parse({"--server=foo", "bar"}) assert.same({server = {"foo", "bar"}}, args) end) it("handles short option correclty", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" local args = parser:parse({"-s", "foo"}) assert.same({server = "foo"}, args) end) it("handles flag correclty", function() local parser = Parser() - parser:flag("-q", "--quiet") + parser:flag "-q" "--quiet" local args = parser:parse({"--quiet"}) assert.same({quiet = true}, args) args = parser:parse({}) @@ -75,23 +75,23 @@ describe("tests related to options", function() it("handles combined flags correclty", function() local parser = Parser() - parser:flag("-q", "--quiet") - parser:flag("-f", "--fast") + parser:flag "-q" "--quiet" + parser:flag "-f" "--fast" local args = parser:parse({"-qf"}) assert.same({quiet = true, fast = true}, args) end) it("handles short options without space between option and argument", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" local args = parser:parse({"-sfoo"}) assert.same({server = "foo"}, args) end) it("handles flags combined with short option correclty", function() local parser = Parser() - parser:flag("-q", "--quiet") - parser:option("-s", "--server") + parser:flag "-q" "--quiet" + parser:option "-s" "--server" local args = parser:parse({"-qsfoo"}) assert.same({quiet = true, server = "foo"}, args) end) @@ -152,27 +152,27 @@ describe("tests related to options", function() describe("Options with optional argument", function() it("handles emptiness correctly", function() local parser = Parser() - parser:option("-p", "--password", { + parser:option "-p" "--password" { args = "?" - }) + } local args = parser:parse({}) assert.same({}, args) end) it("handles option without argument correctly", function() local parser = Parser() - parser:option("-p", "--password", { + parser:option "-p" "--password" { args = "?" - }) + } local args = parser:parse({"-p"}) assert.same({password = {}}, args) end) it("handles option with argument correctly", function() local parser = Parser() - parser:option("-p", "--password", { + parser:option "-p" "--password" { args = "?" - }) + } local args = parser:parse({"-p", "password"}) assert.same({password = {"password"}}, args) end) @@ -180,9 +180,9 @@ describe("tests related to options", function() it("handles multi-argument options correctly", function() local parser = Parser() - parser:option("--pair", { + parser:option "--pair" { args = 2 - }) + } local args = parser:parse({"--pair", "Alice", "Bob"}) assert.same({pair = {"Alice", "Bob"}}, args) end) @@ -190,55 +190,55 @@ describe("tests related to options", function() describe("Multi-count options", function() it("handles multi-count option correctly", function() local parser = Parser() - parser:option("-e", "--exclude", { + parser:option "-e" "--exclude" { count = "*" - }) + } local args = parser:parse({"-efoo", "--exclude=bar", "-e", "baz"}) assert.same({exclude = {"foo", "bar", "baz"}}, args) end) it("handles not used multi-count option correctly", function() local parser = Parser() - parser:option("-e", "--exclude", { + parser:option "-e" "--exclude" { count = "*" - }) + } local args = parser:parse({}) assert.same({exclude = {}}, args) end) it("handles multi-count multi-argument option correctly", function() local parser = Parser() - parser:option("-e", "--exclude", { + parser:option "-e" "--exclude" { count = "*", args = 2 - }) + } local args = parser:parse({"-e", "Alice", "Bob", "-e", "Emma", "Jacob"}) assert.same({exclude = {{"Alice", "Bob"}, {"Emma", "Jacob"}}}, args) end) it("handles multi-count flag correctly", function() local parser = Parser() - parser:flag("-q", "--quiet", { + parser:flag "-q" "--quiet" { count = "*" - }) + } local args = parser:parse({"-qq", "--quiet"}) assert.same({quiet = 3}, args) end) it("overwrites old invocations", function() local parser = Parser() - parser:option("-u", "--user", { + parser:option "-u" "--user" { count = "0-2" - }) + } local args = parser:parse({"-uAlice", "--user=Bob", "--user", "John"}) assert.same({user = {"Bob", "John"}}, args) end) it("handles not used multi-count flag correctly", function() local parser = Parser() - parser:flag("-q", "--quiet", { + parser:flag "-q" "--quiet" { count = "*" - }) + } local args = parser:parse({}) assert.same({quiet = 0}, args) end) @@ -248,7 +248,7 @@ describe("tests related to options", function() describe("passing incorrect options", function() it("handles lack of required argument correctly", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" assert.has_error(function() parser:parse{"--server"} end, "too few arguments") end) @@ -264,30 +264,30 @@ describe("tests related to options", function() it("handles too many arguments correctly", function() local parser = Parser() - parser:option("-s", "--server") + parser:option "-s" "--server" assert.has_error(function() parser:parse{"-sfoo", "bar"} end, "too many arguments") end) it("doesn't accept GNU-like long options when it doesn't need arguments", function() 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") end) it("handles too many invocations correctly", function() local parser = Parser() - parser:flag("-q", "--quiet", { + parser:flag "-q" "--quiet" { count = 1, overwrite = false - }) + } assert.has_error(function() parser:parse{"-qq"} end, "option '-q' must be used at most 1 time") end) it("handles too few invocations correctly", function() local parser = Parser() - parser:option("-f", "--foo", { + parser:option "-f" "--foo" { count = "3-4" - }) + } assert.has_error(function() parser:parse{"-fFOO", "-fBAR"} end, "option '-f' must be used at least 3 times") end) end) diff --git a/src/argparse.lua b/src/argparse.lua index 591a064..8e9b9e4 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -1,257 +1,256 @@ -local Parser, Command, Argument, Option - --- Create classes with setters -do - 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 +local function deep_update(t1, t2) + for k, v in pairs(t2) do + if type(v) == "table" then + v = deep_update({}, v) end - return t1 + t1[k] = v end - local class_metatable = {} + return t1 +end - function class_metatable.__call(cls, ...) - return setmetatable(deep_update({}, cls.__proto), cls)(...) +-- 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 - function class_metatable.__index(cls, key) - return cls.__parent and cls.__parent[key] - end + local names = {} - local function class(proto) - local cls = setmetatable({__proto = proto, __parent = {}}, class_metatable) - cls.__index = cls - return cls - end + -- Create setter methods and fill set of property names. + for _, property in ipairs(properties) do + local name, callback = property[1], property[2] - 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 - - 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 + class[name] = function(self, value) + if not callback(self, value) then + self["_" .. name] = value end return self end - return cl + names[name] = true end - 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 + 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("#", ...) - self[type_] = typechecker_factory - return typechecker_factory - end - }) + for i, property in ipairs(properties) do + if i > nargs then + break + end - local function aliased_name(self, name) - typecheck.string "name" (self, name) + local arg = select(i, ...) - table.insert(self._aliases, name) - 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))) + if arg ~= nil then + self[property[1]](self, arg) + end end end + + return self 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 + -- If indexing class fails, fallback to its parent. + local class_metatable = {} + class_metatable.__index = parent + + function class_metatable.__call(self, ...) + -- Calling a class returns its instance. + -- Arguments are delegated to the instance. + local object = deep_update({}, self.__prototype) + setmetatable(object, self) + return object(...) end - local function add_help(self, param) - 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" - }) + return setmetatable(class, class_metatable) 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() local buf = {} local i = 1