Skip to content

Introduction to Click

rich-click is a drop-in replacement for Click, a Python CLI framework. This means that if you know Click, you already know rich-click.

The Click documentation is the appropriate place to learn more advanced things about Click. We are providing a quick overview of Click here to catch users up to speed.

Click utilizes function decorators as its primary interface for composing a CLI.

For example, the @click.command decorator creates a Command object that calls the function:

docs/code_snippets/introduction_to_click/hello.py
import click
def hello():
"""Prints 'hello, world!' into the terminal."""
print("Hello, world!")
if __name__ == "__main__":
hello()

You can run the file like normal, or you can run --help to render the function’s docstring:

Terminal window
$ python hello.py
Output
Hello, world!
Terminal window
$ python hello.py --help
Output
Usage: hello.py [OPTIONS]

  Prints 'hello, world!' into the terminal.

Options:
  --help  Show this message and exit.

Arguments and options are also added with decorators. The difference between arguments and options is:

  • Arguments are required, and options are not (unless you specify required=True).
  • Arguments are positional, and options must be prefixed with one or two dashes.

The below code shows some of the features available with options and arguments:

docs/code_snippets/introduction_to_click/hello_v2.py
import click
@click.option("--times", "-t",
default=1,
type=click.INT,
show_default=True,
help="Number of times to print the greeting.")
@click.option("--say-goodbye",
is_flag=True,
default=False,
help="After saying hello, say goodbye.")
def hello(name, times, say_goodbye):
"""Prints 'hello, [name]!' into the terminal N times."""
for t in range(times):
print(f"Hello, {name}!")
if say_goodbye:
print("Goodbye!")
if __name__ == "__main__":
hello()
Terminal window
$ python hello_v2.py --say-goodbye --times 3 Edward
Output
Hello, Edward!
Hello, Edward!
Hello, Edward!
Goodbye!
Terminal window
$ python hello_v2.py --help
Output
Usage: hello_v2.py [OPTIONS] NAME

  Prints 'hello, [name]!' into the terminal N times.

Options:
  -t, --times INTEGER  Number of times to print the greeting.  [default: 1]
  --say-goodbye        After saying hello, say goodbye.
  --help               Show this message and exit.

Click is able to parse the new arguments and options, e.g. it knows that --times [number] maps to the function argument times. Additionally, Click also knows to render these new arguments in the help text.

Last but not least, Click allows for command groups and sub-commands, which allows you to nest commands inside other commands.

docs/code_snippets/introduction_to_click/hello_v3.py
import click
@click.group("greetings")
def greetings_cli():
"""CLI for greetings."""
@greetings_cli.command("english")
def english(name):
"""Greet in English"""
print(f"Hello, {name}!")
@greetings_cli.command("french")
def french(name):
"""Greet in French"""
print(f"Bonjour, {name}!")
if __name__ == "__main__":
greetings_cli()

Running python hello.py --help gives you the help text for the group and lists the subcommands:

Terminal window
$ python hello_v3.py --help
Output
Usage: hello_v3.py [OPTIONS] COMMAND [ARGS]...

  CLI for greetings.

Options:
  --help  Show this message and exit.

Commands:
  english  Greet in English
  french   Greet in French

And you can run any of the subcommands like so:

Terminal window
$ python hello_v3.py french Jennifer
Output
Bonjour, Jennifer!
Terminal window
$ python hello_v3.py french --help
Output
Usage: hello_v3.py french [OPTIONS] NAME

  Greet in French

Options:
  --help  Show this message and exit.

So, what does any of this have to do with rich-click? Simply put: rich-click is a drop-in replacement for Click. Let’s take the second CLI example above with the --times option and add rich-click by replacing this:

import click

With this:

import rich_click as click

That’s the only change needed to use rich-click! And now we get the following beautiful help text:

Terminal window
$ python hello_rich.py --help
Output

 Usage: hello_rich.py [OPTIONS] COMMAND [ARGS]...

 CLI for greetings.

╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help  Show this message and exit.                                          │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ english                   Greet in English                                   │
│ french                    Greet in French                                    │
╰──────────────────────────────────────────────────────────────────────────────╯

Click has been around for over a decade and is the most popular third-party CLI tool in Python. It’s popularity is for a good reason: you can do basically anything in Click. Click is very well abstracted, and as a result, you will likely never feel like you are brushing up against the limitations of what Click is capable of.

There are other CLI libraries available. Of particular note is Typer, which is itself built on top of Click. Typer is also able to format help messages with Rich, functionality that was adapted from rich-click - as a result, the output looks remarkably similar! However, the two libraries have since drifted apart, so note that not all rich-click functionality is available within Typer.