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

Fix metavar for Choice options when show_choices=False #2365

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mrmups
Copy link

@mrmups mrmups commented Oct 2, 2022

The show_choices parameter is intended to suppress choices from being displayed inline when click.prompt() is used. It's available to click.option() because an option can act like a prompt if the prompt=True parameter is used. However, this parameter does not prevent the choices from being used to create the Choice metavar displayed within the --help message.

This change addresses this issue by creating the metavar string using the ParamTypes of the choices instead of the values themselves.

Example script:

import click

@click.command()
@click.option(
    '-s', 
    '--string', 
    type=click.Choice(['hello', 'world']), 
    show_choices=False, 
    prompt=True, 
    help="This value echoed back to stdout."
)
def demoecho(string):
    click.echo(string)

if __name__ == "__main__":
    demoecho()

Output when show_choices=True:

$ demoecho --help         
Usage: demoecho [OPTIONS]

Options:
  -s, --string [hello|world]   This value echoed back to stdout.
  --help                       Show this message and exit.

Output when show_choices=False:

$ demoecho --help         
Usage: demoecho [OPTIONS]

Options:
  -s, --string [TEXT]    This value echoed back to stdout.
  --help                 Show this message and exit.

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@mrmups mrmups changed the title Fix 2356 show choices help msg Fix metavar for Choice options when show_choices=False Oct 2, 2022
@mrmups mrmups changed the base branch from main to 8.1.x October 2, 2022 03:17
@mrmups mrmups force-pushed the fix-2356-show_choices-help-msg branch from 03a23b6 to 8b9ae77 Compare October 5, 2022 03:07
@zmoon
Copy link

zmoon commented Oct 5, 2022

Could it be shown without the square brackets if there is only one type choice?

@davidism
Copy link
Member

davidism commented Jun 30, 2023

Could it be shown without the square brackets if there is only one type choice?

Choices are always in brackets regardless of number.

@davidism davidism added this to the 8.2.0 milestone Jun 30, 2023
@zmoon
Copy link

zmoon commented Jun 30, 2023

Choices are always in brackets regardless of number.

I noticed that, was merely making a suggestion.

In my personal opinion, I feel for type choices (not literal choices), it would look better without the brackets.

@davidism
Copy link
Member

davidism commented Jun 30, 2023

But in my personal opinion it's more consistent to always use brackets, so we're at a stalemate, and I get to break the stalemate. 😉

@mrmups
Copy link
Author

mrmups commented Jul 11, 2023

I also prefer consistency. Reverting formatting change.

@mrmups mrmups changed the base branch from 8.1.x to main July 11, 2023 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

show_choices=False seems to have no effect
3 participants