在 Manus 中运行任何 Skill
一键导入
一键导入
一键在 Manus 中运行任何 Skill
开始使用build-rust-cli
星标9
分支0
更新时间2026年2月28日 23:10
Best practices for building CLI tools in Rust
安装
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
SKILL.md
readonly菜单
Best practices for building CLI tools in Rust
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Create a new claudini release by tagging and pushing
Create a new branch, commit all changes, push, and open a PR.
Wait for CI checks to pass on a PR, then merge it and switch back to main.
| name | build-rust-cli |
| description | Best practices for building CLI tools in Rust |
Reference guide for building and testing CLI applications in Rust. Synthesized from the Rust CLI Book, clap examples, and ripgrep.
main.rs thin. Parse args, load settings, dispatch to command handlers. Business logic lives in dedicated modules (commands.rs, client.rs, etc.).'a lifetime params borrowing from the caller.#[derive(Parser)] and #[derive(Subcommand)] for structured, type-safe argument parsing.version to #[command()] so --version works.ValueEnum for constrained string choices (e.g., priority levels, output formats). This gives free validation, help text, and tab completion instead of opaque API errors from invalid values.///) on struct fields — clap uses them as help text automatically.#[arg(short, long)] for common flags.#[arg(long, default_value = "...")] for options with sensible defaults.Vec<T> for repeatable flags (e.g., --label id1 --label id2).anyhow for application-level errors. Use thiserror only for library crates..context() or .with_context() to give errors meaning. "failed to read config" is better than "No such file or directory"..unwrap() in production paths. Use ? with context instead.Result from main():
fn main() {
if let Err(err) = run() {
eprintln!("error: {err:#}");
std::process::exit(1);
}
}
This lets you style the error output (red, bold prefix) and control the format.indicatif) default to stderr — keep it that way.--json flag for machine-readable output. When active, disable spinners and colors. Use serde_json::to_string_pretty() for human-readable JSON.comfy_table for tabular human output with UTF-8 borders and colored headers.console crate for styled text (bold, colors, dim).IsTerminal trait, stable since Rust 1.70) to auto-disable colors when piped.Default impl)APP_KEY, APP_BASE_URL, etc.)#[cfg(test)])wiremock for HTTP mocking — create a MockServer, mount expectations, pass the mock URI as the base URL.tempfile::TempDir for filesystem tests.temp-env::with_vars + serial_test::serial for environment variable tests (prevents contamination between tests).// ── Construction ──, // ── Error handling ──.tests/cli.rs)assert_cmd to run the compiled binary as a subprocess.predicates for composable stdout/stderr assertions.wiremock::matchers::query_param to verify filters reach the server.plane_cmd_with(mock_uri)) to reduce boilerplate in tests that need a mock server..expect(1) on mocks when you want to verify the request was actually made.trycmd/snapbox): write expected input/output as files, run them automatically. Good for regression testing help text and output format.main() returns Err).std::process::exit(code) when you need explicit control.| Purpose | Crate |
|---|---|
| Arg parsing | clap (derive) |
| Error handling | anyhow |
| Serialization | serde + serde_json |
| HTTP client | reqwest (json, rustls-tls) |
| Async runtime | tokio |
| Terminal colors | console |
| Progress bars | indicatif |
| Tables | comfy-table |
| HTTP mocking | wiremock |
| CLI testing | assert_cmd + predicates |
| Temp files | tempfile |
| Env var testing | temp-env + serial_test |
cargo fmt and cargo clippy --all-targets -- -D warnings before every commit.edition = "2024" (or latest stable) in Cargo.toml.rustls-tls instead of native-tls for easier cross-compilation.default-features = false on reqwest to avoid pulling in OpenSSL.