click-config-file
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
-
TypeError: Attempted to convert a callback into a command twice.
cause This error typically occurs in Click when a function is decorated with both `@click.group()` and `@click.command()` or other conflicting decorators, which might happen inadvertently when adding `click-config-file`'s decorators.fixEnsure that a function is decorated as either a `click.command()` or a `click.group()`, but not both. Review your decorator stack and remove the conflicting decorator. -
Incorrect or unexpected parsing of options defined with `multiple=True` from a configuration file.
cause When a Click option is defined with `multiple=True` (e.g., `--numbers TEXT --numbers TEXT`), `click-config-file` (and Click itself) expects the corresponding value from the configuration file to be a list or tuple of values. Providing a single string may result in unexpected string splitting (e.g., 'abc' becoming ['a', 'b', 'c']) or errors.fixEnsure that values for options defined with `multiple=True` are explicitly formatted as a list or array within your configuration file. For `configobj` (the default provider), this typically means space-separated values on a single line, which `configobj` will parse into a list. For other providers (e.g., YAML with a custom provider), use standard list syntax (e.g., `numbers: [1, 2, 3]`).
Warnings
- gotcha Typographical errors or unsupported options in the configuration file are silently ignored by click-config-file. This can lead to unexpected behavior and makes debugging misconfigurations difficult, as no warnings or errors are raised.
- gotcha The hierarchy for option resolution is strictly defined: Command-Line Interface (CLI) arguments override Environment Variables, which override Configuration File settings, which in turn override default option values. Misunderstanding this order can lead to unexpected values being applied.
- gotcha The `configuration_option` decorator internally uses `is_eager=True`, which forces Click to invoke its callback before other options. If other Click options on the same command also set `is_eager=True`, it can lead to conflicts or unpredictable behavior regarding option parsing order.
Install
-
pip install click-config-file
Imports
- configuration_option
from click_config_file import configuration_option
Quickstart
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")