mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-27 18:42:20 +00:00
* For arguments: "argument 'foo' is required" -> "missing argument 'foo'" * For options: "option '--foo' must be used at least 1 time" -> "missing option '--foo'"
28 lines
923 B
Lua
28 lines
923 B
Lua
local Parser = require "argparse"
|
|
getmetatable(Parser()).error = function(_, msg) error(msg) end
|
|
|
|
describe("tests related to :pparse()", function()
|
|
it("returns true and result on success", function()
|
|
local parser = Parser()
|
|
parser:option "-s --server"
|
|
local ok, args = parser:pparse{"--server", "foo"}
|
|
assert.is_true(ok)
|
|
assert.same({server = "foo"}, args)
|
|
end)
|
|
|
|
it("returns false and bare error message on failure", function()
|
|
local parser = Parser()
|
|
parser:argument "foo"
|
|
local ok, errmsg = parser:pparse{}
|
|
assert.is_false(ok)
|
|
assert.equal("missing argument 'foo'", errmsg)
|
|
end)
|
|
|
|
it("rethrows errors from callbacks", function()
|
|
local parser = Parser()
|
|
parser:flag "--foo"
|
|
:action(function() error("some error message") end)
|
|
assert.error_matches(function() parser:pparse{"--foo"} end, "some error message")
|
|
end)
|
|
end)
|