| name | clap |
| description | Build production Rust CLIs with Clap: subcommands, config layering, validation, exit codes, shell completions, and testable command surfaces |
| user-invocable | false |
| disable-model-invocation | true |
| version | 1.0.0 |
| category | toolchain |
| author | Claude MPM Team |
| license | MIT |
| progressive_disclosure | {"entry_point":{"summary":"Create ergonomic, testable Rust CLIs using Clap derive, subcommands, and config layering (CLI + env + config file)","when_to_use":"When building Rust CLI tools that need reliable argument parsing, good help UX, strong validation, and automated tests","quick_start":"1. Add clap (derive) 2. Define Args + Subcommand 3. Parse once in main 4. Map errors to exit codes 5. Test with assert_cmd"},"token_estimate":{"entry":120,"full":4500}} |
| context_limit | 700 |
| tags | ["rust","cli","clap","config","testing"] |
| requires_tools | [] |
Clap (Rust) - Production CLI Patterns
Overview
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.
Quick Start
Minimal CLI
✅ Correct: derive Parser
use clap::Parser;
#[derive(Parser, Debug)]
#[command(name = "mytool", version, about = "Example CLI")]
struct Args {
#[arg(long)]
verbose: bool,
#[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();
}
Subcommands (real tools)
Model multi-mode CLIs with subcommands and shared global flags.
✅ Correct: global flags + subcommands
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)]
{
verbose: ,
config: <>,
cmd: Command,
}
{
Serve { port: },
Migrate { mode: Mode },
}
{ Up, Down }
() {
= Args::();
args.cmd {
Command::Serve { port } => (, port),
Command::Migrate { mode } => (, mode),
}
}