Easily add configuration file support to your Click applications by adding a single no-arguments decorator.
Basic usage
click-config-file is designed to be a usable by simply adding the appropriate decorator to your command without having to supply any mandatory arguments. It comes with a set of sensible defaults that should just work for most cases.
Given this application:
@click.command()
@click.option('--name', default='World', help='Who to greet.')
@click_config_file.configuration_option()
def hello(name):
click.echo('Hello {}!'.format(name))
Running hello --help will give you this:
Usage: hello [OPTIONS] Options: --name TEXT Who to greet. --config PATH Read configuration from PATH. --help Show this message and exit.
If the configuration file does not exist, running hello will do what you expect:
Hello World!
With this configuration file:
name="Universe"
Calling hello will also do what you expect:
Hello Universe!
Calling hello --name Multiverse will override the configuration file setting, as it should:
Hello Multiverse!
The default name for the configuration file option is --config. If no --config option is provided either via CLI or an environment variable, the module will search for a file named config in the appropriate directory for your OS.
Command line and environment options will override the configuration file options. Configuration file options override default options. So the resolution order for a given option is: CLI > Environment > Configuration file > Default.
Supported file formats
By default click-config-file supports files formatted according to Configobj’s unrepr mode.
You can add support for additional configuration providers by setting the x`provider keyword argument. This argument expects a callable that will take the configuration file path and command name as arguments and returns a dictionary with the provided configuration options.
The command name is passed in order to allow for a shared configuration file divided by sections for each command.
For example, this will read the configuration options from a shared JSON file:
def myprovider(file_path, cmd_name):
with open(file_path) as config_data:
return json.load(config_data)[cmd_name]
@click.command()
@click.option('--name', default='World')
@click_config_file.configuration_option(provider=myprovider)
def hello(name):
click.echo('Hello {}!'.format(name))
Installation
pip install click-config-file
Why?
There are several existing implementations of config file support for Click, however they seem to lack one or more of the following features:
Sensible defaults
Proper handling of resolution order
Support for multi value options, multiple options or a combination of both
In contrast this module may lack some more sophisticated features of the other implementations. This is a deliberate choice as this module is intended to be a simple option that Just Works with sensible defaults.
Metadata
Release files for click-config-file 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| click_config_file-0.4.3.tar.gz | 4.2 kB | Without provenance | Details for click_config_file-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| click_config_file-0.4.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for click_config_file-0.4.3-py2.py3-none-any.whl |
Total release size: 11.5 kB
Release files / click_config_file-0.4.3.tar.gz
| Download URL | click_config_file-0.4.3.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b467bb4a7e3227ce349bd776062466c89ea6bdab39f4e54a179b52a0cc9c2a08
|
|
BLAKE2b-256 checksum How to use checksums |
7d0d0be444a028f9d617a697ad8ca45639f12294c9550982c7ea15bf2217a6f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / click_config_file-0.4.3-py2.py3-none-any.whl
| Download URL | click_config_file-0.4.3-py2.py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
0c6c7598a9f7121394f2e3896ee8c6f4c9c2d3e7c19ee619652897885e1a9e3a
|
|
BLAKE2b-256 checksum How to use checksums |
2a3f91946b67f239c389a71ebcdce58a23e6afa8cdbd4dbe525cb81a8c8f1a7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for click-config-file 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.