Typer-Config

1.5.1 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `typer-config` using the `@use_yaml_config()` decorator. Create a `config.yml` file in the same directory with `name: Alice` and `age: 30`. Run the script with `python your_script.py` or `python your_script.py --config config.yml`. The parameters `name` and `age` will be populated from the config file, which can be overridden by command-line arguments.

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()

view raw JSON →