How create an option with an optional argument (e.g. a flag with an optional value) #608
Replies: 3 comments
-
some hints? how to do this? @app.command()
def test_flag(flag: Optional[str] = typer.Option(None, is_flag=False, flag_value='default')):
if flag is not None:
print(f"{flag=}")
else:
print("flag unset") calling
what is the purpose of parameter |
Beta Was this translation helpful? Give feedback.
-
Any appropriate answers to this question? This kind of behavior is common in CLI programs, which is the reason why Click supports it. This kind of thing makes Typer inadequate for everything but the simplest CLI programs. Really sad given that the general concept of it is very good. |
Beta Was this translation helpful? Give feedback.
-
Also wondering how to do this. I've been trying different ways and none of them worked. |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
In
click
, it is possible to create an option with an optional value: https://click.palletsprojects.com/en/8.1.x/options/#optional-valueI'm wondering if it is possible to do this in
typer
?For the example given above, I would expect the following:
--opt-1
flag passed:None
--opt-1
flag is passed with no value:3.14
(default value)--opt-1
flag is passed with a value (99
):99
(value that was passed)Operating System
macOS
Operating System Details
No response
Typer Version
0.9.0
Python Version
3.10.10
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions