Build fast CLI applications with Clap in Rust
✓Works with OpenClaudeYou are a Rust CLI developer. The user wants to build fast command-line applications using the Clap argument parsing library.
What to check first
- Run
cargo --versionto confirm Rust is installed - Check
Cargo.tomland verifyclapdependency withderivefeature:clap = { version = "4.4", features = ["derive"] }
Steps
- Add Clap to your
Cargo.tomlwith thederivefeature enabled for proc macro support - Import
Parserfromclap::Parserat the top of yourmain.rs - Define a struct with
#[derive(Parser)]to represent your CLI structure with#[command]metadata - Use
#[arg]attributes on struct fields to configure individual arguments with long names, short flags, and help text - Create subcommands by nesting enums with
#[command(subcommand)]if your CLI needs multiple modes - Call
parse()on your struct inmain()to parsestd::env::args()and get a populated instance - Match on subcommand enum variants or access fields directly to implement your CLI logic
- Use
.get_matches()and.subcommand()for manual parsing if dynamic behavior is needed
Code
use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "myapp")]
#[command(about = "A fast CLI tool", long_about = None)]
#[command(version = "0.1.0")]
#[command(author = "Your Name")]
struct Cli {
/// Input file path
#[arg(short, long, value_name = "FILE")]
input: Option<PathBuf>,
/// Output file path
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
/// Verbosity level (0-3)
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
/// Enable quiet mode
#[arg(short, long)]
quiet: bool,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
/// Process files
Process {
/// File to process
#[arg(value_name = "FILE")]
file: PathBuf,
/// Number of threads
#[arg(short, long, default_value = "1")]
threads: usize,
},
/// Show statistics
Stats {
/// Include detailed breakdown
#[arg(short, long)]
detailed: bool,
},
}
fn main() {
let cli = Cli::parse();
if let Some(Commands::Process { file, threads }) = cli.command {
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Rust Skills
Other Claude Code skills in the same category — free to download.
Rust API
Scaffold Rust web API with Actix-web or Axum
Rust Testing
Set up Rust unit and integration testing
Rust WASM
Build WebAssembly modules with Rust and wasm-pack
Rust Error Handling
Implement proper error handling with thiserror and anyhow
Rust Async
Implement async programming with Tokio runtime
Rust Serde
Serialize and deserialize data with Serde in Rust
Rust Error Handling Patterns
Build idiomatic error types using thiserror for libraries and anyhow for applications
Rust Async with Tokio
Write async Rust services with Tokio runtime for high-performance I/O
Want a Rust skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.