Skip to content

bpo-42258: argparse: show choices once per argument #23143

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 2 commits into from

Conversation

mendelmaleh
Copy link

@mendelmaleh mendelmaleh commented Nov 4, 2020

When using more than one flag for an argument, it is redundant to have the choices more than once, since they are the same argument and have the same choices.
Showing it once means cleaner output, and often means that the other option lines are shorter, like in the test case.

sample code

import argparse

ap = argparse.ArgumentParser(allow_abbrev=False)
ap.add_argument('-c', '--choices', choices=['a', 'b', 'c'])
ap.parse_args()

previous output

usage: main.py [-h] [-c {a,b,c}]

optional arguments:
  -h, --help            show this help message and exit
  -c {a,b,c}, --choices {a,b,c}

new output

usage: main.py [-h] [-c {a,b,c}]

optional arguments:
  -h, --help            show this help message and exit
  -c, --choices {a,b,c}

https://bugs.python.org/issue42258

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@mendelmaleh

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@mendelmaleh mendelmaleh changed the title bpo-4225: argparse: show choices once per argument bpo-42258: argparse: show choices once per argument Nov 4, 2020
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Dec 16, 2020
Copy link
Contributor

@MaxwellDupre MaxwellDupre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works on test case plus
./python -m test -v test_argparse
Ran 1656 tests in 3.191s
OK
== Tests result: SUCCESS ==
1 test OK.
Total duration: 3.5 sec
Tests result: SUCCESS
cpython on  bpo-42258 [$?] via 🐍 v3.11.0a5+ took 4s

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Jul 31, 2022
Copy link
Contributor

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, but I don't like the way this changes the help:

usage: test.py [-h] [-x xyz]

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

to:

usage: test.py [-h] [-x xyz]

optional arguments:
  -h, --help   show this help message and exit
  -x, --x xyz

Since that makes it look like there isn't an argument when you use -x.

parser = argparse.ArgumentParser()
parser.add_argument("-x", "--x", metavar="xyz")
parser.parse_args()

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.

6 participants