Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/template-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Verify Copier template

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
generate-and-test:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
- name: Generate project, test CLI and build
run: uv run --no-project --with copier --with pytest pytest tests -q
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,67 @@
# template-python-cli
A minimal, agent-ready Python CLI template powered by uv, Cyclopts, Rich, Ruff and pytest. Fast to bootstrap, easy to extend. Built with clean architecture, automated testing and GitHub Actions CI.
# copier-python-cli

A minimal, agent-ready **Copier template for Python CLI apps**, powered by **uv**, **Cyclopts**, **Rich**, **Ruff**, and **pytest**. Fast to bootstrap, easy to extend, and test-first by default.

## Generate a new CLI

Install [uv](https://docs.astral.sh/uv/getting-started/installation/), then run:

```sh
uvx --from copier copier copy gh:RuchirChawdhry/copier-python-cli ./my-cli
cd my-cli
uv sync
uv run my-cli --help
uv run my-cli greet Ada
```

`my-cli` in these examples assumes you enter `my-cli` as the project name when prompted. Copier asks for the CLI name, description, author and Python version. The name determines both the installed command (`my-cli`) and importable Python package (`my_cli`). No package-manager choices or post-generation hooks to maintain.

## Generated project

```text
my-cli/
├── .github/workflows/ci.yml
├── .copier-answers.yml
├── .gitignore
├── .python-version
├── AGENTS.md
├── LICENSE
├── README.md
├── pyproject.toml
├── src/my_cli/
│ ├── __init__.py
│ ├── __main__.py
│ └── cli.py
└── tests/test_cli.py
```

### Daily commands

| Task | Command |
| --- | --- |
| Install dependencies | `uv sync` |
| Run CLI | `uv run my-cli --help` |
| Add runtime dependency | `uv add <package>` |
| Add development dependency | `uv add --dev <package>` |
| Test | `uv run pytest -q` |
| Lint | `uv run ruff check .` |
| Format | `uv run ruff format .` |
| Build | `uv build` |

The generated project installs itself as a package using `uv_build` with a `src/` layout. Its GitHub Actions workflow runs quality checks on pull requests and pushes to `main`.

## Test the template itself

```sh
uv run --no-project --with copier --with pytest pytest tests -q
```

The template tests generate temporary projects, inspect metadata and file names, and exercise `uv sync`, Ruff, pytest, the installed CLI, and `uv build`. The checks also run automatically in this repository's pull requests.

## Updating generated projects

Commit `.copier-answers.yml` and `uv.lock` in each generated application. Once this template has versioned release tags, run `copier update` inside a generated project to incorporate template improvements while retaining project changes.

## License

MIT. See [LICENSE](LICENSE).
37 changes: 37 additions & 0 deletions copier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
_min_copier_version: "9.0.0"
_subdirectory: template

project_name:
type: str
help: Distribution and CLI name (lowercase kebab-case)
default: my-python-cli
validator: >-
{% if not (project_name | regex_search('^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$')) %}
Use lowercase letters, numbers and single hyphens; start with a letter.
{% endif %}

description:
type: str
help: One-line description of the project
default: A small, dependable Python command-line tool.

author_name:
type: str
help: Name to include in the MIT license
default: Your Name

python_version:
type: str
help: Python version used for local development
choices:
- "3.11"
- "3.12"
- "3.13"
- "3.14"
default: "3.13"

copyright_year:
type: int
help: Copyright year for the MIT license
default: 2026
ask: false
29 changes: 29 additions & 0 deletions template/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
quality:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
- name: Install project
run: uv sync --group dev
- name: Lint
run: uv run --no-sync ruff check .
- name: Format
run: uv run --no-sync ruff format --check .
- name: Test
run: uv run --no-sync pytest -q
- name: Build distributions
run: uv build
14 changes: 14 additions & 0 deletions template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.venv/
__pycache__/
*.py[cod]
.pytest_cache/
.ruff_cache/
.coverage*
htmlcov/
build/
dist/
*.egg-info/
.env
.env.*
!.env.example
# Commit uv.lock for reproducible application builds.
1 change: 1 addition & 0 deletions template/.python-version.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ python_version }}
21 changes: 21 additions & 0 deletions template/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Agent guide

This is a standalone **uv**-managed Python CLI. Do not mix its environment, dependencies, or commands with other repositories.

## Working contract

1. Follow red/green/refactor: add a failing pytest test, implement the smallest change, then refactor.
2. Keep the CLI layer thin (`src/*/cli.py`). Put reusable logic in importable modules and test it directly.
3. Run commands from this repository's root using `uv run` or `uv sync`. Never use system-wide `pip install` for project dependencies.
4. Avoid unrelated refactors or changes to sibling repositories. Add dependencies via `uv add` / `uv add --dev`.
5. Before finishing, run `uv run pytest -q`, `uv run ruff check .`, `uv run ruff format --check .`, and `uv build`.
6. Commit `uv.lock` after the first `uv sync` and whenever dependencies change.

## Layout

- `src/`: installable Python package and CLI entry point
- `tests/`: automated behavioral tests
- `pyproject.toml`: one source of truth for metadata, dependencies, tooling, and entry points
- `.github/workflows/ci.yml`: pull-request quality gate

Keep changes focused, readable, and portable across Windows, Linux, and macOS.
21 changes: 21 additions & 0 deletions template/LICENSE.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) {{ copyright_year }} {{ author_name }}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions template/README.md.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# {{ project_name }}

{{ description }}

A lightweight Python CLI powered by **uv**, **Cyclopts**, **Rich**, **Ruff**, and **pytest**.

## Get started

Install [uv](https://docs.astral.sh/uv/getting-started/installation/), then:

```sh
uv sync
uv run {{ project_name }} --help
uv run {{ project_name }} greet Ada
```

Each project has its own `.venv` managed by uv. Commit `uv.lock` after the first `uv sync`.

## Development

```sh
uv run pytest -q
uv run ruff check .
uv run ruff format --check .
uv run ruff format .
uv build
```

The CLI entry point is `src/{{ project_name | replace('-', '_') }}/cli.py`. Put reusable logic in separate modules, and write a failing test before changing behavior. See [AGENTS.md](AGENTS.md) for agent conventions.

## Template updates

This project was generated with Copier. Keep `.copier-answers.yml` committed to support future updates once the template publishes versioned tags:

```sh
copier update
```

## License

MIT. See [LICENSE](LICENSE).
31 changes: 31 additions & 0 deletions template/pyproject.toml.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[project]
name = "{{ project_name }}"
version = "0.1.0"
description = {{ description | to_json }}
readme = "README.md"
requires-python = ">={{ python_version }}"
license = "MIT"
dependencies = [
"cyclopts>=3,<5",
"rich>=13,<15",
]

[project.scripts]
{{ project_name }} = "{{ project_name | replace('-', '_') }}.cli:app"

[build-system]
requires = ["uv_build>=0.13.6,<0.14"]
build-backend = "uv_build"

[dependency-groups]
dev = [
"pytest>=8,<10",
"ruff>=0.12",
]

[tool.pytest.ini_options]
testpaths = ["tests"]

[tool.ruff]
line-length = 100
target-version = "py{{ python_version | replace('.', '') }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Python command-line application."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Allow running the CLI with ``python -m``."""

from .cli import app

if __name__ == "__main__":
app()
17 changes: 17 additions & 0 deletions template/src/{{ project_name | replace('-', '_') }}/cli.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Command-line interface.

Add commands here. Keep reusable business logic in separate modules.
"""

from cyclopts import App
from rich.console import Console
from rich.markup import escape

app = App(help={{ description | to_json }})
console = Console()


@app.command
def greet(name: str = "World") -> None:
"""Greet someone by name."""
console.print(f"Hello, [bold cyan]{escape(name)}[/bold cyan]!")
31 changes: 31 additions & 0 deletions template/tests/test_cli.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""CLI contract tests: exercise the installed module as a user would."""

import subprocess
import sys


def invoke(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[sys.executable, "-m", "{{ project_name | replace('-', '_') }}", *args],
capture_output=True,
text=True,
check=False,
)


def test_help_lists_greet_command() -> None:
result = invoke("--help")
assert result.returncode == 0, result.stderr
assert "greet" in result.stdout


def test_greet_uses_provided_name() -> None:
result = invoke("greet", "Ada")
assert result.returncode == 0, result.stderr
assert "Hello, Ada!" in result.stdout


def test_greet_escapes_rich_markup() -> None:
result = invoke("greet", "[red]Ada[/red]")
assert result.returncode == 0, result.stderr
assert "Hello, [red]Ada[/red]!" in result.stdout
2 changes: 2 additions & 0 deletions template/{{ _copier_conf.answers_file }}.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by Copier. Do not edit manually; use `copier update`.
{{ _copier_answers | to_nice_yaml -}}
Loading
Loading