Skip to main content

cloup

PyPI Package Travis-CI Build Status Coverage Status

cloup (click + group) extends pallets/click to add option groups and the possibility of organizing the subcommands of a Group in multiple help sections.

Currently, this package only affects how the command help is formatted, it doesn’t allow to specify constraints on option groups. Look at click-option-group if you want that. Nonetheless, constraints would be a very easy addition and may be added soon.

Installation

To install the last release:

pip install cloup

Versioning

cloup uses semantic versioning. I’ll release v1.0 when I’m satisfied with API and features but cloup is already usable, just make sure you specify a compatible version number in your list of requirements if you decide to use it, e.g.:

cloup==0.3.*

Patch releases are guaranteed to be backwards compatible (even before v1.0).

Option groups

You can define option groups in two ways or “styles”: I’ll call them “nested style” and “flat style”. The full code for the examples shown below can also be found in examples/option_groups_example.py.

Flat style

In “flat style”, you first define your option groups and then call the option() method on them. Don’t reuse OptionGroup objects in multiple commands.

input_grp = OptionGroup('Input options', help='This is a very useful description of the group')
output_grp = OptionGroup('Output options')

@cloup.command('clouptest', align_option_groups=True)
@input_grp.option('-o', '--one', help='1st input option')
@input_grp.option('--two', help='2nd input option')
@input_grp.option('--three', help='3rd input option')
@output_grp.option('--four / --no-four', help='1st output option')
@output_grp.option('--five', help='2nd output option')
@output_grp.option('--six', help='3rd output option')
@option('--seven', help='first uncategorized option', type=click.Choice('yes no ask'.split()))
@option('--height', help='second uncategorized option')
def cli_flat(**kwargs):
    """ A CLI that does nothing. """
    print(kwargs)

Subcommand sections

See the full example code here.

# {Definitions of subcommands are omitted}

@cloup.group('git')
def git():
    return 0

"""
group.section() creates a new GroupSection object, adds it to "group" and returns it.

In the help, sections are shown in the same order they are added.
Commands in each sections are shown in the same order they are listed, unless
you pass the argument "sorted=True".
"""
git.section('Start a working area (see also: git help tutorial)',
    git_clone,
    git_init
)
git.section('Work on the current change (see also: git help everyday)',
    git_rm,
    git_sparse_checkout,
    git_mv
)

# The following commands will be added to the "default section" (a sorted GroupSection)
git.add_command(cloup.command('fake-2', help='Fake command #2')(f))
git.add_command(cloup.command('fake-1', help='Fake command #1')(f))

The help will be:

Usage: git [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Start a working area (see also: git help tutorial):
  clone            Clone a repository into a new directory
  init             Create an empty Git repository or reinitialize an...

Work on the current change (see also: git help everyday):
  rm               Remove files from the working tree and from the index
  sparse-checkout  Initialize and modify the sparse-checkout
  mv               Move or rename a file, a directory, or a symlink

Other commands:
  fake-1           Fake command #1
  fake-2           Fake command #2

Though I think using _.section() is the cleanest way to define sections, there are alternatives.

One is to pass a list of GroupSection objects to @cloup.group():

SECTIONS = [
    GroupSection('Start a working area (see also: git help tutorial)',
                 git_clone, git_init),
    GroupSection('Work on the current change (see also: git help everyday)',
                 git_rm, git_sparse_checkout, git_mv)
]

@cloup.group('git', sections=SECTIONS)
def git():
    return 0

Others are:

  • git.add_section(section) to add an existing GroupSection object

  • git.add_command(cmd, name, section, ...); the section must NOT contain the command

  • @git.command(cmd, name, section, ...), specifying section.

Note: individual commands don’t know the section they belong to. Neither cloup.Command nor @cloup.command() accept a “section” argument.

Credits

For implementing option groups, I started from the idea of @chrisjsewell presented in this comment.

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.3.0 (2020-03-26)

  • Backward incompatible API changes

    • option_groups decorator now takes options as positional arguments *options;

    • Group.section decorator now takes sections as positional arguments *sections;

    • align_sections_help was renamed to align_sections;

    • GroupSection.__init__() sorted_ argument was renamed to sorted.

  • Additional signature for option_group: you can pass the help argument as 2nd positional argument.

  • Aligned option groups (option align_option_groups with default True).

  • More refactoring and testing.

0.2.0 (2020-03-11)

  • Rename CloupCommand and CloupGroup resp. to just Command and Group

  • [Feature] Add possibility of organizing subcommands of a cloup.Group in multiple help sections

  • Various code improvements

0.1.0 (2020-02-25)

  • First release on PyPI.

Metadata

Release files for cloup 0.3.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 cloup 0.3.0
File Size Uploaded
cloup-0.3.0.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cloup 0.3.0
File Interpreter ABI Platform
cloup-0.3.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 29.9 kB

Release files / cloup-0.3.0.tar.gz

Download URL cloup-0.3.0.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0e84ab2d99c0652c12ff73179cd1360c43053fc8250b8e494b6f32dab7e9bdbb
BLAKE2b-256 checksum
How to use checksums
689201c67bc3a59cdb600d393245d6d2ca2565c0229efa619a2911cf0416ee0f
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/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

Release files / cloup-0.3.0-py2.py3-none-any.whl

Download URL cloup-0.3.0-py2.py3-none-any.whl
Size 10.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b6e276566362baa9ce792b68745f82a623787a2354472402fc40652f4ea9279c
BLAKE2b-256 checksum
How to use checksums
f4ce828db4c74210d7fc2b4c1be285df30c28d499bbb33e63207977824d3a39f
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/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

Release history Release notifications | RSS feed

4.0.0

2 release files

3.1.0

2 release files

3.0.9

2 release files

3.0.8

2 release files

3.0.7

2 release files

3.0.6

2 release files

3.0.5

2 release files

3.0.4

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.13.0

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

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