Skip to content

Commit 7cc773b

Browse files
miss-islingtonsavannahostrowskiDiPaolo
authored
[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 7ee9921) Co-authored-by: Savannah Ostrowski <[email protected]> Co-authored-by: Pavel Ditenbir <[email protected]>
1 parent 62dcb8e commit 7cc773b

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

Lib/argparse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,12 @@ def add_argument(self, action):
263263

264264
# find all invocations
265265
get_invocation = self._format_action_invocation
266-
invocations = [get_invocation(action)]
266+
invocation_lengths = [len(get_invocation(action)) + self._current_indent]
267267
for subaction in self._iter_indented_subactions(action):
268-
invocations.append(get_invocation(subaction))
268+
invocation_lengths.append(len(get_invocation(subaction)) + self._current_indent)
269269

270270
# update the maximum item length
271-
invocation_length = max(map(len, invocations))
272-
action_length = invocation_length + self._current_indent
271+
action_length = max(invocation_lengths)
273272
self._action_max_length = max(self._action_max_length,
274273
action_length)
275274

Lib/test/test_argparse.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,6 +4762,46 @@ def custom_type(string):
47624762
version = ''
47634763

47644764

4765+
class TestHelpUsageLongSubparserCommand(TestCase):
4766+
"""Test that subparser commands are formatted correctly in help"""
4767+
maxDiff = None
4768+
4769+
def test_parent_help(self):
4770+
def custom_formatter(prog):
4771+
return argparse.RawTextHelpFormatter(prog, max_help_position=50)
4772+
4773+
parent_parser = argparse.ArgumentParser(
4774+
prog='PROG',
4775+
formatter_class=custom_formatter
4776+
)
4777+
4778+
cmd_subparsers = parent_parser.add_subparsers(title="commands",
4779+
metavar='CMD',
4780+
help='command to use')
4781+
cmd_subparsers.add_parser("add",
4782+
help="add something")
4783+
4784+
cmd_subparsers.add_parser("remove",
4785+
help="remove something")
4786+
4787+
cmd_subparsers.add_parser("a-very-long-command",
4788+
help="command that does something")
4789+
4790+
parser_help = parent_parser.format_help()
4791+
self.assertEqual(parser_help, textwrap.dedent('''\
4792+
usage: PROG [-h] CMD ...
4793+
4794+
options:
4795+
-h, --help show this help message and exit
4796+
4797+
commands:
4798+
CMD command to use
4799+
add add something
4800+
remove remove something
4801+
a-very-long-command command that does something
4802+
'''))
4803+
4804+
47654805
# =====================================
47664806
# Optional/Positional constructor tests
47674807
# =====================================
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in :mod:`argparse` where lengthy subparser argument help is incorrectly indented.

0 commit comments

Comments
 (0)