Tyro
Tyro is a Python library that generates command-line interfaces (CLIs) and configuration objects directly from standard Python type annotations, docstrings, and default values. It simplifies CLI creation by reducing boilerplate, improving IDE support, and enabling static type checking, acting as a lightweight wrapper around `argparse`. The current version is 1.0.12, and it follows a frequent, point-release cadence.
Warnings
- gotcha Tyro's parsing logic for complex types like `Union` and `List` (especially with `nargs`) or positional arguments can sometimes behave differently from standard `argparse`. Users migrating complex `argparse` setups should thoroughly test argument parsing, particularly with edge cases.
- gotcha When working with nested dataclasses or `Union` types that define subcommands, `tyro` by default prefixes arguments (e.g., `parent.child.arg`). This can lead to very long argument names. While explicit, it might not be desired for all CLIs.
- deprecated The `@tyro.conf.configure` decorator is generally discouraged for applying global configuration markers. The preferred approach for global options is to pass the `config=` argument directly to `tyro.cli()`.
Install
-
pip install tyro
Imports
- cli
import tyro # ... tyro.cli(...)
Quickstart
from dataclasses import dataclass
import tyro
@dataclass
class Args:
"""Configure a greeting."""
name: str
greet: str = "Hi" # Default value
if __name__ == "__main__":
# From command line:
# python your_script.py --name World
# Or: python your_script.py --name World --greet Hello
args = tyro.cli(Args)
print(f"{args.greet}, {args.name}!")