Skip to main content

🌈 Extra colorization and configuration file for Click.

Project description

Click Extra

Last release Python versions Unittests status Coverage status

What is Click Extra?

click-extra is a collection of helpers and utilities for Click, the Python CLI framework.

It provides boilerplate code and good defaults, as well as some workarounds and patches that have not reached upstream yet (or are unlikely to).

Features

  • TOML and YAML configuration file loader
  • Colorization of help screens
  • --color/--no-color option flag
  • Colored --version option
  • Colored --verbosity option and logs
  • --time/--no-time flag to measure duration of command execution
  • Platform recognition utilities (macOS, Linux and Windows)
  • New conditional markers for pytest:
    • @skip_linux, @skip_macos and @skip_windows
    • @unless_linux, @unless_macos and @unless_windows
    • @destructive and @non_destructive

Installation

Install click-extra with pip:

$ pip install click-extra

Configuration loader usage

TOML configuration

Given this CLI in a my_cli.py file:

import click

from click_extra.config import config_option


@click.group(context_settings={"show_default": True})
@click.option("--dummy-flag/--no-flag")
@click.option("--my-list", multiple=True)
@config_option()
def my_cli(dummy_flag, my_list):
    click.echo(f"dummy_flag    is {dummy_flag!r}")
    click.echo(f"my_list       is {my_list!r}")


@my_cli.command()
@click.option("--int-param", type=int, default=10)
def subcommand(int_param):
    click.echo(f"int_parameter is {int_param!r}")


if __name__ == "__main__":
    my_cli()

It produces the following help screens:

$ python ./my_cli.py
Usage: my_cli.py [OPTIONS] COMMAND [ARGS]...

Options:
  --dummy-flag / --no-flag  [default: no-flag]
  --my-list TEXT
  -C, --config CONFIG_PATH  Location of the configuration file. Supports both
                            local path and remote URL.  [default: s(dynamic)]
  --help                    Show this message and exit.  [default: False]

Commands:
  subcommand

A bare call returns:

$ ./my_cli.py subcommand
dummy_flag    is False
my_list       is ()
int_parameter is 10

Now we will change the default CLI output by creating a TOML file at ~/.my_cli.py/config.toml which contains:

# My default configuration file.
top_level_param = "is_ignored"

[my-cli]
extra_value = "is ignored too"
dummy_flag = true   # New boolean default.
my_list = ["item 1", "item #2", "Very Last Item!"]

[garbage]
# An empty random section that will be skipped

[my-cli.subcommand]
int_param = 3
random_stuff = "will be ignored"

In the file above, pay attention to:

  • the configuration's folder (~/.my_cli.py/) which correspond to the script's name (my_cli.py);
  • the top-level config section ([my-cli]), that is derived from the CLI's group ID (def my_cli());
  • all the extra comments, sections and values that will be silently ignored.

Now we can verify the TOML file is read automatticaly and change the defaults:

$ ./my_cli.py subcommand
dummy_flag    is True
my_list       is ('item 1', 'item #2', 'Very Last Item!')
int_parameter is 3

Still, any inline parameter is allowedal to ovverides the configuration defaults:

$ ./my_cli.py subcommand --int-param 555
dummy_flag    is True
my_list       is ('item 1', 'item #2', 'Very Last Item!')
int_parameter is 555

YAML configuration

Same example as above is working as-is with YAML.

Just replace the TOML file with the following configuration at ~/.my_cli.py/config.yaml:

# My default configuration file.
top_level_param: is_ignored

my-cli:
  extra_value: is ignored too
  dummy_flag: true   # New boolean default.
  my_list:
    - point 1
    - 'point #2'
    - Very Last Point!

  subcommand:
    int_param: 77
    random_stuff: will be ignored

garbage: >
  An empty random section that will be skipped
$ ./my_cli.py --config ~/.my_cli.py/config.yaml subcommand
dummy_flag    is True
my_list       is ('point 1', 'point #2', 'Very Last Point!')
int_parameter is 77

Remote configuration

Remote URL can be passed directly to the --config option:

$ ./my_cli.py --config https://example.com/dummy/configuration.yaml subcommand
dummy_flag    is True
my_list       is ('point 1', 'point #2', 'Very Last Point!')
int_parameter is 77

Colorization of help screen

Extend Cloup's own help formatter and theme to add colorization of:

  • Options
  • Choices
  • Metavars

Used in

Check these projects to get real-life example of click-extra usage:

Issues addressed by click-extra

Keep track of things to undo if they reach upstream.

click:

click-config-file:

click-help-color:

click-log:

cli-helper:

cloup:

python-tabulate:

Dependencies

Here is a graph of Python package dependencies:

click-extra dependency graph

Development

Development guidelines are the same as parent project mpm, from which click-extra originated.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

click-extra-1.4.1.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

click_extra-1.4.1-py3-none-any.whl (38.3 kB view details)

Uploaded Python 3

File details

Details for the file click-extra-1.4.1.tar.gz.

File metadata

  • Download URL: click-extra-1.4.1.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for click-extra-1.4.1.tar.gz
Algorithm Hash digest
SHA256 44206982210a37cf50c570ac3c7d9e7e56848b5b17355e08d7ad8ef209893820
MD5 48ba000421c0f46682f4a439960e36b4
BLAKE2b-256 5daf89c66dcb92de0d1e4749a070f5c234bb1cc323d392dc870e61b5b6f12bff

See more details on using hashes here.

File details

Details for the file click_extra-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: click_extra-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 38.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for click_extra-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee96feed6602fb4c2e444daf07f1d72f14a7e92871a1f76b82ee94f4c6cc7774
MD5 005b30971053d37021dea8026cf199b6
BLAKE2b-256 70b56bf5d85cc0209f4f99b20f5176c92e062eb0301a12286116d3edb2782519

See more details on using hashes here.

Supported by

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