Remove 'aliases' property

Allow setting several names using 'name' property instead, e.g.
':name "-f --foo"' instead of ':aliases {"-f", "--foo"}'.

This change breaks documented interface of 0.3.x.
This commit is contained in:
mpeterv
2015-06-09 22:06:32 +03:00
parent 4320f40844
commit 44fd3b3cb8
3 changed files with 14 additions and 19 deletions

View File

@@ -146,7 +146,7 @@ Options:
it("creates correct help message for commands", function() it("creates correct help message for commands", function()
local parser = Parser "foo" local parser = Parser "foo"
parser:flag "-q" "--quiet" parser:flag "-q --quiet"
local run = parser:command "run" local run = parser:command "run"
:description "Run! " :description "Run! "
run:option "--where" run:option "--where"

View File

@@ -4,7 +4,7 @@ getmetatable(Parser()).error = function(_, msg) error(msg) end
describe("tests related to :pparse()", function() describe("tests related to :pparse()", function()
it("returns true and result on success", function() it("returns true and result on success", function()
local parser = Parser() local parser = Parser()
parser:option "-s" "--server" parser:option "-s --server"
local ok, args = parser:pparse{"--server", "foo"} local ok, args = parser:pparse{"--server", "foo"}
assert.is_true(ok) assert.is_true(ok)
assert.same({server = "foo"}, args) assert.same({server = "foo"}, args)
@@ -20,8 +20,8 @@ describe("tests related to :pparse()", function()
it("still raises an error if it is caused by misconfiguration", function() it("still raises an error if it is caused by misconfiguration", function()
local parser = Parser() local parser = Parser()
parser:option "--foo" parser:flag "--foo"
:aliases {1, 2, 3} :action(error)
assert.has_error(function() parser:pparse{} end) assert.has_error(function() parser:pparse{"--foo"} end)
end) end)
end) end)

View File

@@ -98,19 +98,16 @@ local function typechecked(name, ...)
return {name, function(_, value) typecheck(name, types, value) end} return {name, function(_, value) typecheck(name, types, value) end}
end end
local aliased_name = {"name", function(self, value) local multiname = {"name", function(self, value)
typecheck("name", {"string"}, 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) for alias in value:gmatch("%S+") do
typecheck("aliases", {"table"}, value) self._name = self._name or alias
table.insert(self._aliases, alias)
if not self._name then
self._name = value[1]
end end
-- Do not set _name as with other properties.
return true
end} end}
local function parse_boundaries(str) local function parse_boundaries(str)
@@ -200,8 +197,7 @@ local Parser = new_class({
local Command = new_class({ local Command = new_class({
_aliases = {} _aliases = {}
}, { }, {
aliased_name, multiname,
aliased_aliases,
typechecked("description", "string"), typechecked("description", "string"),
typechecked("epilog", "string"), typechecked("epilog", "string"),
typechecked("target", "string"), typechecked("target", "string"),
@@ -236,8 +232,7 @@ local Option = new_class({
_mincount = 0, _mincount = 0,
_overwrite = true _overwrite = true
}, { }, {
aliased_name, multiname,
aliased_aliases,
typechecked("description", "string"), typechecked("description", "string"),
typechecked("target", "string"), typechecked("target", "string"),
boundaries("args"), boundaries("args"),