mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
more command tests, added require_command
This commit is contained in:
@@ -10,4 +10,53 @@ describe("tests related to commands", function()
|
|||||||
local args = parser:parse{"temp.txt", "remove"}
|
local args = parser:parse{"temp.txt", "remove"}
|
||||||
assert.same({file = "temp.txt", remove = true}, args)
|
assert.same({file = "temp.txt", remove = true}, args)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("switches context properly", function()
|
||||||
|
local parser = argparse.parser "name"
|
||||||
|
local install = parser:command "install"
|
||||||
|
install:flag "-q" "--quiet"
|
||||||
|
|
||||||
|
local args = parser:parse{"install", "-q"}
|
||||||
|
assert.same({install = true, quiet = true}, args)
|
||||||
|
assert.has_error(function() parser:parse{"-q", "install"} end, "unknown option -q")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("allows to continue passing old options", function()
|
||||||
|
local parser = argparse.parser "name"
|
||||||
|
parser:flag "-v" "--verbose" {
|
||||||
|
count = "*"
|
||||||
|
}
|
||||||
|
parser:command "install"
|
||||||
|
|
||||||
|
local args = parser:parse{"-vv", "install", "--verbose"}
|
||||||
|
assert.same({install = true, verbose = 3}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles nested commands", function()
|
||||||
|
local parser = argparse.parser "name"
|
||||||
|
local foo = parser:command "foo"
|
||||||
|
local bar = foo:command "bar"
|
||||||
|
local baz = foo:command "baz"
|
||||||
|
|
||||||
|
local args = parser:parse{"foo", "bar"}
|
||||||
|
assert.same({foo = true, bar = true}, args)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles no commands depending on parser.require_command", function()
|
||||||
|
local parser = argparse.parser "name"
|
||||||
|
parser:command "install"
|
||||||
|
|
||||||
|
local args = parser:parse{}
|
||||||
|
assert.same({}, args)
|
||||||
|
|
||||||
|
parser.require_command = true
|
||||||
|
assert.has_error(function() parser:parse{} end, "command is required")
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("Detects wrong commands", function()
|
||||||
|
local parser = argparse.parser "name"
|
||||||
|
local install = parser:command "install"
|
||||||
|
|
||||||
|
assert.has_error(function() parser:parse{"isntall"} end, "unknown command isntall")
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
@@ -39,7 +39,8 @@ local Parser = class {
|
|||||||
arguments = {},
|
arguments = {},
|
||||||
options = {},
|
options = {},
|
||||||
commands = {},
|
commands = {},
|
||||||
fields = {"name", "description", "target"}
|
require_command = false,
|
||||||
|
fields = {"name", "description", "target", "require_command"}
|
||||||
}:include(Declarative)
|
}:include(Declarative)
|
||||||
|
|
||||||
local Command = Parser:extends {
|
local Command = Parser:extends {
|
||||||
@@ -448,6 +449,10 @@ function Parser:parse(args)
|
|||||||
close(cur_arg)
|
close(cur_arg)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if parser.require_command and #commands > 0 then
|
||||||
|
parser:error("command is required")
|
||||||
|
end
|
||||||
|
|
||||||
format()
|
format()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
Reference in New Issue
Block a user