10000 GH-87041: Fix incorrect indentation in argparse help by savannahostrowski · Pull Request #124230 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-87041: Fix incorrect indentation in argparse help #124230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-42875: fix incorrect indent of long sub-commands
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
  • Loading branch information
DiPaolo authored and savannahostrowski committed Sep 22, 2024
commit 74bfec5287600599b9cc75c08c586600975d8187
7 changes: 3 additions & 4 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,12 @@ def add_argument(self, action):

# find all invocations
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):
invocations.append(get_invocation(subaction))
invocation_lengths.append(len(get_invocation(subaction)) + self._current_indent)

# update the maximum item length
invocation_length = max(map(len, invocations))
action_length = invocation_length + self._current_indent
action_length = max(invocation_lengths)
self._action_max_length = max(self._action_max_length,
action_length)

Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4958,6 +4958,49 @@ def custom_type(string):
version = ''


class TestHelpUsageLongSubparserCommand(TestCase):
"""Test that long command in subparser is displayed correctly in help

The test was added for https://bugs.python.org/issue42875
"""

def setUp(self):
super().setUp()
self.main_program = os.path.basename(sys.argv[0])

def test_parent_help(self):
parent_parser = argparse.ArgumentParser(
add_help=False, formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=50))

main_parser = argparse.ArgumentParser(
formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=50))
cmd_subparsers = main_parser.add_subparsers(title="commands", metavar='CMD', help='command to use')

cmd_parser = cmd_subparsers.add_parser("add", help="add something", parents=[parent_parser])
cmd_parser.add_subparsers(title="action", dest="action_command")

cmd_parser2 = cmd_subparsers.add_parser("remove", help="remove something", parents=[parent_parser])
cmd_parser2.add_subparsers(title="action", dest="action_command")
7479

cmd_subparsers.add_parser("a-very-long-command", help="command that does something", parents=[parent_parser])

parser_help = main_parser.format_help()

progname = self.main_program
self.assertEqual(parser_help, textwrap.dedent('''\
usage: {}{}[-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
'''.format(progname, ' ' if progname else '')))


# =====================================
# Optional/Positional constructor tests
# =====================================
Expand Down
0