Files
argparse/spec/pparse_spec.lua
mpeterv babe715548 Change error messages for missing elements
* For arguments: "argument 'foo' is required" -> "missing argument 'foo'"
* For options: "option '--foo' must be used at least 1 time" ->
  "missing option '--foo'"
2015-10-30 15:13:30 +03:00

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)