Last modified: Aug 11, 2025 By Alexander Williams
Python Typer: Arguments vs Options
Python Typer is a library for building CLI apps. It simplifies command-line interfaces. It uses arguments and options for user input.
Understanding the difference is key. This guide explains both with examples.
What Are Typer Arguments?
Arguments are positional. They must be provided in order. No flags are used.
They are required by default. But you can make them optional.
Here’s an example using typer.Argument()
:
import typer
def greet(name: str = typer.Argument("World")):
typer.echo(f"Hello, {name}!")
if __name__ == "__main__":
typer.run(greet)
Run it with a name or without:
# With argument
$ python app.py John
Hello, John!
# Without argument (uses default)
$ python app.py
Hello, World!
What Are Typer Options?
Options use flags like --name
. They are optional by default.
They provide more flexibility. Users can specify them in any order.
Here’s an example using typer.Option()
:
import typer
def greet(name: str = typer.Option("World", "--name", "-n")):
typer.echo(f"Hello, {name}!")
if __name__ == "__main__":
typer.run(greet)
Run it with or without the flag:
# With option
$ python app.py --name John
Hello, John!
# Without option (uses default)
$ python app.py
Hello, World!
Key Differences
Arguments are positional. They don’t use flags. They are required unless a default is set.
Options use flags. They are optional by default. They can be in any order.
Arguments are simpler for required inputs. Options are better for optional or multiple inputs.
When to Use Arguments vs Options
Use arguments for required inputs. For example, a filename or command.
Use options for optional settings. For example, verbosity or output format.
In multi-command apps, arguments often define subcommands.
Advanced Usage
Typer allows combining both. Here’s an example:
import typer
def greet(
name: str = typer.Argument("World"),
formal: bool = typer.Option(False, "--formal", "-f")
):
if formal:
typer.echo(f"Greetings, {name}!")
else:
typer.echo(f"Hello, {name}!")
if __name__ == "__main__":
typer.run(greet)
Run it with both:
$ python app.py John --formal
Greetings, John!
Conclusion
Arguments and options serve different purposes in Typer. Arguments are positional and often required. Options use flags and are flexible.
Choose based on your CLI app’s needs. For more, see how to install Typer.
Mastering both will help you build better CLI tools.