Multi-value Arguments#

Arguments of both fixed and variable lengths can be annotated with standard Python collection types. For Python 3.7 and 3.8, we can use either from __future__ import annotations ortyping.List[T], typing.Tuple[T1, T2], etc.

 1import dataclasses
 2import pathlib
 3
 4import tyro
 5
 6
 7@dataclasses.dataclass(frozen=True)
 8class TrainConfig:
 9    # Example of a variable-length tuple. `list[T]`, `set[T]`,
10    # `dict[K, V]`, etc are supported as well.
11    dataset_sources: tuple[pathlib.Path, ...]
12    """Paths to load training data from. This can be multiple!"""
13
14    # Fixed-length tuples are also okay.
15    image_dimensions: tuple[int, int] = (32, 32)
16    """Height and width of some image data."""
17
18
19if __name__ == "__main__":
20    config = tyro.cli(TrainConfig)
21    print(config)

python 01_basics/03_collections.py --help
usage: 03_collections.py [-h] --dataset-sources [PATH [PATH ...]]
                         [--image-dimensions INT INT]

╭─ options ──────────────────────────────────────────────────────────────╮
│ -h, --help                                                             │
│     show this help message and exit                                    │
│ --dataset-sources [PATH [PATH ...]]                                    │
│     Paths to load training data from. This can be multiple! (required) │
│ --image-dimensions INT INT                                             │
│     Height and width of some image data. (default: 32 32)              │
╰────────────────────────────────────────────────────────────────────────╯

python 01_basics/03_collections.py --dataset-sources ./data --image-dimensions 16 16
TrainConfig(dataset_sources=(PosixPath('data'),), image_dimensions=(16, 16))

python 01_basics/03_collections.py --dataset-sources ./data
TrainConfig(dataset_sources=(PosixPath('data'),), image_dimensions=(32, 32))