click icon indicating copy to clipboard operation
click copied to clipboard

Fix metavar for Choice options when show_choices=False

Open mrmups opened this issue 2 years ago • 5 comments

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.
  • fixes #2356

Checklist:

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

mrmups avatar Oct 02 '22 03:10 mrmups

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

zmoon avatar Oct 05 '22 20:10 zmoon

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

Choices are always in brackets regardless of number.

davidism avatar Jun 30 '23 16:06 davidism

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.

zmoon avatar Jun 30 '23 17:06 zmoon

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. :wink:

davidism avatar Jun 30 '23 17:06 davidism

I also prefer consistency. Reverting formatting change.

mrmups avatar Jul 11 '23 05:07 mrmups