Skip to main content

Format click help output nicely with rich

Project description

rich-click

Format click help output nicely with Rich.

  • Click is a "Python package for creating beautiful command line interfaces".
  • Rich is a "Python library for rich text and beautiful formatting in the terminal".

The intention of rich-click is to provide attractive help output from click, formatted with rich, with minimal customisation required.

Screenshots

Native click help With rich-click

Installation

You can install rich-click from the Python Package Index (PyPI) with pip or equivalent.

python -m pip install rich-click

Usage

There are two main ways to set up rich-click formatting for your tool. Which you choose will depend on your use-case and your personal disposition:

The path of least typing

Monkey patching is probably bad and you should only use this method if you are a Responsible Developer. It's also good if you're lazy, as it requires very little typing. In fact, the whole thing can be done in just four lines:

import click
import rich_click
click.Group.format_help = rich_click.rich_format_help
click.Command.format_help = rich_click.rich_format_help

This overwrites the default click methods with those from the rich-click package. As such, no other changes are needed - just continue to use click as you would normally and it will use these custom methods to print your help output.

The good and proper way

If using monkey-patching in your project makes your palms sweaty and your pulse race, then you'll be pleased to know that you can also use rich-click in a nicely declarative and verbose manner:

import click
import rich_click

class RichClickGroup(click.Group):
    def format_help(self, ctx, formatter):
        rich_click.rich_format_help(self, ctx, formatter)
class RichClickCommand(click.Command):
    def format_help(self, ctx, formatter):
        rich_click.rich_format_help(self, ctx, formatter)

@click.group(cls=RichClickGroup)
@click.option('--debug/--no-debug', default=False)
def cli(debug):
    click.echo(f"Debug mode is {'on' if debug else 'off'}")

@cli.command(cls=RichClickCommand)
def sync():
    click.echo('Syncing')

(example based on the click docs)

Here we are making new Group and Command child classes that are based on click. We define our custom format_help() functions and then tell click to to use these classes with the cls argument.

Customisation

There isn't really much you can customise yet, but this is planned. I'm thinking:

  • Colours
  • Whether to print positional arguments or not
    • Could maybe provide custom args to give help text to these
  • The ability to break up commands and options into multiple named panels

Contributing

Contributions and suggestions for new features are welcome, as are bug reports! Please create a new issue or better still, dive right in with a pull-request.

Credits

This package was put together hastily by Phil Ewels (@ewels), but the hard work was really done by Will McGugan (@willmcgugan) who not only is the author of Rich but also wrote the original code that this package is based on.

Project details


Download files

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

Source Distribution

rich-click-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

rich_click-0.1.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file rich-click-0.1.2.tar.gz.

File metadata

  • Download URL: rich-click-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 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.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for rich-click-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d5e298c506d0b7c623e2dbbb51c06c01bd77a30d8705aa3227d7f09ed43010de
MD5 56c058a9edbda9292eced6646910dd64
BLAKE2b-256 ed712e3bf92884524e66cb4ed8fcfcb6a7e0153e2b60838e2113f6286bee0d42

See more details on using hashes here.

File details

Details for the file rich_click-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: rich_click-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for rich_click-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a91fc2760b0238fc4c84f5009ea706a7b093824f0e78db27f6511a018850d130
MD5 5dfe6b161bf43bcf841cbfb7348c11c7
BLAKE2b-256 c6925385015b343d2399b0b9526eb3f43f73a7404e72c6aa41b852cc352b073f

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