mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] GH-87041: Fix incorrect indentation in argparse help (GH-124230) (GH-124374)
In case of usage a long command along with max_help_position more than
the length of the command, the command's help was incorrectly started
on the new line.
(cherry picked from commit 7ee9921734
)
Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
Co-authored-by: Pavel Ditenbir <pavel.ditenbir@gmail.com>
This commit is contained in:
parent
62dcb8e013
commit
7cc773ba3d
3 changed files with 44 additions and 4 deletions
|
@ -263,13 +263,12 @@ class HelpFormatter(object):
|
||||||
|
|
||||||
# find all invocations
|
# find all invocations
|
||||||
get_invocation = self._format_action_invocation
|
get_invocation = self._format_action_invocation
|
||||||
invocations = [get_invocation(action)]
|
invocation_lengths = [len(get_invocation(action)) + self._current_indent]
|
||||||
for subaction in self._iter_indented_subactions(action):
|
for subaction in self._iter_indented_subactions(action):
|
||||||
invocations.append(get_invocation(subaction))
|
invocation_lengths.append(len(get_invocation(subaction)) + self._current_indent)
|
||||||
|
|
||||||
# update the maximum item length
|
# update the maximum item length
|
||||||
invocation_length = max(map(len, invocations))
|
action_length = max(invocation_lengths)
|
||||||
action_length = invocation_length + self._current_indent
|
|
||||||
self._action_max_length = max(self._action_max_length,
|
self._action_max_length = max(self._action_max_length,
|
||||||
action_length)
|
action_length)
|
||||||
|
|
||||||
|
|
|
@ -4762,6 +4762,46 @@ class TestHelpMetavarTypeFormatter(HelpTestCase):
|
||||||
version = ''
|
version = ''
|
||||||
|
|
||||||
|
|
||||||
|
class TestHelpUsageLongSubparserCommand(TestCase):
|
||||||
|
"""Test that subparser commands are formatted correctly in help"""
|
||||||
|
maxDiff = None
|
||||||
|
|
||||||
|
def test_parent_help(self):
|
||||||
|
def custom_formatter(prog):
|
||||||
|
return argparse.RawTextHelpFormatter(prog, max_help_position=50)
|
||||||
|
|
||||||
|
parent_parser = argparse.ArgumentParser(
|
||||||
|
prog='PROG',
|
||||||
|
formatter_class=custom_formatter
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd_subparsers = parent_parser.add_subparsers(title="commands",
|
||||||
|
metavar='CMD',
|
||||||
|
help='command to use')
|
||||||
|
cmd_subparsers.add_parser("add",
|
||||||
|
help="add something")
|
||||||
|
|
||||||
|
cmd_subparsers.add_parser("remove",
|
||||||
|
help="remove something")
|
||||||
|
|
||||||
|
cmd_subparsers.add_parser("a-very-long-command",
|
||||||
|
help="command that does something")
|
||||||
|
|
||||||
|
parser_help = parent_parser.format_help()
|
||||||
|
self.assertEqual(parser_help, textwrap.dedent('''\
|
||||||
|
usage: PROG [-h] CMD ...
|
||||||
|
|
||||||
|
options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
|
||||||
|
commands:
|
||||||
|
CMD command to use
|
||||||
|
add add something
|
||||||
|
remove remove something
|
||||||
|
a-very-long-command command that does something
|
||||||
|
'''))
|
||||||
|
|
||||||
|
|
||||||
# =====================================
|
# =====================================
|
||||||
# Optional/Positional constructor tests
|
# Optional/Positional constructor tests
|
||||||
# =====================================
|
# =====================================
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix a bug in :mod:`argparse` where lengthy subparser argument help is incorrectly indented.
|
Loading…
Add table
Add a link
Reference in a new issue