CLI Arguments with DefaultΒΆ
We can also use the same typer.Argument()
to set a default value.
That way the CLI argument will be optional and also have a default value.
An optional CLI argument with a defaultΒΆ
We can also use typer.Argument()
to make a CLI argument have a default value other than None
:
import typer
from typing_extensions import Annotated
def main(name: Annotated[str, typer.Argument()] = "Wade Wilson"):
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
π€ Other versions and variants
Tip
Prefer to use the Annotated
version if possible.
import typer
def main(name: str = typer.Argument("Wade Wilson")):
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
Tip
Because now the value will be a str
passed by the user or the default value of "Wade Wilson"
which is also a str
, we know the value will never be None
, so we don't have to (and shouldn't) use Optional[str]
.
Have in mind that the Optional[something]
tells Python that a value "could be None
". But the use of Optional
doesn't affect Typer in any way, e.g. it doesn't tell Typer if a value is required or not.
Check it:
Dynamic default valueΒΆ
And we can even make the default value be dynamically generated by passing a function as the default_factory
argument:
import random
import typer
from typing_extensions import Annotated
def get_name():
return random.choice(["Deadpool", "Rick", "Morty", "Hiro"])
def main(name: Annotated[str, typer.Argument(default_factory=get_name)]):
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
π€ Other versions and variants
Tip
Prefer to use the Annotated
version if possible.
import random
import typer
def get_name():
return random.choice(["Deadpool", "Rick", "Morty", "Hiro"])
def main(name: str = typer.Argument(default_factory=get_name)):
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
In this case, we created the function get_name
that will just return a random str
each time.
And we pass it as the first function argument to typer.Argument()
.
Tip
The word "factory" in default_factory
is just a fancy way of saying "function that will create the default value".
Check it: