8000 ``argparse`` Prints options per flag name when only once is necessary · Issue #101599 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

argparse Prints options per flag name when only once is necessary #101599

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
Harrison-O opened this issue Feb 6, 2023 · 5 comments
Closed

argparse Prints options per flag name when only once is necessary #101599

Harrison-O opened this issue Feb 6, 2023 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@Harrison-O
Copy link
Harrison-O commented Feb 6, 2023

Bug report

When running a command line program with the --help/-h flag with argparse, if another flag has multiple names and choices to pick from, the options are printed multiple times instead of just once. For example, the program below:

import argparse

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument('-m', '--metric', choices=["accuracy", "precision", "recall"])
    parser.parse_args(["-h"])

Will print

usage: argparse_test.py [-h] [-m {accuracy,precision,recall}]

options:
  -h, --help            show this help message and exit
  -m {accuracy,precision,recall}, --metric {accuracy,precision,recall}

Notice that the flag choices are printed out twice, once for each flag name. This is redundant and negatively impacts readability. The program should output:

usage: argparse_test.py [-h] [-m {accuracy,precision,recall}]

options:
  -h, --help            show this help message and exit
  -m, --metric {accuracy,precision,recall}

Your environment

  • CPython versions tested on: 3.10.6
  • Operating system and architecture: Windows 11 with WSL 2

Linked PRs

@Harrison-O Harrison-O added the type-bug An unexpected behavior, bug, or error label Feb 6, 2023
@arhadthedev arhadthedev added type-feature A feature request or enhancement stdlib Python modules in the Lib dir and removed type-bug An unexpected behavior, bug, or error labels Feb 6, 2023
@arhadthedev
Copy link
Member

I agree that such a verbosity clutters the help output so it should be deduplicated into a description column. Also, there is no corresponding example in https://docs.python.org/3/library/argparse.html.

I mark it as a feature request, not a bug because such a verbosity explains things in direct, dumb, undesirable but working way.

@arhadthedev
Copy link
Member

@bethard, @bitdancer as prominent contributors into argparse according to git log.

@ferdnyc
Copy link
Contributor
ferdnyc commented Feb 8, 2023

Not just multiple choices, it does the same thing for metavar placeholders:

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
            '-m', '--metric',
            choices=["accuracy", "precision", "recall"],
            help="By which metric trick'd be thee met, Ric?")
    parser.add_argument(
            '-d', '--debug', metavar='DEBUG_LEVEL',
            action='store', type=int,
            help="Set the debug level (default: 0)")
    parser.parse_args(["-h"])
$ python3 ./ap.py --help
usage: ap.py [-h] [-m {accuracy,precision,recall}] [-d DEBUG_LEVEL]

options:
  -h, --help            show this help message and exit
  -m {accuracy,precision,recall}, --metric {accuracy,precision,recall}
                        By which metric trick'd be thee met, Ric?
  -d DEBUG_LEVEL, --debug DEBUG_LEVEL
                        Set the debug level (default: 0)

Points for consistency, I guess, but "-d DEBUG_LEVEL, --debug DEBUG_LEVEL" is pretty unnecessarily redundant as well.

Jokimax added a commit to Jokimax/cpython that referenced this issue Apr 8, 2023
serhiy-storchaka pushed a commit that referenced this issue Feb 2, 2024
If the option with argument has short and long names,
output argument only once, after the long name:

   -o, --option ARG    description

instead of

   -o ARG, --option ARG    description
@hugovk
Copy link
Member
hugovk commented Feb 3, 2024

And update the docs?

For example, at least:

  -v {0,1,2}, --verbosity {0,1,2}

https://docs.python.org/3.13/howto/argparse.html#combining-positional-and-optional-arguments

I didn't spot any in the library reference, but it needs checking too:

https://docs.python.org/3.13/library/argparse.html#module-argparse

cc @Jokimax

@hugovk hugovk reopened this Feb 3, 2024
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…103372)

If the option with argument has short and long names,
output argument only once, after the long name:

   -o, --option ARG    description

instead of

   -o ARG, --option ARG    description
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
…103372)

If the option with argument has short and long names,
output argument only once, after the long name:

   -o, --option ARG    description

instead of

   -o ARG, --option ARG    description
savannahostrowski added a commit to savannahostrowski/cpython that referenced this issue Sep 13, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 13, 2024
…utorial (pythonGH-124025)

(cherry picked from commit e5b0185)

Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 13, 2024
…utorial (pythonGH-124025)

(cherry picked from commit e5b0185)

Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
@hugovk
Copy link
Member
hugovk commented Sep 13, 2024

Thanks all!

@hugovk hugovk closed this as completed Sep 13, 2024
@github-project-automation github-project-automation bot moved this to Doc issues in Argparse issues Sep 13, 2024
Yhg1s pushed a commit that referenced this issue Sep 24, 2024
…tutorial (GH-124025) (#124037)

GH-101599: Update docs to remove redundant option in argparse tutorial (GH-124025)
(cherry picked from commit e5b0185)

Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
bittner added a commit to behave/behave-django that referenced this issue Apr 24, 2025
bittner added a commit to behave/behave-django that referenced this issue Apr 30, 2025
bittner added a commit to behave/behave-django that referenced this issue Apr 30, 2025
bittner added a commit to behave/behave-django that referenced this issue Apr 30, 2025
bittner added a commit to behave/behave-django that referenced this issue Apr 30, 2025
bittner added a commit to behave/behave-django that referenced this issue Jun 14, 2025
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-feature A feature request or enhancement
Projects
Status: Doc issues
Development

No branches or pull requests

5 participants
0