click
click copied to clipboard
Fix metavar for Choice options when show_choices=False
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
andtox
, no tests failed.
Could it be shown without the square brackets if there is only one type choice?
Could it be shown without the square brackets if there is only one type choice?
Choices are always in brackets regardless of number.
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.
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:
I also prefer consistency. Reverting formatting change.