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(
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
use_json_config,use_toml_config,use_ini_config, anduse_dotenv_configfor those file formats.Note that since INI requires a top-level section
use_ini_configrequires 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
name: World
greeting: Hello
suffix: "!"
And invoked with python:
Terminal
$ python simple_app.py
Hello, World!
$ python simple_app.py Alice
Hello, Alice!
$ python simple_app.py --greeting Hi
Hi, World!
$ python simple_app.py --config other.yml
Hi, Alice!!