mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
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:
@@ -92,14 +92,6 @@ describe("tests related to positional arguments", function()
|
|||||||
assert.has_error(function() parser:parse{"bar", "baz"} end, "too many arguments")
|
assert.has_error(function() parser:parse{"bar", "baz"} end, "too many arguments")
|
||||||
end)
|
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()
|
it("handles too few arguments with one argument correctly", function()
|
||||||
local parser = argparse.parser()
|
local parser = argparse.parser()
|
||||||
parser:argument "foo"
|
parser:argument "foo"
|
||||||
|
@@ -80,6 +80,30 @@ describe("tests related to options", function()
|
|||||||
assert.same({quiet = true, server = "foo"}, args)
|
assert.same({quiet = true, server = "foo"}, args)
|
||||||
end)
|
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()
|
describe("Options with optional argument", function()
|
||||||
it("handles emptiness correctly", function()
|
it("handles emptiness correctly", function()
|
||||||
local parser = argparse.parser()
|
local parser = argparse.parser()
|
||||||
@@ -186,6 +210,7 @@ describe("tests related to options", function()
|
|||||||
it("handles unknown options correctly", function()
|
it("handles unknown options correctly", function()
|
||||||
local parser = argparse.parser()
|
local parser = argparse.parser()
|
||||||
:add_help(false)
|
:add_help(false)
|
||||||
|
parser:option "--option"
|
||||||
assert.has_error(function() parser:parse{"--server"} end, "unknown option '--server'")
|
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{"--server=localhost"} end, "unknown option '--server'")
|
||||||
assert.has_error(function() parser:parse{"-s"} end, "unknown option '-s'")
|
assert.has_error(function() parser:parse{"-s"} end, "unknown option '-s'")
|
||||||
|
@@ -179,24 +179,20 @@ function Parser:assert(assertion, ...)
|
|||||||
return assertion or self:error(...)
|
return assertion or self:error(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:make_charset()
|
function Parser:update_charset(charset)
|
||||||
if not self._charset then
|
charset = charset or {}
|
||||||
self._charset = {["-"] = true}
|
|
||||||
|
|
||||||
for _, command in ipairs(self._commands) do
|
for _, command in ipairs(self._commands) do
|
||||||
command:make_charset()
|
command:update_charset(charset)
|
||||||
|
end
|
||||||
|
|
||||||
for char in pairs(command._charset) do
|
for _, option in ipairs(self._options) do
|
||||||
self._charset[char] = true
|
for _, alias in ipairs(option._aliases) do
|
||||||
end
|
charset[alias:sub(1, 1)] = true
|
||||||
end
|
|
||||||
|
|
||||||
for _, option in ipairs(self._options) do
|
|
||||||
for _, alias in ipairs(option._aliases) do
|
|
||||||
self._charset[alias:sub(1, 1)] = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return charset
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:make_targets()
|
function Parser:make_targets()
|
||||||
@@ -304,7 +300,6 @@ function Parser:prepare()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
self:make_charset()
|
|
||||||
self:make_targets()
|
self:make_targets()
|
||||||
self:make_boundaries()
|
self:make_boundaries()
|
||||||
self:make_command_names()
|
self:make_command_names()
|
||||||
@@ -588,7 +583,6 @@ function Parser:parse(args)
|
|||||||
|
|
||||||
local function switch(p)
|
local function switch(p)
|
||||||
parser = p:prepare()
|
parser = p:prepare()
|
||||||
charset = parser._charset
|
|
||||||
|
|
||||||
if parser._action then
|
if parser._action then
|
||||||
table.insert(com_callbacks, parser._action)
|
table.insert(com_callbacks, parser._action)
|
||||||
@@ -721,6 +715,7 @@ function Parser:parse(args)
|
|||||||
end
|
end
|
||||||
|
|
||||||
switch(self)
|
switch(self)
|
||||||
|
charset = parser:update_charset()
|
||||||
mainloop()
|
mainloop()
|
||||||
|
|
||||||
if cur_option then
|
if cur_option then
|
||||||
|
Reference in New Issue
Block a user