mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Show choices in help and usage messages
This commit is contained in:
@@ -86,6 +86,6 @@ The ``choices`` property can be used to restrict an argument to a set of choices
|
|||||||
|
|
||||||
.. code-block:: none
|
.. code-block:: none
|
||||||
|
|
||||||
Usage: script.lua [-h] <direction>
|
Usage: script.lua [-h] {north,south,east,west}
|
||||||
|
|
||||||
Error: argument 'direction' must be one of 'north', 'south', 'east', 'west'
|
Error: argument 'direction' must be one of 'north', 'south', 'east', 'west'
|
||||||
|
@@ -86,6 +86,34 @@ Options:
|
|||||||
--config <config>]], parser:get_help())
|
--config <config>]], parser:get_help())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("creates correct help message for arguments with choices", function()
|
||||||
|
local parser = Parser "foo"
|
||||||
|
parser:argument "move"
|
||||||
|
:choices {"rock", "paper", "scissors"}
|
||||||
|
|
||||||
|
assert.equal([[
|
||||||
|
Usage: foo [-h] {rock,paper,scissors}
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
{rock,paper,scissors}
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-h, --help Show this help message and exit.]], parser:get_help())
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("creates correct help message for options with argument choices", function()
|
||||||
|
local parser = Parser "foo"
|
||||||
|
parser:option "--format"
|
||||||
|
:choices {"short", "medium", "full"}
|
||||||
|
|
||||||
|
assert.equal([[
|
||||||
|
Usage: foo [-h] [--format {short,medium,full}]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-h, --help Show this help message and exit.
|
||||||
|
--format {short,medium,full}]], parser:get_help())
|
||||||
|
end)
|
||||||
|
|
||||||
it("adds margin for multiline descriptions", function()
|
it("adds margin for multiline descriptions", function()
|
||||||
local parser = Parser "foo"
|
local parser = Parser "foo"
|
||||||
parser:flag "-v"
|
parser:flag "-v"
|
||||||
|
@@ -89,6 +89,30 @@ Usage: foo <first> <second-and-third> <second-and-third>
|
|||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("creates correct usage message for arguments with choices", function()
|
||||||
|
local parser = Parser "foo"
|
||||||
|
:add_help(false)
|
||||||
|
parser:argument "move"
|
||||||
|
:choices {"rock", "paper", "scissors"}
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
[=[Usage: foo {rock,paper,scissors}]=],
|
||||||
|
parser:get_usage()
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("creates correct usage message for options with argument choices", function()
|
||||||
|
local parser = Parser "foo"
|
||||||
|
:add_help(false)
|
||||||
|
parser:option "--format"
|
||||||
|
:choices {"short", "medium", "full"}
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
[=[Usage: foo [--format {short,medium,full}]]=],
|
||||||
|
parser:get_usage()
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
it("creates correct usage message for commands", function()
|
it("creates correct usage message for commands", function()
|
||||||
local parser = Parser "foo"
|
local parser = Parser "foo"
|
||||||
:add_help(false)
|
:add_help(false)
|
||||||
|
@@ -513,18 +513,34 @@ function Argument:_get_argname(narg)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Argument:_get_choices_list()
|
||||||
|
return "{" .. table.concat(self._choices, ",") .. "}"
|
||||||
|
end
|
||||||
|
|
||||||
function Argument:_get_default_argname()
|
function Argument:_get_default_argname()
|
||||||
|
if self._choices then
|
||||||
|
return self:_get_choices_list()
|
||||||
|
else
|
||||||
return "<" .. self._name .. ">"
|
return "<" .. self._name .. ">"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Option:_get_default_argname()
|
function Option:_get_default_argname()
|
||||||
|
if self._choices then
|
||||||
|
return self:_get_choices_list()
|
||||||
|
else
|
||||||
return "<" .. self:_get_default_target() .. ">"
|
return "<" .. self:_get_default_target() .. ">"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Returns labels to be shown in the help message.
|
-- Returns labels to be shown in the help message.
|
||||||
function Argument:_get_label_lines()
|
function Argument:_get_label_lines()
|
||||||
|
if self._choices then
|
||||||
|
return {self:_get_choices_list()}
|
||||||
|
else
|
||||||
return {self._name}
|
return {self._name}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Option:_get_label_lines()
|
function Option:_get_label_lines()
|
||||||
local argument_list = self:_get_argument_list()
|
local argument_list = self:_get_argument_list()
|
||||||
|
Reference in New Issue
Block a user