Skip to content

Commit 8d3a0fe

Browse files
miss-islingtonsavannahostrowskiDiPaolo
authored
[3.13] GH-87041: Fix incorrect indentation in argparse help (GH-124230) (#124373)
GH-87041: Fix incorrect indentation in argparse help (GH-124230) 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 67aa68f commit 8d3a0fe

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
@@ -261,13 +261,12 @@ def add_argument(self, action):
261261

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

268268
# update the maximum item length
269-
invocation_length = max(map(len, invocations))
270-
action_length = invocation_length + self._current_indent
269+
action_length = max(invocation_lengths)
271270
self._action_max_length = max(self._action_max_length,
272271
action_length)
273272

Lib/test/test_argparse.py

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

49834983

4984+
class TestHelpUsageLongSubparserCommand(TestCase):
4985+
"""Test that subparser commands are formatted correctly in help"""
4986+
maxDiff = None
4987+
4988+
def test_parent_help(self):
4989+
def custom_formatter(prog):
4990+
return argparse.RawTextHelpFormatter(prog, max_help_position=50)
4991+
4992+
parent_parser = argparse.ArgumentParser(
4993+
prog='PROG',
4994+
formatter_class=custom_formatter
4995+
)
4996+
4997+
cmd_subparsers = parent_parser.add_subparsers(title="commands",
4998+
metavar='CMD',
4999+
help='command to use')
5000+
cmd_subparsers.add_parser("add",
5001+
help="add something")
5002+
5003+
cmd_subparsers.add_parser("remove",
5004+
help="remove something")
5005+
5006+
cmd_subparsers.add_parser("a-very-long-command",
5007+
help="command that does something")
5008+
5009+
parser_help = parent_parser.format_help()
5010+
self.assertEqual(parser_help, textwrap.dedent('''\
5011+
usage: PROG [-h] CMD ...
5012+
5013+
options:
5014+
-h, --help show this help message and exit
5015+
5016+
commands:
5017+
CMD command to use
5018+
add add something
5019+
remove remove something
5020+
a-very-long-command command that does something
5021+
'''))
5022+
5023+
49845024
# =====================================
49855025
# Optional/Positional constructor tests
49865026
# =====================================
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)