Default Config File Example
This example loads a configuration from a default YAML file if --config
is not given.
An example typer app:
simple_app.py
from typing_extensions import Annotated
import typer
from typer_config.decorators import use_yaml_config # other formats available (1)
app = typer.Typer()
@app.command()
@use_yaml_config(default_value="config.yml")
def main(
arg1: str,
opt1: Annotated[str, typer.Option()],
opt2: Annotated[str, typer.Option()] = "hello",
):
typer.echo(f"{opt1} {opt2} {arg1}")
if __name__ == "__main__":
app()
- This package also provides
use_json_config
,use_toml_config
,use_ini_config
, anduse_dotenv_config
for those file formats.Note that since INI requires a top-level section
use_ini_config
requires a list of strings that express the path to the section you wish to use, e.g.@use_ini_config(["section", "subsection", ...])
.
With a config file:
config.yml
arg1: stuff
opt1: things
opt2: nothing
And invoked with python:
Terminal
$ python simple_app.py
things nothing stuff
$ python simple_app.py others
things nothing others
$ python simple_app.py --opt1 people
people nothing stuff
$ python simple_app.py --config other.yml
foo bar baz