Python click - allow a prompt to be empty
Asked Answered
B

1

5

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?

Breeding answered 20/1, 2017 at 21:28 Comment(0)
P
11

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

Polysepalous answered 20/1, 2017 at 21:39 Comment(4)
Actually it is. The Option and Argument classes are both subclasses of the Parameter class. So that's not the issue.Breeding
the trick actually is the default="" which allows for an empty string. Did this work?Polysepalous
That did it. Sorry, I didn't even notice the default you were using earlier. Can you make a note of that in your response to make it obvious that's what is needed?Breeding
sure, I added a sentence at the topPolysepalous

© 2022 - 2024 — McMap. All rights reserved.