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(
arg1: str,
opt1: Annotated[str, typer.Option()],
opt2: Annotated[str, typer.Option()] = "hello",
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"{opt1} {opt2} {arg1}")
if __name__ == "__main__":
app()
-
This package also provides
json_conf_callback
,toml_conf_callback
, anddotenv_conf_callback
for those file formats. -
You must use
is_eager=True
in 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
arg1: stuff
opt1: things
opt2: nothing
And invoked with python:
Terminal
$ python simple_app.py --config config.yml
things nothing stuff
$ python simple_app.py --config config.yml others
things nothing others
$ python simple_app.py --config config.yml --opt1 people
people nothing stuff