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 101
Section titled “Click 101”Commands
Section titled “Commands”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:
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:
$ python hello.pyHello, world!$ python hello.py --helpUsage: hello.py [OPTIONS] Prints 'hello, world!' into the terminal. Options: --help Show this message and exit.
Arguments and Options
Section titled “Arguments and Options”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:
import click
default=1, show_default=True, help="Number of times to print the greeting.") 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()$ python hello_v2.py --say-goodbye --times 3 EdwardHello, Edward! Hello, Edward! Hello, Edward! Goodbye!
$ python hello_v2.py --helpUsage: 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.
Groups
Section titled “Groups”Last but not least, Click allows for command groups and sub-commands, which allows you to nest commands inside other commands.
import click
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:
$ python hello_v3.py --helpUsage: 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:
$ python hello_v3.py french JenniferBonjour, Jennifer!$ python hello_v3.py french --helpUsage: hello_v3.py french [OPTIONS] NAME Greet in French Options: --help Show this message and exit.
Next Steps
Section titled “Next Steps”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 clickWith this:
That’s the only change needed to use rich-click! And now we get the following beautiful help text:
$ python hello_rich.py --helpUsage: 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 │ ╰──────────────────────────────────────────────────────────────────────────────╯
Other CLI libraries
Section titled “Other CLI libraries”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.