Skip to content

Save Config Files

This example shows you how save the parameters of the invoked command to a configuration file using the @dump_config decorator which operates on Typer commands (requested in issue #25).

An example typer app:

simple_app.py
from typing_extensions import Annotated

import typer
from typer_config.decorators import (
    dump_json_config,  # other formats available (1)
    use_json_config,
)

app = typer.Typer()


@app.command()
@use_json_config()  # before dump decorator (2)
@dump_json_config("./dumped.json")
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 @dump_yaml_config and @dump_toml_config for those file formats.

  2. If you put @use_json_config before @dump_json_config, you will not capture the config parameter in your config dump. You probably want this behavior to avoid cascading config files.

And invoked with python:

Terminal
$ python simple_app.py --greeting Hello --suffix "!" World
Hello, World!

$ cat ./dumped.json
{"name": "World", "greeting": "Hello", "suffix": "!"}