Skip to content

Dotenv Example

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

An example typer app:

simple_app.py
from typing_extensions import Annotated

import typer
from typer_config.decorators import use_dotenv_config

app = typer.Typer()


@app.command()
@use_dotenv_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()

With a config file:

config.env
name=World
greeting=Hello
suffix=!

And invoked with python:

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

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

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