Last Updated: February 25, 2016
· scott2b
Scott bradley 300x450

Implementing a help command in Python Click

Click is Armin Ronacher's very cool command-line building library that make short work of building your own CLI in Python. It's really worth checking out: http://click.pocoo.org/

One of click's excellent features is the built-in help mechanism. mycli --help basically comes for free, as well as individual command help: mycli mycommand --help.

A common usage pattern these days, however, is to have a help command that shows the overall help for the application. Click give you the --help option mentioned above, but your users might expect mycli help to work instead.

Say you've defined your cli as a group:

def cli():
    """My very cool command-line tool"""

And various commands that look like this:

def dosomething():
    """Do Something."""

Getting a help command to work is as simple as calling get_help on the parent context of the help command context:

def help(ctx):

That's all there is to it!

Say Thanks
Filed Under