Skip to content

INI Example

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

An example typer app:

simple_app.py
from typing_extensions import Annotated

import typer
from typer_config.decorators import use_ini_config

app = typer.Typer()


@app.command()
@use_ini_config(["section"])
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.ini
[section]
name = World
greeting = Hello
suffix = !

And invoked with python:

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

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

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