mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Improved usage messages for default values
This commit is contained in:
@@ -40,19 +40,36 @@ describe("tests related to usage message generation", function()
|
||||
)
|
||||
end)
|
||||
|
||||
it("creates correct usage message for elements with default value", function()
|
||||
it("creates correct usage message for arguments with default value", function()
|
||||
local parser = Parser "foo"
|
||||
:add_help(false)
|
||||
parser:argument "input"
|
||||
:default "a.in"
|
||||
parser:argument "pair"
|
||||
:args(2)
|
||||
:default "foo"
|
||||
parser:argument "pair2"
|
||||
:args(2)
|
||||
:default "bar"
|
||||
:defmode "arg"
|
||||
|
||||
assert.equal(
|
||||
[=[Usage: foo [<input>] [<pair> <pair>] [<pair2>] [<pair2>]]=],
|
||||
parser:prepare():get_usage()
|
||||
)
|
||||
end)
|
||||
|
||||
it("creates correct usage message for options with default value", function()
|
||||
local parser = Parser "foo"
|
||||
:add_help(false)
|
||||
parser:option "-f" "--from"
|
||||
:default "there"
|
||||
parser:option "-o" "--output"
|
||||
:default "a.out"
|
||||
:count(1)
|
||||
:defmode "arg"
|
||||
|
||||
assert.equal(
|
||||
[=[Usage: foo [-f [<from>]] [-o [<output>]] [<input>]]=],
|
||||
[=[Usage: foo [-f <from>] [-o [<output>]]]=],
|
||||
parser:prepare():get_usage()
|
||||
)
|
||||
end)
|
||||
|
@@ -102,7 +102,7 @@ function Argument:get_arg_usage(argname)
|
||||
local buf = {}
|
||||
local required_argname = argname
|
||||
|
||||
if self._default then
|
||||
if self._default and self._defmode:find "a" then
|
||||
required_argname = "[" .. argname .. "]"
|
||||
end
|
||||
|
||||
@@ -132,6 +132,12 @@ end
|
||||
function Argument:get_usage()
|
||||
if not self._usage then
|
||||
self._usage = table.concat(self:get_arg_usage("<" .. self._name .. ">"), " ")
|
||||
|
||||
if self._default and self._defmode:find "c" then
|
||||
if self._maxargs > 1 or (self._minargs == 1 and not self._defmode:find "a") then
|
||||
self._usage = "[" .. self._usage .. "]"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return self._usage
|
||||
|
Reference in New Issue
Block a user