Skip to content

Simple YAML Example

This simple example uses a --config option to load a configuration from a YAML file.

An example typer app:

simple_app.py
from typing_extensions import Annotated

import typer
from typer_config.decorators import use_yaml_config  # other formats available (1)

app = typer.Typer()


@app.command()
@use_yaml_config()
def main(
    arg1: str,
    opt1: Annotated[str, typer.Option()],
    opt2: Annotated[str, typer.Option()] = "hello",
):
    typer.echo(f"{opt1} {opt2} {arg1}")


if __name__ == "__main__":
    app()

  1. This package also provides use_json_config, use_toml_config, and use_dotenv_config for those file formats.

With a config file:

config.yml
arg1: stuff
opt1: things
opt2: nothing

And invoked with python:

Terminal
$ python simple_app.py --config config.yml
things nothing stuff

$ python simple_app.py --config config.yml others
things nothing others

$ python simple_app.py --config config.yml --opt1 people
people nothing stuff