Skip to content

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:

fast →💬 Call it with the NAME CLI argumentpython main.py Camila
💬 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:

fast →python main.py Camila
Gutiérrez
Hello Camila Gutiérrez

restart ↻

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:

fast →python main.py
Old ProjectOld Project
Deleting project Old Project

python main.py
Old ProjectNew Spice
Error: the two entered values do not match

Old ProjectOld Project
Deleting project Old Project


restart ↻
You can ask questions about Typer. Try:
How can I terminate a program?
How to launch applications?
How to add help to CLI argument?