Skip to content

gh-119021: Fix subparsers' indentation in argparse module #119616

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

Closed
wants to merge 3 commits into from

Conversation

olgarithms
Copy link
Contributor

@olgarithms olgarithms commented May 27, 2024

Remove unintuitive indentation when formatting the help string for subparsers' actions.

This is one proposal to fix the current problem of correct indentation in subparsers' actions.
The issue occurs when the parameter metavar is used when creating a subparser.

Test cases

Without the metavar parameter

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--a", help="parser helpful text")

subparsers = parser.add_subparsers(title="subparser title")
a = subparsers.add_parser("subparser parser A", help="subparser A help")
b = subparsers.add_parser("subparser parser B", help="subparser B help")

parser.print_help()

The below is the correct behaviour which remains unchanged.

usage: prog [-h] [--a A] {subparser parser A,subparser_parser B} ...

options:
  -h, --help            show this help message and exit
  --a A                 parser helpful text

subparser title:
  {subparser parser A,subparser parser B}
  subparser parser A    subparser A help
  subparser parser B    subparser B help

With the metavar parameter

import argparse

parser = argparse.ArgumentParser(prog="prog")
parser.add_argument("--a", help="parser helpful text")

subparsers = parser.add_subparsers(title="subparser title", metavar="meta")
a = subparsers.add_parser("subparser parser A", help="subparser A help")
b = subparsers.add_parser("subparser parser B", help="subparser B help")

parser.print_help()

Old behaviour

usage: prog [-h] [--a A] meta ...

options:
  -h, --help          show this help message and exit
  --a A               parser helpful text

subparser title:
  meta
    subparser parser A
                      subparser A help
    subparser parser B
                      subparser B help

New behaviour

usage: prog [-h] [--a A] meta ...

options:
  -h, --help          show this help message and exit
  --a A               parser helpful text

subparser title:
  meta
  subparser parser A  subparser A help
  subparser parser B  subparser B help

Effect in other cases

As you can see from the modified test cases, some cases will have an indentation level removed.
The argparse module is quite large and a lot of objects come with a few different options. I am not an expert on the module, but given there is good coverage and the modified cases being so few (plus the code changes being minimal!), I think this is a valuable proposal.

cc. @ericvsmith , @lysnikolaou

Remove unintuitive indentation when formatting the help string for
subparsers' actions.
@picnixz
Copy link
Member

picnixz commented Jun 6, 2024

I think the "meta" should still be indented separately otherwise, you could assume that "meta" is a valid subparser, which is not the case; it's just the name of the variable (only visually!). In practice you could have

Possible Commands:
  COMMAND
  purge  	help for 'purge' command
  create	help for 'create' command

But I think having

Possible Commands:
  COMMAND
  	purge  	help for 'purge' command
  	create	help for 'create' command

would still be clearer. Keep in mind that this change may affect command-line helps of real-world applications which may not what they would be glad for. I nonetheless agree that the help string can be put on the same line as the command name rather than on a new line if there is sufficient space.

@serhiy-storchaka
Copy link
Member

Thank you for your contribution @olgarithms, but this issue was fixed in other way by #124230.

@olgarithms olgarithms deleted the fix-issue-119021 branch September 24, 2024 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants