from typing import List, Optional
import typer
def main(device: Optional[List[str]] = typer.Option(None)):
if not device:
print("No provided devices")
raise typer.Abort()
for d in device:
print(f"Processing device: {d}")
if __name__ == "__main__":
typer.run(main)
And then it can be used like in:
$ ./my_cli --device 1 --device 2
Take a look at the documentation.
If the only input for the CLI is a list of devices, you can simplify it to using arguments instead. Then it could be used like:
$ ./my_cli 1 2
Answer from ericbn on Stack OverflowTyper
typer.tiangolo.com › tutorial › options › help
CLI Options with Help - Typer
Let's now do the same for CLI options: Python 3.10+ from typing import Annotated import typer app = typer.Typer() @app.command() def main( name: str, lastname: Annotated[str, typer.Option(help="Last name of person to greet.")] = "", formal: Annotated[bool, typer.Option(help="Say hi formally.")] = False, ): """ Say hi to 'name', optionally with a --lastname.
Typer
typer.tiangolo.com › tutorial › options › name
CLI Option Name - Typer
You can pass positional arguments to typer.Option() to define the CLI option name(s).
09:00
Build Python CLI Apps With Typer: Arguments, Options, and Commands ...
22:06
Building a Python Command-Line To-Do App With Typer: Setting Up ...
12:13
Getting Started with Command Line Interfaces using Python, Typer ...
Python - Typer - CLI Arguments
06:45
Python - Typer - CLI Options - YouTube
Typer
typer.tiangolo.com › tutorial › commands › options
Command CLI Options - Typer
In fact, each command can have different CLI arguments and CLI options: Python 3.10+ from typing import Annotated import typer app = typer.Typer() @app.command() def create(username: str): print(f"Creating user: {username}") @app.command() def delete( username: str, force: Annotated[ bool, typer.Option(prompt="Are you sure you want to delete the user?") ], ): if force: print(f"Deleting user: {username}") else: print("Operation cancelled") @app.command() def delete_all( force: Annotated[ bool, typer.Option(prompt="Are you sure you want to delete ALL users?") ], ): if force: print("Deleting all users") else: print("Operation cancelled") @app.command() def init(): print("Initializing user database") if __name__ == "__main__": app() 🤓 Other versions and variants ·
Typer
typer.tiangolo.com › reference › parameters
Parameters - Typer - Tiangolo.com
However, you can set them to be optional by defining a default value: ## Example ```python @app.command() def main(name: Annotated[str, typer.Argument()] = "World"): print(f"Hello {name}!") ``` Note how in this example, if `name` is not specified on the command line, the application will still execute normally and print "Hello World!".
Top answer 1 of 2
1
from typing import List, Optional
import typer
def main(device: Optional[List[str]] = typer.Option(None)):
if not device:
print("No provided devices")
raise typer.Abort()
for d in device:
print(f"Processing device: {d}")
if __name__ == "__main__":
typer.run(main)
And then it can be used like in:
$ ./my_cli --device 1 --device 2
Take a look at the documentation.
If the only input for the CLI is a list of devices, you can simplify it to using arguments instead. Then it could be used like:
$ ./my_cli 1 2
2 of 2
-2
from pathlib import Path
from typing import List
import typer
def main(files: List[Path], celebration: str):
for path in files:
if path.is_file():
print(f"This file exists: {path.name}")
print(celebration)
if __name__ == "__main__":
typer.run(main)
Calmcode
calmcode.io › course › typer › options
Calmcode - typer: Options
import typer from typing import List from pathlib import Path app = typer.Typer(name="demo", add_completion=False, help="This is a demo app.") @app.command() def talk(text: str = typer.Argument(..., help="The text to type."), repeat: int = typer.Option(1, help="Number of times to repeat."), loud: bool = typer.Option(False, is_flag=True)): """Talks some text below""" if loud: text = text.upper() for _ in range(repeat): print(text) if __name__ == "__main__": app() The talk command can be run in multiple ways now. python cli.py talk "hello there" python cli.py talk "hello there" --repeat 2 python cli.py talk "hello there" --loud python cli.py talk "hello there" --loud --repeat 3
Stack Overflow
stackoverflow.com › questions › 79454429 › typer-option-in-python
parameter passing - typer.Option in python - Stack Overflow
If you don't want to specify a value, just don't include the parameter flag when calling the command, e.g. python3 code.py test. Additionally, the name of the -t parameter in the function would be name not test · Copy@app.command( help = "Test command help text", no_args_is_help = True ) def test(name:str = typer.Option( None, "--test", "-t", help="help text" )): if name is None: name = 'all' print(name)
Typer
typer.tiangolo.com › tutorial › options › required
Required CLI Options - Typer
To make a CLI option required, you can put typer.Option() inside of Annotated and leave the parameter without a default value.
Typer
typer.tiangolo.com › tutorial › arguments › help
CLI Arguments with Help - Typer
import typer app = typer.Typer() @app.command() def main(name: str = typer.Argument(..., help="The name of the user to greet")): print(f"Hello {name}") if __name__ == "__main__": app() And it will be used in the automatic --help option: $ uv run python main.py --help // Check the section with Arguments below 🚀 Usage: main.py [OPTIONS] {name} Arguments: name The name of the user to greet [required] Options: --help Show this message and exit.
Typer
typer.tiangolo.com › tutorial › options
CLI Options - Typer
typer.Option() works very similarly to typer.Argument(), but has some extra features that we'll see next.
Medium
medium.com › @hayata-yamamoto › introduction-to-typer-a-powerful-cli-tool-for-python-developers-780c9fd87c19
Introduction to Typer: A Powerful CLI Tool for Python Developers | by Hayata Yamamoto | Medium
June 5, 2025 - Each section is commented with links to the relevant Typer documentation. import time from typing import Optional import typer from rich import print, progress, prompt app = typer.Typer() # Callback definition: https://typer.tiangolo.com/tutorial/commands/callback/ @app.callback(help="App callback definition") def callback( arg1: str = typer.Argument(..., help="Required argument for the entire app"), option1: bool = typer.Option(default=False, help="Optional argument for the entire app"), ) -> None: print(f"{arg1=}") print(f"{option1=}") # Required arguments: https://typer.tiangolo.com/tutorial/commands/arguments/… ·
Typer
typer.tiangolo.com › tutorial › options › prompt
CLI Option Prompt - Typer
from typing import Annotated import typer app = typer.Typer() @app.command() def main(name: str, lastname: Annotated[str, typer.Option(prompt=True)]): print(f"Hello {name} {lastname}") if __name__ == "__main__": app()
Typer
typer.tiangolo.com › tutorial › multiple-values › multiple-options
Multiple CLI Options - Typer
You can declare a CLI option that can be used multiple times, and then get all the values. For example, let's say you want to accept several users in a single execution. For this, use the standard Python list to declare it as a list of str: ... from typing import Annotated import typer app = typer.Typer() @app.command() def main(user: Annotated[list[str] | None, typer.Option()] = None): if not user: print(f"No provided users (raw input = {user})") raise typer.Abort() for u in user: print(f"Processing user: {u}") if __name__ == "__main__": app()
Blue Book
lyz-code.github.io › blue-book › typer
Typer - The Blue Book
You can make a CLI option work as a counter with the counter parameter: import typer def main(verbose: Annotated[int, typer.Option("--verbose", "-v", count=True)] = 0): print(f"Verbose level is {verbose}") if __name__ == "__main__": typer.run(main)
Typer
typer.tiangolo.com › tutorial › multiple-values › options-with-multiple-values
CLI Options with Multiple Values - Typer
You can also declare a CLI option that takes several values of different types. You can set the number of values and types to anything you want, but it has to be a fixed number of values. For this, use the standard Python tuple: Python 3.10+ from typing import Annotated import typer app = typer.Typer() @app.command() def main(user: Annotated[tuple[str, int, bool], typer.Option()] = (None, None, None)): username, coins, is_wizard = user if not username: print("No user provided") raise typer.Abort() print(f"The username {username} has {coins} coins") if is_wizard: print("And this user is a wizard!") if __name__ == "__main__": app() 🤓 Other versions and variants ·
Typer
typer.tiangolo.com › tutorial › parameter-types › enum
Enum - Choices - Typer
Python 3.10+ from enum import Enum from typing import Annotated import typer class Food(str, Enum): food_1 = "Eggs" food_2 = "Bacon" food_3 = "Cheese" app = typer.Typer() @app.command() def main(groceries: Annotated[list[Food], typer.Option()] = [Food.food_1, Food.food_3]): print(f"Buying groceries: {', '.join([f.value for f in groceries])}") if __name__ == "__main__": app() 🤓 Other versions and variants ·