CLI Option prompts with default=None don't default to None
patrickarmengol opened this issue · comments
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the Typer documentation, with the integrated search.
- I already searched in Google "How to X in Typer" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to Typer but to Click.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import Optional
import typer
app = typer.Typer()
@app.command()
def foo(
boo: Optional[int] = typer.Option(default=42, prompt=True),
too: Optional[int] = typer.Option(default=None, prompt=True),
moo: Optional[int] = typer.Option(default=None)
) -> None:
print(f'\nresult\n{boo}\n{too}\n{moo}')
if __name__ == '__main__':
app()
Description
When defining an CLI Option prompt, if the default is set to None
, the prompt requires an answer, and the answer needs to be of the non-None-type defined for the parameter.
Here's the output to the example code:
Boo [42]:
Too:
Too: asdf
Error: 'asdf' is not a valid integer.
Too: 3
result
42
3
None
42
is recognized as the default for boo
, so when hitting return boo
is set to 42
.
But too
does not display "None" as the default, and requires an answer of type int.
This is in contrast with both boo
and moo
, which revert to the defaults correctly when no input is given.
What I expect to happen is that too
is set to None when no input is given in the prompt.
Perhaps I am missing something. Anyone have any ideas how to get around this using the built in Typer prompt?
If this is currently a limitation of prompting, I would like to request an enhancement to allow None to be used as a default.
Operating System
Linux
Operating System Details
No response
Typer Version
0.7.0
Python Version
3.10.8
Additional Context
No response
Hmm, I initially thought this might not be click related since it deals with types and typer.Option, but now I see it just forwards to extends click.core.Option, which then click's prompt. I'll file again on that project.