Skip to main content

click-option-group

PyPI version Build status Coverage Status Supported Python versions License

click-option-group is a Click-extension package that adds option groups missing in Click.

Aim and Motivation

Click is a package for creating powerful and beautiful command line interfaces (CLI) in Python, but it has no the functionality for creating option groups.

Option groups are convenient mechanism for logical structuring CLI, also it allows you to set the specific behavior and set the relationship among grouped options (mutually exclusive options for example). Moreover, argparse stdlib package contains this functionality out of the box.

At the same time, many Click users need this functionality. You can read interesting discussions about it in the following issues:

The aim of this package is to provide group options with extensible functionality using canonical and clean API (Click-like API as far as possible).

Quickstart

Installing

Install and update using pip:

pip install -U click-option-group

A Simple Example

Here is a simple example how to use option groups in your Click-based CLI. Just use optgroup for declaring option groups by decorating your command function in Click-like API style.

# app.py

import click
from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup

@click.command()
@optgroup.group('Server configuration', 
                help='The configuration of some server connection')
@optgroup.option('-h', '--host', default='localhost', help='Server host name')
@optgroup.option('-p', '--port', type=int, default=8888, help='Server port')
@optgroup.option('-n', '--attempts', type=int, default=3, help='The number of connection attempts')
@optgroup.option('-t', '--timeout', type=int, default=30, help='The server response timeout')
@optgroup.group('Input data sources', cls=RequiredMutuallyExclusiveOptionGroup, 
                help='The sources of the input data')
@optgroup.option('--tsv-file', type=click.File(), help='CSV/TSV input data file')
@optgroup.option('--json-file', type=click.File(), help='JSON input data file')
@click.option('--debug/--no-debug', default=False, help='Debug flag')
def cli(**params):
    print(params)

if __name__ == '__main__':
    cli()
$ python app.py --help
Usage: app.py [OPTIONS]

Options:
  Server configuration:           The configuration of some server connection
    -h, --host TEXT               Server host name
    -p, --port INTEGER            Server port
    -n, --attempts INTEGER        The number of connection attempts
    -t, --timeout INTEGER         The server response timeout
  Input data sources: [mutually_exclusive, required]
                                  The sources of the input data
    --tsv-file FILENAME           CSV/TSV input data file
    --json-file FILENAME          JSON input data file
  --debug / --no-debug            Debug flag
  --help                          Show this message and exit.

Documentation

https://click-option-group.readthedocs.io

Changelog

v0.3.1

  • Add hidden=True to _GroupTitleFakeOption as a temporary workaroud for issue #4

v0.3.0

  • Add support for hidden options inside groups (PR #2)

v0.2.3

  • Transfer the repo to click-contrib organisation

v0.2.2

  • Add true lineno in warning when declaring empty option group
  • Update readme

v0.2.1

  • Use RuntimeWarning and stacklevel 2 when declaring empty option group
  • Update readme

v0.2.0

  • Implement RequiredMutuallyExclusiveOptionGroup class instead of required argument for MutuallyExclusiveOptionGroup
  • Add tests with 100% coverage
  • Update readme

v0.1.0

  • First public release

Metadata

Release files for click-option-group 0.3.1

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-option-group 0.3.1
File Size Uploaded
click-option-group-0.3.1.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for click-option-group 0.3.1
File Interpreter ABI Platform
click_option_group-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / click-option-group-0.3.1.tar.gz

Download URL click-option-group-0.3.1.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7af80d006e316ca74278d41df2310f388e51fee81922406fce5e61a6c8d9753e
BLAKE2b-256 checksum
How to use checksums
f7d35c68d0e8ab93f202f1bd34608bc5704ce197e2e0c9569e18e74929339e46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.6

Release files / click_option_group-0.3.1-py3-none-any.whl

Download URL click_option_group-0.3.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca78d8adc11e411729b312321e359bb072f8a88c99249c6c71348f62133fb57f
BLAKE2b-256 checksum
How to use checksums
dd7cf1e9a5ba5ac3a1c6e0c0563e97b5c0c7bed1a54c187ee43a6edae3d99daa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.6

Release history Release notifications | RSS feed

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

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