Skip to main content

Strongly typed, zero-effort CLI interfaces

Project description


tyro logo

Documentation   •   pip install tyro

build mypy lint codecov codecov


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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tyro-0.4.1.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

tyro-0.4.1-py3-none-any.whl (61.1 kB view details)

Uploaded Python 3

File details

Details for the file tyro-0.4.1.tar.gz.

File metadata

  • Download URL: tyro-0.4.1.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.8.16 Linux/5.15.0-1034-azure

File hashes

Hashes for tyro-0.4.1.tar.gz
Algorithm Hash digest
SHA256 46ec5c77dc9d3506c1a2fdc06300ff4233dbdceeb72f601a507ed15763187cf9
MD5 2c9fb8161002a0cb5e5946388a7eb657
BLAKE2b-256 5a26bee543ff6e4382a9e216993874a9825e97e016f5d6fcd53cde267d3ce428

See more details on using hashes here.

File details

Details for the file tyro-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: tyro-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 61.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.8.16 Linux/5.15.0-1034-azure

File hashes

Hashes for tyro-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ea520af3e6130c7e516efc683afdd000047895f6b77d7e71fa58b5932427d26
MD5 3b89b90bbe62fc0cf15e3c01ac1884f3
BLAKE2b-256 c22419287a5fced48c55ce5a9b6b1153955061283a894d3eb1c154ab2e7a9f61

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page