DEV Community

Thesius Code
Thesius Code

Posted on • Originally published at datanest-stores.pages.dev

Python CLI Framework

Python CLI Framework

Build beautiful, professional command-line tools with Click, Rich, and TOML configuration — from first command to PyPI package.

Python 3.10+
Click
License: MIT


What You Get

  • Click-based CLI — Group commands with --verbose, --output-format, and --config
  • Rich output — Tables, JSON, YAML, and CSV formatters with color and progress bars
  • TOML configuration — Read/write config files with environment variable fallback
  • Project scaffoldinginit command generates new project structure from Jinja2 templates
  • Task runnerrun command with Rich progress bars and error handling
  • Structured logging — Rich console handler with optional file rotation
  • Full test suite — Click CliRunner tests for every command
  • Best practices guide — Building, testing, and distributing CLI apps

File Tree

python-cli-framework/
├── src/
│   └── cli/
│       ├── main.py              # CLI entry point with Click group
│       ├── commands/
│       │   ├── init.py          # Project scaffolding command
│       │   ├── run.py           # Task runner command
│       │   └── config.py        # Config management command
│       ├── config.py            # TOML config read/write
│       ├── output.py            # Table/JSON/YAML/CSV formatters
│       ├── logging_setup.py     # Structured logging
│       └── utils.py             # Utility functions
├── templates/
│   ├── pyproject.toml.j2        # Project template
│   └── README.md.j2             # README template
├── tests/
│   ├── test_cli.py              # CLI command tests
│   └── test_config.py           # Config tests
├── guides/
│   └── building-cli-apps.md     # Best practices guide
└── pyproject.toml               # Package configuration
Enter fullscreen mode Exit fullscreen mode

Getting Started

1. Install

pip install -e ".[dev]"
Enter fullscreen mode Exit fullscreen mode

2. Try the CLI

# Initialize a new project
mycli init my-project --template default

# View configuration
mycli config list
mycli config get project.name
mycli config set project.version "2.0.0"

# Run tasks with progress bars
mycli run build --verbose

# Change output format
mycli --output-format json config list
mycli --output-format table config list
Enter fullscreen mode Exit fullscreen mode

3. Use as a starting point

# src/cli/main.py — extend with your own commands
from cli.main import cli

@cli.command()
@click.argument("name")
def greet(name: str) -> None:
    """Say hello to NAME."""
    click.echo(f"Hello, {name}!")
Enter fullscreen mode Exit fullscreen mode

Requirements

  • Python 3.10+
  • click >= 8.1
  • rich >= 13.0
  • tomli >= 2.0 (Python < 3.11) / tomllib (Python 3.11+)
  • tomli-w >= 1.0
  • jinja2 >= 3.1

Architecture

┌─────────────────────────────────────────────┐
│              CLI Entry Point                │
│    main.py: @click.group() + options        │
│    --verbose  --output-format  --config     │
└──────────────────┬──────────────────────────┘
                   │
    ┌──────────────┼──────────────┐
    ▼              ▼              ▼
┌────────┐   ┌────────┐   ┌──────────┐
│  init  │   │  run   │   │  config  │
│scaffold│   │ tasks  │   │ get/set  │
└───┬────┘   └───┬────┘   └────┬─────┘
    │            │              │
    ▼            ▼              ▼
┌──────────────────────────────────────┐
│           Shared Layers              │
│  config.py │ output.py │ utils.py   │
│  (TOML)    │ (Rich)    │ (helpers)  │
└──────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

Running Tests

pytest tests/ -v
Enter fullscreen mode Exit fullscreen mode

Related Products


This is 1 of 14 resources in the Python Developer Pro toolkit. Get the complete [Python CLI Framework] with all files, templates, and documentation for $29.

Get the Full Kit →

Or grab the entire Python Developer Pro bundle (14 products) for $159 — save 30%.

Get the Complete Bundle →


Related Articles

Top comments (0)