mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 02:52:20 +00:00
Added show_default field
This commit is contained in:
@@ -116,6 +116,44 @@ Sets verbosity level.
|
||||
}, "\r\n"), parser:get_help())
|
||||
end)
|
||||
|
||||
it("shows default values", function()
|
||||
local parser = Parser "foo"
|
||||
parser:option "-o"
|
||||
:default "a.out"
|
||||
parser:option "-p"
|
||||
:default "8080"
|
||||
:description "Port."
|
||||
|
||||
assert.equal(table.concat({
|
||||
"Usage: foo [-o <o>] [-p <p>] [-h]",
|
||||
"",
|
||||
"Options: ",
|
||||
" -o <o> default: a.out",
|
||||
" -p <p> Port. (default: 8080)",
|
||||
" -h, --help Show this help message and exit. "
|
||||
}, "\r\n"), parser:get_help())
|
||||
end)
|
||||
|
||||
it("does not show default value when show_default == false", function()
|
||||
local parser = Parser "foo"
|
||||
parser:option "-o"
|
||||
:default "a.out"
|
||||
:show_default(false)
|
||||
parser:option "-p"
|
||||
:default "8080"
|
||||
:show_default(false)
|
||||
:description "Port. "
|
||||
|
||||
assert.equal(table.concat({
|
||||
"Usage: foo [-o <o>] [-p <p>] [-h]",
|
||||
"",
|
||||
"Options: ",
|
||||
" -o <o>",
|
||||
" -p <p> Port. ",
|
||||
" -h, --help Show this help message and exit. "
|
||||
}, "\r\n"), parser:get_help())
|
||||
end)
|
||||
|
||||
it("creates correct help message for commands", function()
|
||||
local parser = Parser "foo"
|
||||
parser:flag "-q" "--quiet"
|
||||
|
@@ -191,7 +191,8 @@ do -- Create classes with setters
|
||||
_maxargs = 1,
|
||||
_mincount = 1,
|
||||
_maxcount = 1,
|
||||
_defmode = "unused"
|
||||
_defmode = "unused",
|
||||
_show_default = true
|
||||
}, {
|
||||
name = typecheck.string "name",
|
||||
description = typecheck.string "description",
|
||||
@@ -200,7 +201,8 @@ do -- Create classes with setters
|
||||
default = typecheck.string "default",
|
||||
defmode = typecheck.string "defmode",
|
||||
convert = convert,
|
||||
argname = argname
|
||||
argname = argname,
|
||||
show_default = typecheck.boolean "show_default"
|
||||
})
|
||||
|
||||
Option = add_setters(Argument:extends {
|
||||
@@ -220,7 +222,8 @@ do -- Create classes with setters
|
||||
convert = convert,
|
||||
overwrite = typecheck.boolean "overwrite",
|
||||
action = typecheck["function"] "action",
|
||||
argname = argname
|
||||
argname = argname,
|
||||
show_default = typecheck.boolean "show_default"
|
||||
})
|
||||
end
|
||||
|
||||
@@ -329,7 +332,7 @@ function Command:_get_label()
|
||||
end
|
||||
|
||||
function Argument:_get_description()
|
||||
if self._default then
|
||||
if self._default and self._show_default then
|
||||
if self._description then
|
||||
return ("%s (default: %s)"):format(self._description, self._default)
|
||||
else
|
||||
|
Reference in New Issue
Block a user