Last modified: Aug 11, 2025 By Alexander Williams
Python Typer User Prompting Guide
Python Typer makes CLI app development easy. One key feature is user prompting. This guide covers prompts, confirmations, and hidden inputs.
Getting Started with Typer Prompts
Typer's prompt
function asks users for input. It's simple and customizable. Here's a basic example:
import typer
app = typer.Typer()
@app.command()
def greet():
name = typer.prompt("What's your name?")
typer.echo(f"Hello, {name}!")
if __name__ == "__main__":
app()
$ python app.py
What's your name? John
Hello, John!
The prompt function waits for user input. It's perfect for simple data collection.
Adding Default Values
You can provide default values in prompts. This improves user experience. The default appears in brackets.
@app.command()
def greet():
name = typer.prompt("What's your name?", default="Guest")
typer.echo(f"Hello, {name}!")
$ python app.py
What's your name? [Guest]:
Hello, Guest!
Users can press Enter to accept defaults. This saves time for common cases.
Confirmation Prompts
Typer's confirm
function asks yes/no questions. It returns True or False. Great for critical actions.
@app.command()
def delete_file():
if typer.confirm("Are you sure you want to delete?"):
typer.echo("File deleted!")
else:
typer.echo("Operation cancelled.")
$ python app.py
Are you sure you want to delete? [y/N]: y
File deleted!
Confirmation prompts prevent accidental actions. Always use them for destructive operations.
Hidden Input for Sensitive Data
Use typer.secret
for passwords or API keys. It hides input while typing.
@app.command()
def login():
password = typer.prompt("Enter password", hide_input=True)
typer.echo("Login attempt received.")
$ python app.py
Enter password: ********
Login attempt received.
For better security, use typer.secret
instead. It prevents password display in logs.
Advanced Prompt Customization
Typer prompts support type validation. Add type
parameter for automatic conversion.
@app.command()
def calculate():
age = typer.prompt("Your age", type=int)
typer.echo(f"In 10 years you'll be {age + 10}")
This ensures users enter valid numbers. Typer handles conversion automatically.
Combining with Other Typer Features
Prompts work well with subcommands and type hints. They create interactive CLI apps.
For complex apps, consider multiple commands. Each can have its own prompts.
Error Handling in Prompts
Typer automatically handles input errors. For custom validation, use loops or decorators.
@app.command()
def get_age():
while True:
age = typer.prompt("Age (18+)", type=int)
if age >= 18:
break
typer.echo("Must be 18 or older!")
typer.echo(f"Accepted age: {age}")
This ensures users enter valid ages. The prompt repeats until valid input.
Conclusion
Typer's prompting features make CLI apps interactive. Use prompt
for input, confirm
for yes/no, and secret
for sensitive data.
Combine these with other Typer features for powerful apps. Remember to validate input and provide defaults.
For installation help, see our Typer installation guide. Start building interactive CLI apps today!