click-patterns
Click framework examples and templates - decorators, nested commands, parameter validation. Use when building Python CLI with Click, implementing command groups, adding CLI options/arguments, validating CLI parameters, creating nested subcommands, or when user mentions Click framework, @click decorators, command-line interface.
When & Why to Use This Skill
This Claude skill provides a comprehensive suite of Click framework patterns, templates, and automation scripts designed for building production-ready Python Command Line Interfaces (CLIs). It streamlines the implementation of complex CLI features such as nested command groups, custom parameter validation, and enhanced terminal output using Rich, ensuring developers follow industry best practices for CLI design and architecture.
Use Cases
- Rapidly scaffolding new Python CLI projects using pre-defined templates and generation scripts to maintain a consistent and professional project structure.
- Implementing hierarchical command structures and nested subcommands for sophisticated, multi-functional terminal applications.
- Applying advanced parameter validation and custom types to improve the robustness, security, and user experience of command-line tools.
- Enhancing CLI visual feedback with colored output, formatted help text, and versioning options to create a polished user interface.
- Automating the validation of existing Click-based implementations to ensure compliance with framework standards and best practices.
| name | click-patterns |
|---|---|
| description | Click framework examples and templates - decorators, nested commands, parameter validation. Use when building Python CLI with Click, implementing command groups, adding CLI options/arguments, validating CLI parameters, creating nested subcommands, or when user mentions Click framework, @click decorators, command-line interface. |
| allowed-tools | Read, Write, Bash |
Click Framework Patterns
This skill provides comprehensive Click framework patterns, templates, and examples for building production-ready Python CLIs.
Instructions
When Building a Click CLI
Read the appropriate template based on complexity:
- Simple CLI:
templates/basic-cli.py - Nested commands:
templates/nested-commands.py - Custom validators:
templates/validators.py
- Simple CLI:
Generate new Click project:
bash scripts/generate-click-cli.sh <project-name> <cli-type>Where cli-type is: basic, nested, or advanced
Study complete examples:
examples/complete-example.md- Full-featured CLIexamples/patterns.md- Common patterns and best practices
Validate your Click setup:
bash scripts/validate-click.sh <cli-file.py>
Core Click Patterns
Command Groups:
@click.group()
def cli():
"""Main CLI entry point"""
pass
@cli.command()
def subcommand():
"""A subcommand"""
pass
Options and Arguments:
@click.option('--template', '-t', default='basic', help='Template name')
@click.argument('environment')
def deploy(template, environment):
pass
Nested Groups:
@cli.group()
def config():
"""Configuration management"""
pass
@config.command()
def get():
"""Get config value"""
pass
Parameter Validation:
@click.option('--mode', type=click.Choice(['fast', 'safe', 'rollback']))
@click.option('--count', type=click.IntRange(1, 100))
def command(mode, count):
pass
Available Templates
- basic-cli.py - Simple single-command CLI
- nested-commands.py - Command groups and subcommands
- validators.py - Custom parameter validators
- advanced-cli.py - Advanced patterns with plugins and chaining
Available Scripts
- generate-click-cli.sh - Creates Click project structure
- validate-click.sh - Validates Click CLI implementation
- setup-click-project.sh - Setup dependencies and environment
Available Examples
- complete-example.md - Production-ready Click CLI
- patterns.md - Best practices and common patterns
- edge-cases.md - Edge cases and solutions
Requirements
- Python 3.8+
- Click 8.0+ (
pip install click) - Rich for colored output (
pip install rich)
Best Practices
- Use command groups for organizing related commands
- Add help text to all commands and options
- Validate parameters using Click's built-in validators
- Use context (@click.pass_context) for sharing state
- Handle errors gracefully with try-except blocks
- Add version info with @click.version_option()
- Use Rich for beautiful colored output
Common Use Cases
- Building CLI tools with multiple commands
- Creating deployment scripts with options
- Implementing configuration management CLIs
- Building database migration tools
- Creating API testing CLIs
- Implementing project scaffolding tools
Purpose: Provide Click framework templates and patterns for Python CLI development Load when: Building Click CLIs, implementing command groups, or validating CLI parameters