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(
    name: str,
    greeting: Annotated[str, typer.Option()],
    suffix: Annotated[str, typer.Option()] = "!",
):
    typer.echo(f"{greeting}, {name}{suffix}")


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
name: World
greeting: Hello
suffix: "!"

And invoked with python:

Terminal
$ python simple_app.py --config config.yml
Hello, World!

$ python simple_app.py --config config.yml Alice
Hello, Alice!

$ python simple_app.py --config config.yml --greeting Hi
Hi, World!