Strongly typed, zero-effort CLI interfaces
Project description
Documentation
•
pip install tyro
tyro
is a tool for building command-line
interfaces and configuration objects in Python.
Our core interface, tyro.cli()
, generates command-line interfaces from
type-annotated callables.
Brief walkthrough
To summarize how tyro.cli()
can be used, let's consider a script based on
argparse
. We define two inputs and print the sum:
"""Sum two numbers from argparse."""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--a", type=int, required=True)
parser.add_argument("--b", type=int, default=3)
args = parser.parse_args()
total = args.a + args.b
print(total)
This pattern is dramatically cleaner than manually parsing sys.argv
, but has
several issues: it lacks type checking and IDE support (consider: jumping to
definitions, finding references, docstrings, refactoring and renaming tools),
requires a significant amount of parsing-specific boilerplate, and becomes
difficult to manage for larger projects.
The basic goal of tyro.cli()
is to provide a wrapper for argparse
that
solves these issues.
(1) Command-line interfaces from functions.
We can write the same script as above using tyro.cli()
:
"""Sum two numbers by calling a function with tyro."""
import tyro
def add(a: int, b: int = 3) -> int:
return a + b
# Populate the inputs of add(), call it, then return the output.
total = tyro.cli(add)
print(total)
Or, more succinctly:
"""Sum two numbers by calling a function with tyro."""
import tyro
def add(a: int, b: int = 3) -> None:
print(a + b)
tyro.cli(add) # Returns `None`.
(2) Command-line interfaces from config objects.
A class in Python can be treated as a function that returns an instance. This makes it easy to populate explicit configuration structures:
"""Sum two numbers by instantiating a dataclass with tyro."""
from dataclasses import dataclass
import tyro
@dataclass
class Args:
a: int
b: int = 3
args = tyro.cli(Args)
print(args.a + args.b)
Unlike directly using argparse
, both the function-based and dataclass-based
approaches are compatible with static analysis; tab completion and type checking
will work out-of-the-box.
(3) Additional features.
These examples only scratch the surface of what's possible. tyro
aims to
support all reasonable type annotations, which can help us define things like
hierachical structures, enums, unions, variable-length inputs, and subcommands.
See documentation for examples.
In the wild
tyro
is still a new library, but being stress tested in several projects!
- nerfstudio-project/nerfstudio provides a set of tools for end-to-end training, testing, and rendering of neural radiance fields.
- Sea-Snell/JAXSeq is a library for distributed training of large language models in JAX.
- kevinzakka/obj2mjcf is an interface for processing composite Wavefront OBJ files for Mujoco.
- blurgyy/jaxngp is a CUDA-accelerated implementation of instant-ngp, implemented in JAX.
- NVIDIAGameWorks/kaolin-wisp is a PyTorch library for working with neural fields.
- openrlbenchmark/openrlbenchmark is a comprehensive collection of tracked experiments for reinforcement learning.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tyro-0.5.4.tar.gz
.
File metadata
- Download URL: tyro-0.5.4.tar.gz
- Upload date:
- Size: 95.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5df564393569ce2490d871a77f29f550f51a2d4bd2c4fe72f2d08a0421536f99 |
|
MD5 | ff3458802a860b1c126d9831db6e8a9a |
|
BLAKE2b-256 | 1392575e94e84098033d4257ee45aa7dc82c0305922ae72a3784efc018a55f2b |
File details
Details for the file tyro-0.5.4-py3-none-any.whl
.
File metadata
- Download URL: tyro-0.5.4-py3-none-any.whl
- Upload date:
- Size: 85.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c18ed9d79eddcf0f8634897df52e84ceb36930ea1c8e534683e0d7562cce98dd |
|
MD5 | 6c22f40dde1664fa66d685e9821e9f46 |
|
BLAKE2b-256 | 454d92edf8fcd27eb6bbd9945aae646cc6a898ba53bbb9d01b471f0aefb205b8 |