typer-config
This is a collection of utilities to use configuration files to set parameters for a typer CLI. It is useful for typer commands with many options/arguments so you don't have to constantly rewrite long commands. This package was inspired by phha/click_config_file and prototyped in this issue. It allows you to set values for CLI parameters using a configuration file.
# Long commands like this:
$ my-typer-app --greeting Hello --suffix "!" World
# Can become this:
$ my-typer-app --config config.yml
Quickstart
You can use a decorator to quickly add a configuration parameter to your typer application:
import typer
from typer_config import use_yaml_config # other formats available (1)
app = typer.Typer()
@app.command()
@use_yaml_config() # MUST BE AFTER @app.command() (2)
def main(name: str, greeting: str = "Hello"): ...
if __name__ == "__main__":
app()
-
This package also provides
use_json_config,use_toml_config,use_ini_config, anduse_dotenv_configfor those file formats. You can also use your own loader function and the@use_config(loader_func)decorator.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", ...]). -
The
app.command()decorator registers the function object in a lookup table, so we must transform our command before registration.
Your typer command will now include a --config CONFIG_FILE option at the command line.
See Examples for more use cases.