CLI Option Prompt
It's also possible to, instead of just showing an error, ask for the missing value with prompt=True
:
import typer_cloup as typer
def main(name: str, lastname: str = typer.Option(..., prompt=True)):
typer.echo(f"Hello {name} {lastname}")
if __name__ == "__main__":
typer.run(main)
And then your program will ask the user for it in the terminal:
💬 Call it with the NAME CLI argumentpython main.py Camila
💬 It asks for the missing CLI option --lastnameGutiérrez
Hello Camila Gutiérrez
restart ↻
💬 It asks for the missing CLI option --lastnameGutiérrez
Hello Camila Gutiérrez
restart ↻
Customize the prompt¶
You can also set a custom prompt, passing the string that you want to use instead of just True
:
import typer_cloup as typer
def main(
name: str, lastname: str = typer.Option(..., prompt="Please tell me your last name")
):
typer.echo(f"Hello {name} {lastname}")
if __name__ == "__main__":
typer.run(main)
And then your program will ask for it using with your custom prompt:
Confirmation prompt¶
In some cases you could want to prompt for something and then ask the user to confirm it by typing it twice.
You can do it passing the parameter confirmation_prompt=True
.
Let's say it's a CLI app to delete a project:
import typer_cloup as typer
def main(project_name: str = typer.Option(..., prompt=True, confirmation_prompt=True)):
typer.echo(f"Deleting project {project_name}")
if __name__ == "__main__":
typer.run(main)
And it will prompt the user for a value and then for the confirmation: