Click CLI Command Group
Build professional CLI tools with Click using command groups, options, arguments, and help text.
import click
@click.group()
@click.version_option(version="1.0.0")
def cli():
"""SnippetsLab CLI — manage your snippet collection."""
pass
@cli.command()
@click.argument("name")
@click.option("--lang", "-l", default="python", help="Programming language")
@click.option("--difficulty", "-d", type=click.Choice(["beginner", "intermediate", "advanced"]))
def create(name: str, lang: str, difficulty: str | None):
"""Create a new snippet with NAME."""
click.echo(f"Creating snippet: {name} ({lang})")
if difficulty:
click.echo(f" Difficulty: {difficulty}")
@cli.command()
@click.option("--tag", "-t", multiple=True, help="Filter by tag")
@click.option("--limit", default=10, show_default=True)
def search(tag: tuple[str, ...], limit: int):
"""Search snippets by tag."""
click.echo(f"Searching (tags={tag}, limit={limit})")
if __name__ == "__main__":
cli()Use Cases
- Developer tools
- Admin scripts
- Data pipeline runners
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
CLI Tool with argparse
Build a professional CLI tool with subcommands, typed arguments, environment variable fallbacks, and help text.
Rich Progress Bar for CLI
Display beautiful progress bars and status spinners in CLI applications using the Rich library.
Async HTTP Client with httpx
Production-ready async HTTP client using httpx with retries, timeouts, and connection pooling.
Retry Decorator with Exponential Backoff
Generic retry decorator with configurable attempts, exponential backoff, and exception filtering.