mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Rework usage message building and support arguments in mutexes
Ref #11.
This commit is contained in:
@@ -19,6 +19,24 @@ describe("tests related to mutexes", function()
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles mutex with an argument", function()
|
||||
local parser = Parser()
|
||||
parser:mutex(
|
||||
parser:flag "-q" "--quiet"
|
||||
:description "Supress output.",
|
||||
parser:argument "log"
|
||||
:args "?"
|
||||
:description "Log file"
|
||||
)
|
||||
|
||||
local args = parser:parse{"-q"}
|
||||
assert.same({quiet = true}, args)
|
||||
args = parser:parse{"log.txt"}
|
||||
assert.same({log = "log.txt"}, args)
|
||||
args = parser:parse{}
|
||||
assert.same({}, args)
|
||||
end)
|
||||
|
||||
it("handles mutex with default value", function()
|
||||
local parser = Parser()
|
||||
parser:mutex(
|
||||
@@ -48,6 +66,24 @@ describe("tests related to mutexes", function()
|
||||
end, "option '--quiet' can not be used together with option '-v'")
|
||||
end)
|
||||
|
||||
it("raises an error if mutex with an argument is broken", function()
|
||||
local parser = Parser()
|
||||
parser:mutex(
|
||||
parser:flag "-q" "--quiet"
|
||||
:description "Supress output.",
|
||||
parser:argument "log"
|
||||
:args "?"
|
||||
:description "Log file"
|
||||
)
|
||||
|
||||
assert.has_error(function()
|
||||
parser:parse{"-q", "log.txt"}
|
||||
end, "argument 'log' can not be used together with option '-q'")
|
||||
assert.has_error(function()
|
||||
parser:parse{"log.txt", "--quiet"}
|
||||
end, "option '--quiet' can not be used together with argument 'log'")
|
||||
end)
|
||||
|
||||
it("handles multiple mutexes", function()
|
||||
local parser = Parser()
|
||||
parser:mutex(
|
||||
|
@@ -194,7 +194,31 @@ Usage: foo ([-q] | [-v] | [-i]) ([-l] | [-f <from>])
|
||||
)
|
||||
end)
|
||||
|
||||
it("puts vararg option and mutex usages after positional arguments", function()
|
||||
it("creates correct usage message for mutexes with arguments", function()
|
||||
local parser = Parser "foo"
|
||||
:add_help(false)
|
||||
|
||||
parser:argument "first"
|
||||
parser:mutex(
|
||||
parser:flag "-q" "--quiet",
|
||||
parser:flag "-v" "--verbose",
|
||||
parser:argument "second":args "?"
|
||||
)
|
||||
parser:argument "third"
|
||||
parser:mutex(
|
||||
parser:flag "-l" "--local",
|
||||
parser:option "-f" "--from"
|
||||
)
|
||||
parser:option "--yet-another-option"
|
||||
|
||||
assert.equal([=[
|
||||
Usage: foo ([-l] | [-f <from>])
|
||||
[--yet-another-option <yet_another_option>] <first>
|
||||
([-q] | [-v] | [<second>]) <third>]=], parser:get_usage()
|
||||
)
|
||||
end)
|
||||
|
||||
it("puts vararg option and mutex usages after positional arguments", function()
|
||||
local parser = Parser "foo"
|
||||
:add_help(false)
|
||||
parser:argument("argument")
|
||||
@@ -217,4 +241,34 @@ Usage: foo ([-q] | [-v] | [-i])
|
||||
[--vararg-option <vararg_option> [<vararg_option>]]]=], parser:get_usage()
|
||||
)
|
||||
end)
|
||||
|
||||
it("doesn't repeat usage of elements within several mutexes", function()
|
||||
local parser = Parser "foo"
|
||||
:add_help(false)
|
||||
|
||||
parser:argument("arg1")
|
||||
local arg2 = parser:argument("arg2"):args "?"
|
||||
parser:argument("arg3"):args "?"
|
||||
local arg4 = parser:argument("arg4"):args "?"
|
||||
|
||||
local opt1 = parser:option("--opt1")
|
||||
local opt2 = parser:option("--opt2")
|
||||
local opt3 = parser:option("--opt3")
|
||||
local opt4 = parser:option("--opt4")
|
||||
local opt5 = parser:option("--opt5")
|
||||
local opt6 = parser:option("--opt6")
|
||||
parser:option("--opt7")
|
||||
|
||||
parser:mutex(arg2, opt1, opt2)
|
||||
parser:mutex(arg4, opt2, opt3, opt4)
|
||||
parser:mutex(opt1, opt3, opt5)
|
||||
parser:mutex(opt1, opt3, opt6)
|
||||
|
||||
assert.equal([=[
|
||||
Usage: foo ([--opt1 <opt1>] | [--opt3 <opt3>] | [--opt5 <opt5>])
|
||||
[--opt6 <opt6>] [--opt7 <opt7>] <arg1>
|
||||
([<arg2>] | [--opt2 <opt2>]) [<arg3>]
|
||||
([<arg4>] | [--opt4 <opt4>])]=], parser:get_usage()
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
Reference in New Issue
Block a user