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

show_choices=False seems to have no effect #2356

Closed
zmoon opened this issue Sep 19, 2022 · 5 comments · Fixed by #2365
Closed

show_choices=False seems to have no effect #2356

zmoon opened this issue Sep 19, 2022 · 5 comments · Fixed by #2365
Milestone

Comments

@zmoon
Copy link

zmoon commented Sep 19, 2022

I noticed when using Typer that even if you pass show_choices=False, the choices still get shown in the help. It seems that this is related to Click.

Example:

import click

@click.command()
@click.option('-s', '--string-to-echo', type=click.Choice(['hello', 'world']), show_choices=False)
def echo(string):
    click.echo(string)

if __name__ == "__main__":
    echo()
Usage: ... [OPTIONS]

Options:
  -s, --string-to-echo [hello|world]
  --help                          Show this message and exit.

I guess it isn't documented what show_choices does for Option (if anything) in the non-prompt case, but I expected show_choices=False to disable showing the choices in the help.

Environment:

  • Python version: 3.9.13
  • Click version: 8.1.3
@nachovizzo

This comment was marked as off-topic.

@nachovizzo
Copy link

It's somehow strange that there is even a test for this case, but for some reason looks not to be failing

def test_choices_list_in_prompt(runner, monkeypatch):

@mrmups
Copy link
Contributor

mrmups commented Sep 30, 2022

It looks like the show_choices option 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 is used.

Adding prompt=True to the option in the original example and running it with no options will result in:

mups@docker:~$ demoecho
String to echo:

Where as setting show_choices=True would result in:

mups@docker:~$ demoecho
String to echo (hello, world):

The help text is showing the metavar of a Choice object. The Choice.get_metavar() method could be updated to to check for the show_choices param and either return None or an empty string.

The None option would cause the param type to be shown, which would look something like this:

mups@docker:~$ demoecho --help         
Usage: demoecho [OPTIONS]

Options:
  -s, --string-to-echo CHOICE  The string echoed back to stdout.
  --help                       Show this message and exit.

But I think the better option in this case would be to return an empty string which would look like this:

mups@docker:~$ demoecho --help         
Usage: demoecho [OPTIONS]

Options:
  -s, --string-to-echo   The string echoed back to stdout.
  --help                 Show this message and exit.

Which do you prefer?

@zmoon
Copy link
Author

zmoon commented Sep 30, 2022

Hi @mrmups . I think better would be -s, --string-to-echo TEXT or somesuch would be better actually. -s, --string-to-echo by itself kind of suggests boolean flag.

@mrmups
Copy link
Contributor

mrmups commented Oct 2, 2022

@zmoon Good point. In fact, it should probably just create a set of metavars based on the ParamType of each choice value.
This would result in this for the original example:

mups@docker:~$ demoecho --help         
Usage: demoecho [OPTIONS]

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

and if we added the choice 123:

mups@docker:~$ demoecho --help         
Usage: demoecho [OPTIONS]

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

@davidism davidism added this to the 8.2.0 milestone Jun 30, 2023
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 a pull request may close this issue.

4 participants