Argument Aliases#

tyro.conf.arg() can be used to attach aliases to arguments.

 1from typing_extensions import Annotated
 2
 3import tyro
 4
 5
 6def checkout(
 7    branch: Annotated[str, tyro.conf.arg(aliases=["-b"])],
 8) -> None:
 9    """Check out a branch."""
10    print(f"{branch=}")
11
12
13def commit(
14    message: Annotated[str, tyro.conf.arg(aliases=["-m"])],
15    all: Annotated[bool, tyro.conf.arg(aliases=["-a"])] = False,
16) -> None:
17    """Make a commit."""
18    print(f"{message=} {all=}")
19
20
21if __name__ == "__main__":
22    tyro.extras.subcommand_cli_from_dict(
23        {
24            "checkout": checkout,
25            "commit": commit,
26        }
27    )

python 04_additional/12_aliases.py --help
usage: 12_aliases.py [-h] {checkout,commit}

╭─ options ───────────────────────────────────────────────╮
│ -h, --help              show this help message and exit │
╰─────────────────────────────────────────────────────────╯
╭─ subcommands ───────────────────────────────────────────╮
│ {checkout,commit}                                       │
│     checkout            Check out a branch.             │
│     commit              Make a commit.                  │
╰─────────────────────────────────────────────────────────╯

python 04_additional/12_aliases.py commit --help
usage: 12_aliases.py commit [-h] --message STR [--all | --no-all | -a]

Make a commit.

╭─ options ───────────────────────────────────────────────╮
│ -h, --help              show this help message and exit │
│ --message STR, -m STR   (required)                      │
│ --all, --no-all, -a     (default: False)                │
╰─────────────────────────────────────────────────────────╯

python 04_additional/12_aliases.py commit --message hello --all
message='hello' all=True

python 04_additional/12_aliases.py commit -m hello -a
message='hello' all=True

python 04_additional/12_aliases.py checkout --help
usage: 12_aliases.py checkout [-h] --branch STR

Check out a branch.

╭─ options ───────────────────────────────────────────────╮
│ -h, --help              show this help message and exit │
│ --branch STR, -b STR    (required)                      │
╰─────────────────────────────────────────────────────────╯

python 04_additional/12_aliases.py checkout --branch main
branch='main'

python 04_additional/12_aliases.py checkout -b main
branch='main'