Docs
README
š§ DevTools CLI
A command-line tool demonstrating Click, Rich, and practical CLI development patterns.
Features
- ā¢File management utilities
- ā¢Git helpers
- ā¢Code statistics
- ā¢Project scaffolding
- ā¢Beautiful terminal output with Rich
Installation
# Install in development mode
pip install -e .
# Or install dependencies only
pip install click rich
Usage
File Operations
# Count files matching a pattern
devtools files count ./src
devtools files count . --pattern "*.py"
# Find files with details
devtools files find . --pattern "*.py" --limit 20
# Calculate directory size
devtools files size ./project
# Display directory tree
devtools files tree . --depth 3
Git Helpers
# Enhanced git status with colors
devtools git status
# Pretty git log with table format
devtools git log --count 15
# List branches with info
devtools git branches --all
# Clean up merged branches
devtools git cleanup --dry-run
devtools git cleanup # Actually delete
Code Statistics
# Count lines of code
devtools stats lines ./src
devtools stats lines . --ext .py --ext .js
# Find TODO/FIXME comments
devtools stats todos ./
devtools stats todos . --pattern "TODO|FIXME|HACK|XXX"
# Project summary
devtools stats summary .
Project Scaffolding
# Create new Python project
devtools new python myproject
# Create FastAPI project
devtools new fastapi myapi
# Create CLI tool project
devtools new cli mytool
# Create in specific directory
devtools new python myproject --path ~/projects
Project Structure
02_cli_tool/
āāā devtools/
ā āāā __init__.py
ā āāā main.py # CLI entry point
ā āāā commands/
ā ā āāā __init__.py
ā ā āāā files.py # File commands
ā ā āāā git.py # Git commands
ā ā āāā stats.py # Code statistics
ā ā āāā scaffold.py # Project creation
ā āāā utils/
ā āāā __init__.py
ā āāā console.py # Rich console helpers
ā āāā helpers.py # Utility functions
āāā tests/
ā āāā __init__.py
ā āāā test_cli.py
āāā pyproject.toml
āāā README.md
Key Concepts Demonstrated
Click Framework Patterns
# Command groups for organizing subcommands
@click.group()
def cli():
"""Main CLI group."""
pass
@cli.command()
@click.argument('path')
@click.option('--pattern', '-p', default='*')
def find(path, pattern):
"""Find files matching pattern."""
pass
# Nested command groups
cli.add_command(files) # devtools files ...
cli.add_command(git) # devtools git ...
Rich Library Features
from rich.console import Console
from rich.table import Table
from rich.tree import Tree
console = Console()
# Beautiful tables
table = Table(title="Results")
table.add_column("File", style="cyan")
table.add_column("Size", style="green")
console.print(table)
# Colored output
console.print("[green]Success![/green]")
console.print("[red]Error:[/red] Something went wrong")
# Directory trees
tree = Tree("[bold blue]project/[/bold blue]")
tree.add("src/")
console.print(tree)
CLI Best Practices
- ā¢Use command groups - Organize related commands
- ā¢Provide help text - Document every command and option
- ā¢Use sensible defaults - Make common cases easy
- ā¢Show progress - Use progress bars for long operations
- ā¢Handle errors gracefully - Catch exceptions, show helpful messages
- ā¢Support dry-run - Let users preview destructive operations
Running Tests
# Install test dependencies
pip install pytest
# Run tests
pytest tests/
# Run with verbose output
pytest tests/ -v
Extending the CLI
Adding a New Command Group
# devtools/commands/docker.py
import click
from rich.console import Console
console = Console()
@click.group()
def docker():
"""Docker management commands."""
pass
@docker.command()
def ps():
"""List running containers."""
# Implementation here
pass
# In main.py, add:
# from .commands import docker
# cli.add_command(docker.docker)
Adding Options and Arguments
@click.command()
@click.argument('path', type=click.Path(exists=True))
@click.option('--recursive', '-r', is_flag=True, help='Search recursively')
@click.option('--limit', '-l', default=10, help='Max results')
@click.option('--format', type=click.Choice(['json', 'table']))
def search(path, recursive, limit, format):
"""Search for files."""
pass
Dependencies
- ā¢click (>=8.0.0) - Command line interface creation
- ā¢rich (>=13.0.0) - Beautiful terminal formatting