mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
added default values
This commit is contained in:
@@ -34,15 +34,14 @@ TODO L1
|
|||||||
=======
|
=======
|
||||||
|
|
||||||
* More tests
|
* More tests
|
||||||
* Add defaults
|
* Add converters
|
||||||
* Improve error messages: delegate them to parser, have separate methods for each error type
|
* Improve error messages: delegate them to parser, have separate methods for each error type
|
||||||
|
|
||||||
TODO L2
|
TODO L2
|
||||||
=======
|
=======
|
||||||
|
|
||||||
* Add converters
|
|
||||||
* Add choices
|
* Add choices
|
||||||
* Make interface decalrative
|
* Make interface declarative
|
||||||
* Refactor Parser
|
* Refactor Parser
|
||||||
* Write primitive ugly usage and help message generation
|
* Write primitive ugly usage and help message generation
|
||||||
|
|
||||||
|
84
spec/default_spec.lua
Normal file
84
spec/default_spec.lua
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
local largparse = require "largparse"
|
||||||
|
|
||||||
|
describe("tests related to default values", function()
|
||||||
|
describe("default values for arguments", function()
|
||||||
|
it("handles default argument correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:argument("foo", {
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({})
|
||||||
|
assert.same(args, {foo = "bar"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles default multi-argument correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:argument("foo", {
|
||||||
|
args = 3,
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"baz"})
|
||||||
|
assert.same(args, {foo = {"baz", "bar", "bar"}})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("does not use default values if not needed", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:argument("foo", {
|
||||||
|
args = "1-2",
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"baz"})
|
||||||
|
assert.same(args, {foo = {"baz"}})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("default values for options", function()
|
||||||
|
it("handles option with default value correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-f", "--foo", {
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"-f"})
|
||||||
|
assert.same(args, {foo = "bar"})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("doesn't use default if option is not invoked", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-f", "--foo", {
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({})
|
||||||
|
assert.same(args, {})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles default multi-argument correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-f", "--foo", {
|
||||||
|
args = 3,
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"--foo=baz"})
|
||||||
|
assert.same(args, {foo = {"baz", "bar", "bar"}})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("does not use default values if not needed", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-f", "--foo", {
|
||||||
|
args = "1-2",
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"-f", "baz"})
|
||||||
|
assert.same(args, {foo = {"baz"}})
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles multi-count options with default value correctly", function()
|
||||||
|
local parser = largparse.parser()
|
||||||
|
parser:option("-f", "--foo", {
|
||||||
|
count = "*",
|
||||||
|
default = "bar"
|
||||||
|
})
|
||||||
|
local args = parser:parse({"-f", "--foo=baz", "--foo"})
|
||||||
|
assert.same(args, {foo = {"bar", "baz", "bar"}})
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
@@ -112,7 +112,15 @@ function State:_check()
|
|||||||
|
|
||||||
for _, passed in ipairs(invocations) do
|
for _, passed in ipairs(invocations) do
|
||||||
self:_assert(#passed <= element.maxargs, "too many arguments")
|
self:_assert(#passed <= element.maxargs, "too many arguments")
|
||||||
self:_assert(#passed >= element.minargs, "too few arguments")
|
if #passed < element.minargs then
|
||||||
|
if element.default then
|
||||||
|
for i = 1, element.minargs-#passed do
|
||||||
|
table.insert(passed, element.default)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
self:_error("too few arguments")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self._result[element.target] = invocations
|
self._result[element.target] = invocations
|
||||||
|
Reference in New Issue
Block a user