Skip to content
Sarj Code Standards

prefer-typed-cli

Declare CLI arguments with typed Typer commands.

Why

A list of argv strings does not declare positional meaning, conversions or option validation. Typer derives those contracts from the command signature and rejects invalid arguments before calling application code.

Fix

Use typer.run on a command with explicit parameter types, or register typed functions with Typer.command/callback. Preserve option names, defaults, validation and exit behavior during migration.

Examples

Before — flagged Typer still needs parameter annotations
app/cli.py
import typer
def main(service):
deploy(service)
typer.run(main)
After — preferred Typer parses the declared parameter type
app/cli.py
import typer
def main(service: str, attempts: int = 3) -> None:
deploy(service, attempts)
if __name__ == "__main__":
typer.run(main)
Before — flagged Positional strings hide the CLI contract
app/cli.py
import sys
def main(arguments: list[str]) -> None:
deploy(arguments[0])
main(sys.argv[1:])
After — preferred The command declares each argument
app/cli.py
import typer
def main(service: str, attempts: int = 3) -> None:
deploy(service, attempts)
if __name__ == "__main__":
typer.run(main)