mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
* Use state objects instead of tons of locals in the main function. * Use actions for storing arguments into result table. Actions are now called at the end of each invocation, with result table, target index, arguments and overwrite flag as arguments. * Remove command actions. * Improve error messages, refer to options by the last used alias instead of the main name. TODO: * Improve error messages further ("argument 'foo' is required" -> "missing argument 'foo'", etc.). * Add actions for positional arguments. * Add actions for commands (should be called with final results after parsing is over, in "innermost first" order). * Allow referring to built-in actions by strings a-la Python (e.g. action = "store_false"). * Allow setting initial value to be stored at target index for each option (perhaps use default value for that). * Add more tests, particularly for actions.
28 lines
927 B
Lua
28 lines
927 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("argument 'foo' is required", 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)
|