Explicit Configuration Parameter
Instead of using the @use_config() decorator, you can explicitly add config to your typer command.
However, you must include is_eager=True.
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.callbacks import yaml_conf_callback # other formats available (1)
app = typer.Typer()
@app.command()
def main(
name: str,
greeting: Annotated[str, typer.Option()],
suffix: Annotated[str, typer.Option()] = "!",
config: Annotated[
str,
typer.Option(
callback=yaml_conf_callback,
is_eager=True, # THIS IS REALLY IMPORTANT (2)
),
] = "",
):
# possibly do something with config
typer.echo(f"{greeting}, {name}{suffix}")
if __name__ == "__main__":
app()
-
This package also provides
json_conf_callback,toml_conf_callback, anddotenv_conf_callbackfor those file formats. -
You must use
is_eager=Truein the parameter definition because that will cause it to be processed first. If you don't useis_eager, then your parameter values will depend on the order in which they were processed (read: unpredictably).
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!