Configuration via typing.Annotated[]#

The tyro.conf module contains utilities that can be used to configure command-line interfaces beyond what is expressible via static type annotations.

Features here are supported, but generally unnecessary and should be used sparingly.

 1import dataclasses
 2
 3from typing_extensions import Annotated
 4
 5import tyro
 6
 7
 8@dataclasses.dataclass
 9class Args:
10    # A numeric field parsed as a positional argument.
11    positional: tyro.conf.Positional[int]
12
13    # A boolean field with flag conversion turned off.
14    boolean: tyro.conf.FlagConversionOff[bool] = False
15
16    # A numeric field that can't be changed via the CLI.
17    fixed: tyro.conf.Fixed[int] = 5
18
19    # A field with manually overridden properties.
20    manual: Annotated[
21        str,
22        tyro.conf.arg(
23            name="renamed",
24            metavar="STRING",
25            help="A field with manually overridden properties!",
26        ),
27    ] = "Hello"
28
29
30if __name__ == "__main__":
31    print(tyro.cli(Args))

python 04_additional/07_conf.py --help
usage: 07_conf.py [-h] [OPTIONS] INT

╭─ positional arguments ─────────────────────────────────────────────────────╮
│ INT                     A numeric field parsed as a positional argument.   │
│                         (required)                                         │
╰────────────────────────────────────────────────────────────────────────────╯
╭─ options ──────────────────────────────────────────────────────────────────╮
│ -h, --help              show this help message and exit                    │
│ --boolean {True,False}  A boolean field with flag conversion turned off.   │
│                         (default: False)                                   │
│ --fixed {fixed}         A numeric field that can't be changed via the CLI. │
│                         (fixed to: 5)                                      │
│ --renamed STRING        A field with manually overridden properties!       │
│                         (default: Hello)                                   │
╰────────────────────────────────────────────────────────────────────────────╯

python 04_additional/07_conf.py 5 --boolean True
Args(positional=5, boolean=True, fixed=5, manual='Hello')