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()
-
This package also provides
@dump_yaml_configand@dump_toml_configfor those file formats. -
If you put
@use_json_configbefore@dump_json_config, you will not capture theconfigparameter 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": "!"}