Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python click - allow a prompt to be empty

I'm using the click library for a CLI application. I have various options that the user can specify, and most of them have prompts turned on. However, even if an option isn't required, if you set click to prompt for the option, it won't accept an empty response (like just hitting enter). For example:

@click.option('-n', '--name', required=True, prompt=True)
@click.option('-d', '--description', required=False, prompt=True)

>>> myscript -n Joe
>>> Description: [Enter pressed]
>>> Description: [Enter pressed; click doesn't accept an empty parameter]

Is there a way to get around this, or would this require a feature request?

like image 653
garettmd Avatar asked Jan 05 '23 16:01

garettmd


1 Answers

when you add default="" then an empty string is also accepted:

@click.option('-d', '--description', prompt=True, default="")

Note that required is not a possible argument for option, at least according to the docs

like image 80
hansaplast Avatar answered Jan 12 '23 16:01

hansaplast