8000 GH-87041: Fix incorrect indentation in argparse help (GH-124230) · python/cpython@7ee9921 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7ee9921

Browse files
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. Co-authored-by: Pavel Ditenbir <pavel.ditenbir@gmail.com>
1 parent 9e55a02 commit 7ee9921

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
@@ -4960,6 +4960,46 @@ def custom_type(string):
49604960
version = ''
49614961

49624962

4963+
class TestHelpUsageLongSubparserCommand(TestCase):
4964+
"""Test that subparser commands are formatted correctly in help"""
4965+
maxDiff = None
4966+
4967+
def test_parent_help(self):
4968+
def custom_formatter(prog):
4969+
return argparse.RawTextHelpFormatter(prog, max_help_position=50)
4970+
4971+
parent_parser = argparse.ArgumentParser(
4972+
prog='PROG',
4973+
formatter_class=custom_formatter
4974+
)
4975+
4976+
cmd_subparsers = parent_parser.add_subparsers(title="commands",
4977+
metavar='CMD',
4978+
help='command to use')
4979+
cmd_subparsers.add_parser("add",
4980+
help="add something")
4981+
4982+
cmd_subparsers.add_parser("remove",
4983+
help="remove something")
4984+
4985+
cmd_subparsers.add_parser("a-very-long-command",
4986+
help="command that does something")
4987+
4988+
parser_help = parent_parser.format_help()
4989+
self.assertEqual(parser_help, textwrap.dedent('''\
4990+
usage: PROG [-h] CMD ...
4991+
4992+
options:
4993+
-h, --help show this help message and exit
4994+
4995+
commands:
4996+
CMD command to use
4997+
add add something
4998+
remove remove something
4999+
a-very-long-command command that does something
5000+
'''))
5001+
5002+
49635003
# =====================================
49645004
# Optional/Positional constructor tests
49655005
# =====================================
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)
0