mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Reworked default values
* Better out-of-the-box behavior: commonly used feature should work without configuration. Only use default value if argument/option was not used at all. * Add `defmode` field so that old behaviour can be used, too.
This commit is contained in:
@@ -4,21 +4,33 @@ describe("tests related to default values", function()
|
||||
describe("default values for arguments", function()
|
||||
it("handles default argument correctly", function()
|
||||
local parser = Parser()
|
||||
parser:argument("foo", {
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
parser:argument "foo"
|
||||
:default "bar"
|
||||
local args = parser:parse{}
|
||||
assert.same({foo = "bar"}, args)
|
||||
local args = parser:parse{"baz"}
|
||||
assert.same({foo = "baz"}, args)
|
||||
end)
|
||||
|
||||
it("handles default multi-argument correctly", function()
|
||||
it("handles default argument for multi-argument correctly", function()
|
||||
local parser = Parser()
|
||||
parser:argument("foo", {
|
||||
args = 3,
|
||||
default = "bar",
|
||||
defmode = "arg"
|
||||
})
|
||||
local args = parser:parse{"baz"}
|
||||
assert.same({foo = {"baz", "bar", "bar"}}, args)
|
||||
end)
|
||||
|
||||
it("handles default value for multi-argument correctly", function()
|
||||
local parser = Parser()
|
||||
parser:argument("foo", {
|
||||
args = 3,
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"baz"})
|
||||
assert.same({foo = {"baz", "bar", "bar"}}, args)
|
||||
local args = parser:parse{}
|
||||
assert.same({foo = {"bar", "bar", "bar"}}, args)
|
||||
end)
|
||||
|
||||
it("does not use default values if not needed", function()
|
||||
@@ -34,29 +46,57 @@ describe("tests related to default values", function()
|
||||
|
||||
describe("default values for options", function()
|
||||
it("handles option with default value correctly", function()
|
||||
local parser = Parser()
|
||||
parser:option("-f", "--foo", {
|
||||
default = "bar"
|
||||
})
|
||||
local args = parser:parse({"-f"})
|
||||
assert.same({foo = "bar"}, args)
|
||||
end)
|
||||
|
||||
it("handles underused option with default value correctly", function()
|
||||
local parser = Parser()
|
||||
parser:option "-o" "--output"
|
||||
:count(1)
|
||||
:default "a.out"
|
||||
local args = parser:parse{}
|
||||
assert.same({output = "a.out"}, args)
|
||||
args = parser:parse{"--output", "foo.txt"}
|
||||
assert.same({output = "foo.txt"}, args)
|
||||
assert.has_error(function() parser:parse{"-o"} end, "too few arguments")
|
||||
end)
|
||||
|
||||
it("doesn't use default if option is not invoked", function()
|
||||
it("handles option with default value for multi-argument option correctly", function()
|
||||
local parser = Parser()
|
||||
parser:option("-s", "--several", {
|
||||
default = "foo",
|
||||
args = "2-3"
|
||||
})
|
||||
local args = parser:parse{}
|
||||
assert.same({several = {"foo", "foo"}}, args)
|
||||
end)
|
||||
|
||||
it("handles option with default value and argument", function()
|
||||
local parser = Parser()
|
||||
parser:option("-o", "--output", {
|
||||
default = "a.out",
|
||||
defmode = "arg+count"
|
||||
})
|
||||
local args = parser:parse{}
|
||||
assert.same({output = "a.out"}, args)
|
||||
args = parser:parse{"-o"}
|
||||
assert.same({output = "a.out"}, args)
|
||||
args = parser:parse{"-o", "value"}
|
||||
assert.same({output = "value"}, args)
|
||||
end)
|
||||
|
||||
it("handles option with default argument correctly", function()
|
||||
local parser = Parser()
|
||||
parser:option "-p" "--protected"
|
||||
:target "password"
|
||||
:default "password"
|
||||
:defmode "arg"
|
||||
local args = parser:parse{"-p"}
|
||||
assert.same({password = "password"}, args)
|
||||
end)
|
||||
|
||||
it("doesn't use default argument if option is not invoked", function()
|
||||
local parser = Parser()
|
||||
parser:option("-f", "--foo", {
|
||||
default = "bar"
|
||||
default = "bar",
|
||||
defmode = "arg"
|
||||
})
|
||||
local args = parser:parse({})
|
||||
local args = parser:parse{}
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
@@ -64,7 +104,8 @@ describe("tests related to default values", function()
|
||||
local parser = Parser()
|
||||
parser:option("-f", "--foo", {
|
||||
args = 3,
|
||||
default = "bar"
|
||||
default = "bar",
|
||||
defmode = "arg"
|
||||
})
|
||||
local args = parser:parse({"--foo=baz"})
|
||||
assert.same({foo = {"baz", "bar", "bar"}}, args)
|
||||
@@ -74,7 +115,8 @@ describe("tests related to default values", function()
|
||||
local parser = Parser()
|
||||
parser:option("-f", "--foo", {
|
||||
args = "1-2",
|
||||
default = "bar"
|
||||
default = "bar",
|
||||
defmode = "arg"
|
||||
})
|
||||
local args = parser:parse({"-f", "baz"})
|
||||
assert.same({foo = {"baz"}}, args)
|
||||
@@ -84,7 +126,8 @@ describe("tests related to default values", function()
|
||||
local parser = Parser()
|
||||
parser:option("-f", "--foo", {
|
||||
count = "*",
|
||||
default = "bar"
|
||||
default = "bar",
|
||||
defmode = "arg + count"
|
||||
})
|
||||
local args = parser:parse({"-f", "--foo=baz", "--foo"})
|
||||
assert.same({foo = {"bar", "baz", "bar"}}, args)
|
||||
|
Reference in New Issue
Block a user