Skip to content
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

HelpFormatter does not properly consider extra indentation from subparsers when printing arguments #119021

Open
mathgeniuszach opened this issue May 13, 2024 · 1 comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mathgeniuszach
Copy link

mathgeniuszach commented May 13, 2024

Bug report

Bug description:

Consider the following code,

import argparse
parser = argparse.ArgumentParser(prog="prog")
subparsers = parser.add_subparsers(metavar="mode")
a = subparsers.add_parser("option-a-here", help="helpful help")
b = subparsers.add_parser("option-b-here", help="super helpful help")
parser.parse_args()

Which produces this output, where even though there is more than enough room to print the descriptions after the arguments, it places them on the next line instead.

usage: prog [-h] mode ...

positional arguments:
  mode
    option-a-here
                 helpful help
    option-b-here
                 super helpful help

options:
  -h, --help     show this help message and exit

While with this code instead,

import argparse
parser = argparse.ArgumentParser(prog="prog")
parser.add_argument("--option-a", help="helpful help")
parser.add_argument("--option-b", help="super helpful help")
parser.parse_args()

The program correctly places them on the same line:

usage: prog [-h] [--option-a OPTION_A] [--option-b OPTION_B]

options:
  -h, --help           show this help message and exit
  --option-a OPTION_A  helpful help
  --option-b OPTION_B  super helpful help

I believe this is an error with HelpFormatter.add_argument(), where the indent/dedent coming from HelpFormatter._iter_indented_subactions() is neither evaluated by _SubParserAction._get_subactions() nor considered by self._current_indent.

CPython versions tested on:

3.11, 3.12, 3.13

Operating systems tested on:

Linux

Linked PRs

@mathgeniuszach mathgeniuszach added the type-bug An unexpected behavior, bug, or error label May 13, 2024
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label May 14, 2024
@olgarithms
Copy link
Contributor

I'd like to work on this!

olgarithms added a commit to olgarithms/cpython that referenced this issue May 27, 2024
Remove unintuitive indentation when formatting the help string for
subparsers' actions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: No status
Development

No branches or pull requests

3 participants