The Click Library
Create CLIs with decorators.
The Click Library is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Click?
Click is a popular third-party library for building command-line interfaces using decorators. It handles parsing, help text, prompts, and colors with very little code. Install it with pip install click.
import click
@click.command()
def hello():
click.echo('Hello from Click')
# hello() would run when called as a scriptclick.echo
Use click.echo() instead of print. It handles encoding issues and works smoothly with Click's color and redirection features.
import click
@click.command()
def greet():
click.echo('Welcome!')
Arguments
The @click.argument decorator declares a required positional argument that Click passes to your function as a parameter.
import click
@click.command()
@click.argument('name')
def hello(name):
click.echo('Hello ' + name)
Options
@click.option defines optional flags. Provide a default and Click builds help text and type handling automatically.
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings')
def hello(count):
for _ in range(count):
click.echo('Hi')
Typed Options
Click infers the type from the default, or you can set it explicitly with type=int. Invalid input produces a friendly error.
import click
@click.command()
@click.option('--age', type=int, default=0)
def show(age):
click.echo('Age is ' + str(age))
Boolean Flags
An option with is_flag=True becomes a switch that is True when present, like --verbose.
import click
@click.command()
@click.option('--shout', is_flag=True)
@click.argument('text')
def say(shout, text):
click.echo(text.upper() if shout else text)
Prompts
Set prompt=True and Click will interactively ask the user if the option was not supplied on the command line.
import click
@click.command()
@click.option('--name', prompt='Your name')
def hello(name):
click.echo('Hello ' + name)
Command Groups
@click.group creates a parent command that holds subcommands, the Click equivalent of argparse subparsers. Subcommands attach with @cli.command().
import click
@click.group()
def cli():
pass
@cli.command()
def start():
click.echo('starting')
@cli.command()
def stop():
click.echo('stopping')
Colored Output
click.style wraps text with color and styling, and click.secho combines echo with styling in one call.
import click
@click.command()
def status():
click.secho('OK', fg='green', bold=True)
click.secho('Error', fg='red')
Confirmation and Exit
click.confirm asks a yes/no question, and raising click.Abort or returning ends the command cleanly.
import click
@click.command()
def delete():
if click.confirm('Are you sure?'):
click.echo('deleted')
else:
click.echo('cancelled')
argparse vs Click
argparse is built in and great for simple tools. Click is an external dependency but offers a cleaner decorator style, nested groups, prompts, and color out of the box for larger CLIs.
import click
@click.command()
@click.argument('items', nargs=-1)
def show(items):
for item in items:
click.echo(item)
Quick Check
Test your understanding of Click.
Recap
You learned the Click library:
- Decorators
@click.command,@click.argument,@click.optiondefine a CLI. click.echoandclick.sechoprint, optionally with color.@click.groupbuilds nested subcommands; prompts and confirms are built in.- It is an external dependency installed via pip.
Next: styling terminal output with rich.
Frequently asked questions
Is the “The Click Library” lesson free?
Yes — the full text of “The Click Library” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Click Library”?
Create CLIs with decorators. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Click Library” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- argparse Basics
- Subcommands and Options
- The Click Library
- Rich Terminal Output