rust-cli
Original:🇺🇸 English
Translated
General guidance for building Rust CLI programs (clap/anyhow/tracing/serde_json), with agent-friendly patterns: JSON output mode, stdout/stderr separation, predictable exit codes, integration testing, and a cargo-release based release workflow.
2installs
Sourcetumf/skills
Added on
NPX Install
npx skill4agent add tumf/skills rust-cliTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →rust-cli - Build Maintainable Rust CLIs
Use this skill when you need to design or implement a Rust CLI with production-grade ergonomics and automation.
For language-agnostic OSS publication/release hygiene (LICENSE/SECURITY.md, release notes, CI policy, repo bootstrap conventions), consult the skill.
oss-publishDefaults (Agent-Friendly)
- CLI parsing: derive.
clap - Error handling: (or
anyhowfor library-style errors).thiserror - Logging/diagnostics: +
tracing(write logs to stderr).tracing-subscriber - Structured output: +
serde.serde_json
Companion Skill: agentic-cli-design
If this CLI will be operated by AI agents and/or automation, also consult the skill.
Borrow these concepts: machine-readable output, non-interactive operation, idempotent commands, safe-by-default behavior, observability, and introspection.
agentic-cli-designCross-platform testing pitfalls
Home directory override (Windows limitation)
On Windows, calls Windows API () directly; setting or environment variables does not override the returned path. This differs from Unix/Linux/macOS, where typically reads .
directories::BaseDirsSHGetKnownFolderPathHOMEUSERPROFILEdirectories$HOME| Platform | Behavior | Override via env? |
|---|---|---|
| Unix/Linux/macOS | Reads | ✅ Yes |
| Windows | Calls Win32 API ( | ❌ No |
Test design patterns:
rust
// Pattern 1: Unix-only test (recommended for HOME override)
#[test]
#[cfg(not(target_os = "windows"))]
fn test_home_override() {
let temp = TempDir::new().unwrap();
env::set_var("HOME", temp.path());
// Test code that uses directories::BaseDirs
}
// Pattern 2: Platform-specific logic
#[test]
fn test_cross_platform_home() {
#[cfg(unix)]
{
env::set_var("HOME", "/tmp/test");
// Unix-specific test
}
#[cfg(windows)]
{
// Windows: use explicit paths or dependency injection
let test_dir = PathBuf::from("C:\\temp\\test");
// Windows-specific test
}
}
// Pattern 3: Dependency injection (best for portability)
fn install_skill_to_path(base_dir: &Path, skill_name: &str) {
// Accept path directly, avoid BaseDirs in implementation
}
#[test]
fn test_install_with_explicit_path() {
let temp = TempDir::new().unwrap();
install_skill_to_path(temp.path(), "my-skill");
// Portable across all platforms
}Other common cases with similar issues:
| Function | Platform behavior | Override via env? |
|---|---|---|
| Calls OS API | ❌ No |
| Calls OS API | ❌ No |
| Windows: API, Unix: XDG vars | Partial (Unix only) |
Recommendation: Design functions to accept explicit paths where testability matters; use / only in top-level CLI entrypoint or well-isolated modules.
BaseDirsProjectDirsFlaky tests: time-based cutoffs (SystemTime + second precision)
If you store timestamps as Unix seconds () and compute a cleanup cutoff using , tests can become flaky across platforms.
This often passes on Linux/macOS and fails on Windows due to timing/resolution differences.
i64SystemTime::now()Typical failure mode (SQLite example):
- inserts
record()(e.g.created_at = now_secs()).1707408142 - a moment later, computes
cleanup_old_entries(0)(e.g.cutoff = now_secs()).1707408143 - SQL uses which matches the freshly inserted row (
WHERE created_at < cutoff).1707408142 < 1707408143
Recommended fixes (pick one that matches intended semantics):
- Tests: avoid boundary conditions; use a large margin (e.g. instead of
cleanup_old_entries(1)), and optionally insert a small sleep between operations to avoid same-second edges.0 - Implementation: define semantics explicitly (often a no-op), or inject a clock so tests can be deterministic. If you keep second-precision storage, be careful with
days <= 0vs<and how you define "older than N days".<=
Example test adjustment (stable across platforms):
rust
#[test]
fn test_cleanup_old_entries() {
let (ledger, _temp) = create_test_ledger();
ledger
.record("test-1", "hash-1", "tweet-1", "success")
.unwrap();
// Ensure the cutoff is not computed in the exact same instant.
std::thread::sleep(std::time::Duration::from_millis(10));
// Use a safe margin: a fresh entry should not be deleted.
let deleted = ledger.cleanup_old_entries(1).unwrap();
assert_eq!(deleted, 0);
let entry = ledger.lookup("test-1").unwrap();
assert!(entry.is_some());
}Other common cross-platform differences
| Feature | Unix/macOS | Windows | Testing approach |
|---|---|---|---|
| Path separator | | | Always use |
| Line endings | | | Explicitly specify in tests or use |
| Executable extension | none | | Use |
| Case sensitivity | Yes | No | Test with varied cases on Windows |
| Temp directory | | | Use |
Rust-specific workflow (minimal)
- Implement a JSON output mode (e.g. ) with stdout reserved for the result.
--json - Send logs/diagnostics to stderr (e.g. via ).
tracing - Use predictable exit codes and integration tests that execute the compiled binary.
- For repo-wide release/quality gate conventions, see .
oss-publish
Release workflow (cargo-release)
Use to bump versions, create git tags, and (optionally) publish to crates.io.
cargo-releaseInstall:
bash
cargo install cargo-releaseBump version + tag (no publish)
This is a safe default when you want to control publishing manually:
bash
# Patch: 0.1.0 -> 0.1.1
cargo release patch --execute --no-confirm --no-publish
# Minor: 0.1.0 -> 0.2.0
cargo release minor --execute --no-confirm --no-publish
# Major: 0.1.0 -> 1.0.0
cargo release major --execute --no-confirm --no-publishNotes:
- makes the command non-interactive (agent/CI friendly). Use without it for a safety prompt.
--no-confirm - keeps crates.io publishing as an explicit step.
--no-publish
After bumping/tagging, publish explicitly:
bash
cargo publishPublish preflight checks
Before publishing (especially in automation), prefer these checks:
bash
cargo fmt
cargo clippy -- -D warnings
cargo test
# Ensures the package can be built as it will be uploaded
cargo publish --dry-runOptional: publish from a tag
If you need to publish an already-created tag:
bash
git checkout <tag>
cargo publishRecommendation: keep the release workflow simple.
In most repos, publishing from a clean working tree on the release commit (the one that was tagged) is sufficient.
Templates
- Crate selection:
rust-cli/references/crates.md - Copy/paste scaffolding (Cargo.toml, main.rs, tests, prek config):
rust-cli/references/templates.md