Rust CLI Vibe Coding: Clap v4 and Integration Test Loop
Rust CLI tooling configuration for intermediate developers, covering Clap subcommands, borrow checking, Tokio IO, and assert_cmd testing, with an overall completion rate of 79%.

Completion Score
Overall completion rate of 79%: AI excels in generating Clap subcommand skeletons, help documentation, and integration test templates, quickly producing compilable code. Borrow checking and lifetime convergence still require manual review, as do asynchronous IO and error boundaries.
Dimension breakdown
AI can stably generate derive subcommands and about text, resulting in high help output accuracy.
AI can provide ownership suggestions but often requires manual correction of lifetime annotations and borrow conflicts.
AI can generate Tokio skeletons, but blocking calls and error propagation require manual verification.
AI can generate assert_cmd test templates, but failure path coverage and assertion rigor require manual reinforcement.
Human checkpoint
Before merging, manually review borrow checking and lifetime errors, verify that anyhow error boundaries are clear, confirm that integration tests cover failure paths, and ensure that --help output matches actual behavior.
Upgrade / downgrade path
If budget allows, upgrade to Tokio full features + tokio-test + stricter clippy configuration; if budget is tight, downgrade to synchronous IO + basic assert_cmd tests, retaining Clap and anyhow structures.
When to use & prerequisites
The target audience is intermediate developers familiar with Rust basics who wish to use AI to assist in building CLI tools. Environment requirements: Cursor IDE, Rust toolchain (cargo), Clap v4, Tokio, assert_cmd. Typical scenarios include building a CLI with subcommands from scratch or adding integration tests and error handling to existing tools.
Ready-to-copy configs
约束 Cursor 在 Rust CLI 开发中使用 Clap v4 子命令、anyhow 错误处理与 cargo test 集成测试的规则。
---
description: Rust CLI vibe coding rules for Clap v4, anyhow, Tokio, cargo test
globs: ["**/*.rs"]
alwaysApply: true
---
# Rust CLI Rules
- Use Clap v4 derive API for all CLI parsing; define subcommands explicitly (e.g., `Command::new("app").subcommand(...)`).
- Every subcommand must have `about`/`long_about` help text and `#[command(name = "...")]`.
- Prefer `anyhow::Result<T>` for fallible functions; avoid `unwrap()`/`expect()` in library code paths.
- Async entry points use `#[tokio::main]`; keep blocking IO out of async tasks.
- Integration tests live in `tests/` and use `assert_cmd` + `predicates`; do not rely on stdout snapshots without explicit assertions.
- Run `cargo fmt`, `cargo clippy -- -D warnings`, and `cargo test` before considering a change complete.
- Keep `Cargo.toml` dependencies minimal; pin major versions (clap = "4", tokio = { version = "1", features = ["full"] }).为 AI 编码助手提供仓库级上下文、命令与验收标准。
# AGENTS.md
## Project
Rust CLI tool built with Clap v4, Tokio, anyhow, and assert_cmd integration tests.
## Commands
- Build: `cargo build`
- Test: `cargo test`
- Lint: `cargo clippy -- -D warnings`
- Format: `cargo fmt`
## Conventions
- CLI structure: Clap v4 derive, one subcommand per user action.
- Errors: anyhow in app code; map to user-friendly messages at the boundary.
- Async: Tokio runtime; avoid blocking calls inside async blocks.
- Tests: `tests/cli.rs` with assert_cmd; cover success, invalid args, and IO failure paths.
## Acceptance
- `cargo test` passes.
- `cargo clippy -- -D warnings` is clean.
- `--help` output is accurate and complete.
- No `unwrap()` in non-test library code.AI Vibe Coding in 4 steps
- 1
Build Clap v4 Subcommand Skeleton
Goal: Define subcommands and help documentation using the derive API.
PromptGenerate a Clap v4 derive structure for a Rust CLI: define an `app` command with two subcommands, `run` and `status`, each with `about` and `long_about` attributes, returning anyhow::Result. Keep the code compilable and do not use unwrap.Verify:
Run `cargo run -- --help` and `cargo run -- run --help` to confirm the output includes subcommands and help text. - 2
Converge Borrow Checking and Error Boundaries
Goal: Eliminate unwrap and convert anyhow errors at boundaries.
PromptReview the current code, replacing unwrap/expect in library paths with anyhow::Result, converting errors to user-friendly messages at the main boundary, and fixing borrow checking and lifetime errors.Verify:
Run `cargo clippy -- -D warnings` and `cargo test` to confirm no warnings and passing tests. - 3
Integrate Tokio Asynchronous IO
Goal: Handle file and network IO with Tokio to avoid blocking.
PromptRefactor the CLI's IO section to use Tokio asynchronous implementation: use #[tokio::main], use tokio::fs for file reading, and use reqwest or tokio::net for network requests, ensuring errors propagate via anyhow.Verify:
Run `cargo test` to confirm async paths have test coverage and `cargo clippy` has no blocking call warnings. - 4
Complete assert_cmd Integration Tests
Goal: Cover success, invalid arguments, and IO failure paths with integration tests.
PromptWrite integration tests in tests/cli.rs using assert_cmd and predicates: cover success paths, invalid arguments, and three IO failure scenarios, asserting stdout/stderr and exit codes.Verify:
Run `cargo test --test cli` to confirm all integration tests pass and failure paths have assertions.
Pitfalls & fixes
- Pitfall 1: Calling blocking IO in async blocks, causing runtime stalls. Fix: Use tokio::fs or spawn_blocking.
- Pitfall 2: Using unwrap in library code, causing panics. Fix: Switch to anyhow::Result and convert at boundaries.
- Pitfall 3: Integration tests only assert stdout, ignoring exit codes and stderr. Fix: Use assert_cmd to assert exit code, stdout, and stderr.
FAQ
What are the main differences between Clap v4 and v3?
Clap v4 improves the derive API and subcommand structure, resulting in more consistent help documentation generation. Migration requires checking changes in #[command] and #[arg] attributes.
How to choose between anyhow and thiserror?
anyhow is suitable for application-level error propagation, while thiserror is better for defining structured errors at the library level. CLI tools typically use anyhow, while library code uses thiserror.
How to cover failure paths with assert_cmd tests?
Use assert_cmd's .failure() and .stderr() to assert failure scenarios, and check exit codes and error messages.


