mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Improved help option behaviour
* Help option is only created once per parser, several :prepare() invocations do not create extra options. * In :add_help(foo), foo is passed to the help option overriding default name, see spec/help_spec.lua @ 24
This commit is contained in:
@@ -11,6 +11,27 @@ describe("tests related to help message generation", function()
|
||||
}, "\r\n"), parser:prepare():get_help())
|
||||
end)
|
||||
|
||||
it("does not create extra help options when :prepare is called several times", function()
|
||||
local parser = argparse.parser "foo"
|
||||
assert.equal(table.concat({
|
||||
"Usage: foo [-h]",
|
||||
"",
|
||||
"Options: ",
|
||||
" -h, --help Show this help message and exit. "
|
||||
}, "\r\n"), parser:prepare():prepare():get_help())
|
||||
end)
|
||||
|
||||
it("uses custom help option", function()
|
||||
local parser = argparse.parser "foo"
|
||||
:add_help {aliases = {"\\?"}}
|
||||
assert.equal(table.concat({
|
||||
"Usage: foo [\\?]",
|
||||
"",
|
||||
"Options: ",
|
||||
" \\? Show this help message and exit. "
|
||||
}, "\r\n"), parser:prepare():get_help())
|
||||
end)
|
||||
|
||||
it("creates correct help message for arguments", function()
|
||||
local parser = argparse.parser "foo"
|
||||
parser:argument "first"
|
||||
|
@@ -222,6 +222,7 @@ function Option:make_target()
|
||||
end
|
||||
|
||||
self._target = self._target or self._aliases[1]:sub(2)
|
||||
self._name = self._name or self._aliases[1]
|
||||
end
|
||||
|
||||
function Parser:argument(...)
|
||||
@@ -249,13 +250,20 @@ function Parser:command(...)
|
||||
end
|
||||
|
||||
function Parser:prepare()
|
||||
if self._add_help then
|
||||
self:flag "-h" "--help"
|
||||
:description "Show this help message and exit. "
|
||||
if self._add_help and not self._help_option then
|
||||
self._help_option = self:flag(self._add_help)
|
||||
:action(function()
|
||||
io.stdout:write(self:get_help() .. "\r\n")
|
||||
os.exit(0)
|
||||
end)
|
||||
|
||||
if #self._help_option._aliases == 0 then
|
||||
self._help_option "-h" "--help"
|
||||
end
|
||||
|
||||
if not self._help_option._description then
|
||||
self._help_option:description "Show this help message and exit. "
|
||||
end
|
||||
end
|
||||
|
||||
for _, elements in ipairs{self._arguments, self._options} do
|
||||
|
Reference in New Issue
Block a user