This walks you through building production-grade Rust CLIs with Clap's derive macros. It covers the full stack: subcommands with global flags, config layering (CLI over env over file), proper exit codes, and testing your binary surface with assert_cmd. The progressive disclosure is solid, token estimate looks right at 4500 for the full context. Most valuable for the subcommand patterns and the config merge precedence rules, which are easy to get wrong. Skips the builder API entirely and goes straight to derive, which is the right call for 2024. If you're past "hello world" CLIs and need something maintainers won't hate, this gives you the playbook.
$ npx -y skills add bobmatnyc/claude-mpm-skills --skill clap --agent claude-codeInstalls into .claude/skills of the current project.
Clap provides declarative command-line parsing with strong help output, validation, and subcommand support. Use it to build CLIs with predictable UX and testable execution paths.
✅ Correct: derive Parser
use clap::Parser;
#[derive(Parser, Debug)]
#[command(name = "mytool", version, about = "Example CLI")]
struct Args {
/// Enable verbose output
#[arg(long)]
verbose: bool,
/// Input file path
#[arg(value_name = "FILE")]
input: String,
}
fn main() {
let args = Args::parse();
if args.verbose {
eprintln!("verbose enabled");
}
println!("input={}", args.input);
}
❌ Wrong: parse multiple times
fn main() {
let _a = Args::parse();
let _b = Args::parse(); // duplicate parsing and inconsistent behavior
}
Model multi-mode CLIs with subcommands and shared global flags.
✅ Correct: global flags + subcommands
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)]
struct Args {
#[arg(long, global = true)]
verbose: bool,
Sponsors