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 Distributions

click-configfile-0.2.0.zip (76.3 kB view details)

Uploaded Source

click-configfile-0.2.0.tar.gz (61.6 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.0-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file click-configfile-0.2.0.zip.

File metadata

  • Download URL: click-configfile-0.2.0.zip
  • Upload date:
  • Size: 76.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for click-configfile-0.2.0.zip
Algorithm Hash digest
SHA256 c9578c48dee4cb0b8976a9d2ee46b394c20b3c260d56d923cbf6245daa3655db
MD5 b531f1f93a95add9d8c20e1ef12d03bf
BLAKE2b-256 d908cae53a778cc74a1b85c49abab5324492c9d049d47ad6ac2efa4ad9b877b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for click-configfile-0.2.0.tar.gz
Algorithm Hash digest
SHA256 61ea77cf51875d838a3570f2564dc1d63640f960d077e0b43082890e99bb2459
MD5 99eeffb530597dec67db9bf4dae85e7c
BLAKE2b-256 d404a1e644b80caf45a525d3a8721435c6f2498eb5c30f4a24de4f7f7238b000

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for click_configfile-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0c5f01dd2d0aba0a02dc94b706995f31f2b54a6c6ca4be279a1e8f26808672bb
MD5 87caba1f35bc46c55d8737e3b9f8ce0e
BLAKE2b-256 9edf60e70cf74cc6bb38778de861972d968957d0d26840e753f357d28f7a165c

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