tyro._cli

Core public API.

Module Contents

tyro._cli.OutT
tyro._cli.cli(f: tyro._typing.TypeForm[OutT], *, prog: None | str = None, description: None | str = None, args: None | Sequence[str] = None, default: None | OutT = None, return_unknown_args: typing_extensions.Literal[False] = False, use_underscores: bool = False, console_outputs: bool = True, config: None | Sequence[tyro.conf._markers.Marker] = None) OutT[source]
tyro._cli.cli(f: tyro._typing.TypeForm[OutT], *, prog: None | str = None, description: None | str = None, args: None | Sequence[str] = None, default: None | OutT = None, return_unknown_args: typing_extensions.Literal[True], use_underscores: bool = False, console_outputs: bool = True, config: None | Sequence[tyro.conf._markers.Marker] = None) tuple[OutT, list[str]]
tyro._cli.cli(f: Callable[Ellipsis, OutT], *, prog: None | str = None, description: None | str = None, args: None | Sequence[str] = None, default: None = None, return_unknown_args: typing_extensions.Literal[False] = False, use_underscores: bool = False, console_outputs: bool = True, config: None | Sequence[tyro.conf._markers.Marker] = None) OutT
tyro._cli.cli(f: Callable[Ellipsis, OutT], *, prog: None | str = None, description: None | str = None, args: None | Sequence[str] = None, default: None = None, return_unknown_args: typing_extensions.Literal[True], use_underscores: bool = False, console_outputs: bool = True, config: None | Sequence[tyro.conf._markers.Marker] = None) tuple[OutT, list[str]]

Instantiate or call f, with inputs populated from an automatically generated CLI interface.

f should have type-annotated inputs, and can be a function or type. If f is a type, tyro.cli() returns an instance. If f is a function, tyro.cli() returns the output of calling the function.

Parameters:
  • f – Function or type.

  • prog – The name of the program printed in helptext. Mirrors argument from argparse.ArgumentParser().

  • description – Description text for the parser, displayed when the –help flag is passed in. If not specified, f’s docstring is used. Mirrors argument from argparse.ArgumentParser().

  • args – If set, parse arguments from a sequence of strings instead of the commandline. Mirrors argument from argparse.ArgumentParser.parse_args().

  • default – An instance of OutT to use for default values; supported if f is a type like a dataclass or dictionary, but not if f is a general callable like a function or standard class. Helpful for merging CLI arguments with values loaded from elsewhere. (for example, a config object loaded from a yaml file)

  • return_unknown_args – If True, return a tuple of the output of f and a list of unknown arguments. Mirrors the unknown arguments returned from argparse.ArgumentParser.parse_known_args().

  • use_underscores – If True, use underscores as a word delimeter instead of hyphens. This primarily impacts helptext; underscores and hyphens are treated equivalently when parsing happens. We default helptext to hyphens to follow the GNU style guide. https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html

  • console_outputs – If set to False, parsing errors and help messages will be supressed. This can be useful for distributed settings, where tyro.cli() is called from multiple workers but we only want console outputs from the main one.

  • config – Sequence of config marker objects, from tyro.conf. As an alternative to using them locally in annotations (:class:`tyro.conf.FlagConversionOff`[bool]), we can also pass in a sequence of them here to apply globally.

Returns:

The output of f(...) or an instance f. If f is a class, the two are equivalent. If return_unknown_args is True, returns a tuple of the output of f(...) and a list of unknown arguments.

tyro._cli.get_parser(f: tyro._typing.TypeForm[OutT], *, prog: None | str = None, description: None | str = None, default: None | OutT = None, use_underscores: bool = False, console_outputs: bool = True) tyro._argparse.ArgumentParser[source]
tyro._cli.get_parser(f: Callable[Ellipsis, OutT], *, prog: None | str = None, description: None | str = None, default: None | OutT = None, use_underscores: bool = False, console_outputs: bool = True) tyro._argparse.ArgumentParser

Get the argparse.ArgumentParser object generated under-the-hood by tyro.cli(). Useful for tools like sphinx-argparse, argcomplete, etc.

For tab completion, we recommend using tyro.cli()’s built-in --tyro-write-completion flag.