Skip to main content

click is a framework to simplify writing composable commands for command-line tools. This package extends the click functionality by adding support for commands that use configuration files.

EXAMPLE:

A configuration file, like:

# -- FILE: foo.ini
[foo]
flag = yes
name = Alice and Bob
numbers = 1 4 9 16 25
filenames = foo/xxx.txt
    bar/baz/zzz.txt

[person.alice]
name = Alice
birthyear = 1995

[person.bob]
name = Bob
birthyear = 2001

can be processed with:

# EXAMPLE:
# -- FILE: example_command_with_configfile.py (ALL PARTS: simplified)
from click_configfile import ConfigFileReader, Param, SectionSchema
from click_configfile import matches_section
import click

class ConfigSectionSchema(object):
    """Describes all config sections of this configuration file."""

    @matches_section("foo")
    class Foo(SectionSchema):
        name    = Param(type=str)
        flag    = Param(type=bool, default=True)
        numbers = Param(type=int, multiple=True)
        filenames = Param(type=click.Path(), multiple=True)

    @matches_section("person.*")   # Matches multiple sections
    class Person(SectionSchema):
        name      = Param(type=str)
        birthyear = Param(type=click.IntRange(1990, 2100))


class ConfigFileProcessor(ConfigFileReader):
    config_files = ["foo.ini", "foo.cfg"]
    config_section_schemas = [
        ConfigSectionSchema.Foo,     # PRIMARY SCHEMA
        ConfigSectionSchema.Person,
    ]

    # -- SIMPLIFIED STORAGE-SCHEMA:
    #   section:person.*        -> storage:person.*
    #   section:person.alice    -> storage:person.alice
    #   section:person.bob      -> storage:person.bob

    # -- ALTERNATIVES: Override ConfigFileReader methods:
    #  * process_config_section(config_section, storage)
    #  * get_storage_name_for(section_name)
    #  * get_storage_for(section_name, storage)


# -- COMMAND:
CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor.read_config())

@click.command(context_settings=CONTEXT_SETTINGS)
@click.option("-n", "--number", "numbers", type=int, multiple=True)
@click.pass_context
def command_with_config(ctx, numbers):
    # -- ACCESS ADDITIONAL DATA FROM CONFIG FILES: Using ctx.default_map
    for person_data_key in ctx.default_map.keys():
        if not person_data_key.startswith("person."):
            continue
        person_data = ctx.default_map[person_data_key]
        process_person_data(person_data)    # as dict.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

click-configfile-0.2.2.tar.gz (62.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

click_configfile-0.2.2-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file click-configfile-0.2.2.tar.gz.

File metadata

File hashes

Hashes for click-configfile-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f9d7b81bfce590978d2cb9ab8830ff17fa0b73fc710ba5915b14af4e86fac281
MD5 ea07f5ac75c76a7b82e3046a05e1ce67
BLAKE2b-256 7b2c7f66f4c61534e1b4c3135e2a8e12cf5e8d79024ff7475ed3337118eabe5f

See more details on using hashes here.

File details

Details for the file click_configfile-0.2.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for click_configfile-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4f50cc5a6d5f348c65a26784a249a96e03af49268553d12b2627ade45191cd12
MD5 7f8ceee8b8ca7ac89a7dec904b2cac73
BLAKE2b-256 4504323d0bbee7442070d7635ad0dbba6de18cae6d2aed2e0dd7ade2319f463d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page