Typer-Config
Typer-Config is a collection of utilities that enable Typer CLI applications to utilize configuration files for setting command parameters. This is particularly useful for CLIs with numerous options and arguments, reducing the need to repeatedly type long commands. It supports various formats including YAML, JSON, TOML, and Dotenv. The current version is 1.5.1, and it typically follows the release cadence of its dependencies or as new features/fixes are required.
Warnings
- gotcha When using configuration decorators like `@use_yaml_config()`, they must be placed *after* the `@app.command()` decorator on the function definition. Incorrect placement will prevent the configuration from being loaded and applied correctly.
- gotcha When providing a custom loader function to `@use_config()`, ensure it gracefully handles cases where no config file path is provided or the file doesn't exist. Otherwise, the CLI might raise an error during `--help` output or when the `--config` option is omitted, leading to a poor user experience.
- gotcha Optional dependencies for specific config formats (e.g., `PyYAML` for YAML) are not automatically installed with `pip install typer-config`. If you intend to use a particular format, you must install the corresponding optional dependencies using `pip install typer-config[format_name]` or `typer-config[all]`.
Install
-
pip install typer-config -
pip install typer-config[all] -
pip install typer-config[yaml] -
pip install typer-config[toml] -
pip install typer-config[dotenv]
Imports
- use_yaml_config
from typer_config import use_yaml_config
- use_json_config
from typer_config import use_json_config
- use_toml_config
from typer_config import use_toml_config
- use_dotenv_config
from typer_config import use_dotenv_config
- use_config
from typer_config import use_config
Quickstart
import typer
from typer_config import use_yaml_config
from typing import Optional
app = typer.Typer()
@app.command()
@use_yaml_config()
def main(
name: str = "World",
age: Optional[int] = None,
config_file: Optional[str] = typer.Option(None, hidden=True) # Added for registry example clarity
):
"""A simple CLI app using a YAML config file."""
print(f"Hello, {name}!")
if age:
print(f"You are {age} years old.")
if config_file:
print(f"Using config file: {config_file}")
if __name__ == "__main__":
# Example config.yml content (create this file for testing):
# name: Alice
# age: 30
app()