Skip to main content

Easily add configuration file support to your Click applications by adding a single no-arguments decorator.

https://img.shields.io/pypi/v/click-config-file.svg?style=flat-square https://img.shields.io/conda/vn/conda-forge/click-config-file.svg?style=flat-square https://img.shields.io/travis/phha/click_config_file/master.svg?style=flat-square https://img.shields.io/codacy/grade/a5f6262609314683bf2b2bc546bdaffe/master.svg?style=flat-square

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.

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.

Options

Although configuration_option is designed to work without any mandatory arguments, some optional parameters are supported:

implicit

Default: True

By default configuration_option will look for a configuration file even if no value for the configuration option was provided either via a CLI argument or an environment variable. In this case the value will be set implicitly from cmd_name and config_file_name as described below.

If set to False the configuration file settings will only be applied when a configuration file argument is provided.

cmd_name

Default: ctx.cmd_info

The name of the decorated command. When implicitly creating a configuration file argument, the application directory containing the configuration file is resolved by calling click.get_app_dir(cmd_name).

This defaults to the name of the command as determined by click.

config_file_name

Default: config

When implicit is set to True, this argument provides the name of the configuration file inside the application directory.

In addition to the arguments above, all arguments for click.option() and click.File() are supported.

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 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.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for click-config-file 0.6.0
File Size Uploaded
click_config_file-0.6.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for click-config-file 0.6.0
File Interpreter ABI Platform
click_config_file-0.6.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 11.8 kB

Release files / click_config_file-0.6.0.tar.gz

Download URL click_config_file-0.6.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ded6ec1a73c41280727ec9c06031e929cdd8a5946bf0f99c0c3db3a71793d515
BLAKE2b-256 checksum
How to use checksums
1309dfee76b0d2600ae8bd65e9cc375b6de62f6ad5600616a78ee6209a9f17f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

Release files / click_config_file-0.6.0-py2.py3-none-any.whl

Download URL click_config_file-0.6.0-py2.py3-none-any.whl
Size 6.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3c5802dec437ed596f181efc988f62b1069cd48a912e280cd840ee70580f39d7
BLAKE2b-256 checksum
How to use checksums
2816c71980d10b75cf4ee2c71bb946c3326a18585254399aac64a5e79cfba5a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page