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.
pip install click-config-fileVerified import paths — ran on the pinned version, not inferred.
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.
Manually validate configuration keys against expected options within your Click command or implement a custom provider with validation logic. Consider using alternative libraries that offer strict validation if this is a critical requirement.
Always remember the precedence: CLI > Environment > Config File > Default. Design your application's configuration with this order in mind and clearly document it for users.
Be cautious when combining `configuration_option` with other custom Click decorators or options that modify the parsing process or rely on `is_eager=True`. Test combinations thoroughly. If conflicts arise, you may need to manually manage the `ctx.default_map` instead of relying solely on `configuration_option`.
Ensure 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.
Ensure 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]`).