Skip to content

Fix metavar for Choice options when show_choices=False#2365

Merged
AndreasBackx merged 5 commits into
pallets:mainfrom
mrmups:fix-2356-show_choices-help-msg
Nov 2, 2024
Merged

Fix metavar for Choice options when show_choices=False#2365
AndreasBackx merged 5 commits into
pallets:mainfrom
mrmups:fix-2356-show_choices-help-msg

Conversation

@mrmups

@mrmups mrmups commented Oct 2, 2022

Copy link
Copy Markdown
Contributor

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

zmoon commented Oct 5, 2022

Copy link
Copy Markdown

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

@davidism

davidism commented Jun 30, 2023

Copy link
Copy Markdown
Member

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

zmoon commented Jun 30, 2023

Copy link
Copy Markdown

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

davidism commented Jun 30, 2023

Copy link
Copy Markdown
Member

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

mrmups commented Jul 11, 2023

Copy link
Copy Markdown
Contributor Author

I also prefer consistency. Reverting formatting change.

@mrmups mrmups changed the base branch from 8.1.x to main July 11, 2023 05:37
@AndreasBackx AndreasBackx mentioned this pull request Oct 20, 2024
34 tasks
@AndreasBackx AndreasBackx force-pushed the fix-2356-show_choices-help-msg branch from d42be38 to f7d98fe Compare November 2, 2024 22:41
@AndreasBackx AndreasBackx merged commit 1a4d8c1 into pallets:main Nov 2, 2024
@AndreasBackx

AndreasBackx commented Nov 2, 2024

Copy link
Copy Markdown
Collaborator

@mrmups thanks for the fix. Our apologies that it took this long! ❤️

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

show_choices=False seems to have no effect

4 participants