click-config-file

0.6.0 · active · verified Thu Apr 16

click-config-file is a Python library that provides configuration file support for Click applications. It simplifies adding configuration options to Click commands using a single decorator, handling sensible defaults and resolution order (CLI > Environment > Configuration file > Default). The current version is 0.6.0, with the latest release in April 2020, indicating a stable but infrequently updated project.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to add configuration file support to a Click command. By decorating with `configuration_option`, a `--config` option is automatically added (or an implicit config file is sought). The `cmd_name` and `config_file_name` arguments help locate the configuration file. Values from the config file are applied before CLI arguments.

import click
from click_config_file import configuration_option
import os

# Create a dummy config file for demonstration
# In a real scenario, this file would be created by the user
# e.g., 'my_app.cfg' in the application directory or specified via --config
with open("my_app.cfg", "w") as f:
    f.write('name="Universe"\n')

@click.command()
@click.option('--name', default='World', help='Who to greet.')
@configuration_option(
    cmd_name='my_app', 
    config_file_name='my_app.cfg'
)
def hello(name):
    """Simple program that greets NAME from config or CLI."""
    click.echo(f'Hello {name}!')

if __name__ == '__main__':
    print("--- Running with config file (name=Universe) ---")
    # Simulate running without CLI arguments to pick up config
    hello.main(args=[]) 

    print("\n--- Running with CLI argument (name=Multiverse) ---")
    # Simulate running with CLI arguments to override config
    hello.main(args=['--name', 'Multiverse'])
    
    # Clean up dummy config file
    os.remove("my_app.cfg")

view raw JSON →