Fixed the charset of the current parser being used.

Now the global set of all control characters of all (sub)commands is used.
This commit is contained in:
mpeterv
2014-02-09 00:18:00 +04:00
parent c26d41228d
commit e7aa042004
3 changed files with 36 additions and 24 deletions

View File

@@ -92,14 +92,6 @@ describe("tests related to positional arguments", function()
assert.has_error(function() parser:parse{"bar", "baz"} end, "too many arguments")
end)
it("handles sudden option correctly", function()
local parser = argparse.parser()
:add_help(false)
parser:argument "foo"
assert.has_error(function() parser:parse{"-q"} end, "unknown option '-q'")
end)
it("handles too few arguments with one argument correctly", function()
local parser = argparse.parser()
parser:argument "foo"

View File

@@ -80,6 +80,30 @@ describe("tests related to options", function()
assert.same({quiet = true, server = "foo"}, args)
end)
describe("Special chars set", function()
it("handles windows-style options", function()
local parser = argparse.parser()
:add_help(false)
parser:option "\\I"
:count "*"
:target "include"
local args = parser:parse{"\\I", "src", "\\I", "misc"}
assert.same({include = {"src", "misc"}}, args)
end)
it("corrects charset in commands", function()
local parser = argparse.parser "name"
:add_help(false)
parser:flag "-v" "--verbose"
:count "*"
parser:command "deep"
:add_help(false)
:option "\\s"
local args = parser:parse{"-v", "deep", "\\s", "foo", "-vv"}
assert.same({verbose = 3, deep = true, s = "foo"}, args)
end)
end)
describe("Options with optional argument", function()
it("handles emptiness correctly", function()
local parser = argparse.parser()
@@ -186,6 +210,7 @@ describe("tests related to options", function()
it("handles unknown options correctly", function()
local parser = argparse.parser()
:add_help(false)
parser:option "--option"
assert.has_error(function() parser:parse{"--server"} end, "unknown option '--server'")
assert.has_error(function() parser:parse{"--server=localhost"} end, "unknown option '--server'")
assert.has_error(function() parser:parse{"-s"} end, "unknown option '-s'")

View File

@@ -179,24 +179,20 @@ function Parser:assert(assertion, ...)
return assertion or self:error(...)
end
function Parser:make_charset()
if not self._charset then
self._charset = {["-"] = true}
function Parser:update_charset(charset)
charset = charset or {}
for _, command in ipairs(self._commands) do
command:make_charset()
for char in pairs(command._charset) do
self._charset[char] = true
end
command:update_charset(charset)
end
for _, option in ipairs(self._options) do
for _, alias in ipairs(option._aliases) do
self._charset[alias:sub(1, 1)] = true
end
charset[alias:sub(1, 1)] = true
end
end
return charset
end
function Parser:make_targets()
@@ -304,7 +300,6 @@ function Parser:prepare()
end)
end
self:make_charset()
self:make_targets()
self:make_boundaries()
self:make_command_names()
@@ -588,7 +583,6 @@ function Parser:parse(args)
local function switch(p)
parser = p:prepare()
charset = parser._charset
if parser._action then
table.insert(com_callbacks, parser._action)
@@ -721,6 +715,7 @@ function Parser:parse(args)
end
switch(self)
charset = parser:update_charset()
mainloop()
if cur_option then