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
from typing_extensions import Annotated


def main(name: str, lastname: Annotated[str, typer.Option(prompt=True)]):
    print(f"Hello {name} {lastname}")


if __name__ == "__main__":
    typer.run(main)

Tip

Prefer to use the Annotated version if possible.

import typer


def main(name: str, lastname: str = typer.Option(..., prompt=True)):
    print(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 argument
$ python main.py Camila

// It asks for the missing CLI option --lastname
# Lastname: $ Gutiérrez

Hello Camila Gutiérrez

Customize the prompt

You can also set a custom prompt, passing the string that you want to use instead of just True:

import typer
from typing_extensions import Annotated


def main(
    name: str,
    lastname: Annotated[str, typer.Option(prompt="Please tell me your last name")],
):
    print(f"Hello {name} {lastname}")


if __name__ == "__main__":
    typer.run(main)

Tip

Prefer to use the Annotated version if possible.

import typer


def main(
    name: str, lastname: str = typer.Option(..., prompt="Please tell me your last name")
):
    print(f"Hello {name} {lastname}")


if __name__ == "__main__":
    typer.run(main)

And then your program will ask for it using with your custom prompt:

// Call it with the NAME CLI argument
$ python main.py Camila

// It uses the custom prompt
# Please tell me your last name: $ Gutiérrez

Hello Camila Gutiérrez

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
from typing_extensions import Annotated


def main(
    project_name: Annotated[str, typer.Option(prompt=True, confirmation_prompt=True)],
):
    print(f"Deleting project {project_name}")


if __name__ == "__main__":
    typer.run(main)

Tip

Prefer to use the Annotated version if possible.

import typer


def main(project_name: str = typer.Option(..., prompt=True, confirmation_prompt=True)):
    print(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:

$ python main.py

// Your app will first prompt for the project name, and then for the confirmation
# Project name: $ Old Project
# Repeat for confirmation: $ Old Project

Deleting project Old Project

// If the user doesn't type the same, receives an error and a new prompt
$ python main.py

# Project name: $ Old Project
# Repeat for confirmation: $ New Spice

Error: The two entered values do not match

# Project name: $ Old Project
# Repeat for confirmation: $ Old Project

Deleting project Old Project

// Now it works 🎉