Introduction
Clap is to Rust what Cobra is to Go — the standard framework for building command-line interfaces. Its derive macro approach lets you define CLI arguments as Rust structs with attributes, and Clap generates the parsing, validation, help text, and shell completions automatically. Type safety is enforced at compile time.
With over 16,000 GitHub stars, Clap powers the CLI of tools like ripgrep, bat, fd, starship, cargo (itself), and hundreds of other Rust applications. It is the most downloaded crate on crates.io.
What Clap Does
Clap parses command-line arguments into typed Rust values. You define your CLI structure using derive macros on structs and enums, and Clap handles: argument parsing, type coercion, validation, default values, environment variable fallback, help/version generation, and shell completion scripts.
Architecture Overview
[CLI Arguments]
myapp serve --port 8080 -v
|
[Clap Parser]
Derive macro or Builder API
|
[Typed Rust Values]
Cli { port: 8080, verbose: true }
Compile-time type safety
|
[Auto-Generated]
--help text
--version
Shell completions
Man pages
Error messagesSelf-Hosting & Configuration
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser)]
#[command(version, about = "My CLI tool")]
struct Cli {
#[command(subcommand)]
command: Commands,
/// Output format
#[arg(short, long, value_enum, default_value_t = Format::Text)]
format: Format,
}
#[derive(Subcommand)]
enum Commands {
/// Start the server
Serve {
/// Port to listen on
#[arg(short, long, default_value_t = 8080)]
port: u16,
/// Bind address
#[arg(long, default_value = "127.0.0.1")]
host: String,
},
/// Run database migrations
Migrate {
/// Migration direction
#[arg(value_enum)]
direction: Direction,
},
}
#[derive(Clone, ValueEnum)]
enum Format { Text, Json, Yaml }
#[derive(Clone, ValueEnum)]
enum Direction { Up, Down }
fn main() {
let cli = Cli::parse();
match cli.command {
Commands::Serve { port, host } => {
println!("Starting server on {}:{}", host, port);
}
Commands::Migrate { direction } => {
println!("Running migration...");
}
}
}Key Features
- Derive Macros — define CLI as Rust structs with #[derive(Parser)]
- Type Safety — arguments parsed to Rust types at compile time
- Subcommands — nested command structure via enums
- Validation — range checks, custom validators, conflicts, requirements
- Shell Completions — generate bash, zsh, fish, PowerShell completions
- Env Var Fallback — read values from environment variables
- Help Generation — auto-generated colored help text
- Value Enums — restrict arguments to predefined values
Comparison with Similar Tools
| Feature | Clap | Cobra (Go) | Click (Python) | argparse (Python) | structopt |
|---|---|---|---|---|---|
| Language | Rust | Go | Python | Python | Rust |
| Approach | Derive macros | Builder | Decorators | Builder | Derive (deprecated) |
| Type Safety | Compile-time | Runtime | Runtime | Runtime | Compile-time |
| Subcommands | Yes (enums) | Yes | Yes | Yes | Yes |
| Completions | Yes (clap_complete) | Yes | Limited | No | Yes |
| Validation | Rich | Basic | Rich | Basic | Rich |
| Note | Standard for Rust | Standard for Go | Standard for Python | stdlib | Merged into Clap |
FAQ
Q: Clap derive vs builder API — which should I use? A: Use the derive API for almost everything — it is more concise and type-safe. Use the builder API only when you need dynamic argument construction at runtime.
Q: What happened to structopt? A: structopt was merged into Clap 3+. The derive macro API in Clap is the successor. Migrate by changing your imports from structopt to clap.
Q: How do I generate shell completions? A: Add clap_complete as a dependency and generate completion scripts at build time or via a hidden "completions" subcommand.
Q: Can Clap read from config files? A: Clap focuses on CLI arguments. For config files, use the config or figment crates and merge with Clap args. Or use clap with env var support as a bridge.
Sources
- GitHub: https://github.com/clap-rs/clap
- Documentation: https://docs.rs/clap
- Crate: https://crates.io/crates/clap
- Created by Kevin Knapp
- License: MIT / Apache-2.0