ScriptsApr 13, 2026·3 min read

Clap — Command Line Argument Parser for Rust

Clap is the most popular CLI argument parser for Rust. It provides derive macros for declarative argument definitions, rich help generation, shell completions, subcommands, and comprehensive validation — the Rust equivalent of Python argparse on steroids.

TL;DR
Clap provides derive macros and a builder API for parsing CLI arguments with rich help and validation in Rust.
§01

What it is

Clap is the most widely used CLI argument parser for Rust. It offers two styles: a derive macro approach where you annotate a struct with argument definitions, and a builder API for dynamic or complex argument trees. Clap handles help text generation, shell completions, subcommands, argument groups, value validation, and error formatting.

Clap is the Rust equivalent of Python argparse but with compile-time validation and significantly richer features. It suits anyone building command-line tools in Rust, from simple scripts to complex multi-subcommand applications like cargo itself.

§02

How it saves time or tokens

Without Clap, you parse std::env::args manually, write your own help text, handle missing or invalid values, and build shell completion scripts by hand. Clap generates all of this from your struct definition. A CLI with ten flags, three subcommands, and bash/zsh/fish completions takes minutes to define instead of hours.

§03

How to use

  1. Add Clap with derive support: cargo add clap --features derive.
  2. Define a struct annotated with #[derive(Parser)] and use #[arg(...)] attributes for each field.
  3. Call Cli::parse() in main -- Clap reads arguments, validates them, and returns a populated struct.
§04

Example

use clap::Parser;

#[derive(Parser)]
#[command(name = 'myapp', about = 'A sample CLI application')]
struct Cli {
    /// Name of the person to greet
    #[arg(short, long)]
    name: String,

    /// Number of times to greet
    #[arg(short, long, default_value_t = 1)]
    count: u8,
}

fn main() {
    let cli = Cli::parse();
    for _ in 0..cli.count {
        println!('Hello {}!', cli.name);
    }
}
$ cargo run -- --name Alice --count 3
Hello Alice!
Hello Alice!
Hello Alice!
§05

Related on TokRepo

§06

Common pitfalls

  • Forgetting the derive feature flag results in cryptic compile errors since the Parser derive macro is feature-gated.
  • Using String for optional arguments without wrapping in Option<String> makes Clap require the argument, which confuses users who expect it to be optional.
  • Not specifying value_parser for custom types leads to unhelpful error messages when validation fails.

Frequently Asked Questions

What is the difference between Clap derive and builder APIs?+

The derive API uses Rust attributes on a struct to declare arguments -- concise and type-safe. The builder API constructs argument definitions programmatically at runtime, useful when arguments depend on configuration or plugins. Most projects use derive because it catches errors at compile time and generates less boilerplate.

Does Clap generate shell completions?+

Yes. The clap_complete crate generates completion scripts for bash, zsh, fish, PowerShell, and elvish. You add a hidden subcommand that outputs the completion script, or generate it at build time and ship it with your binary.

How does Clap handle subcommands?+

Define an enum with `#[derive(Subcommand)]` where each variant represents a subcommand with its own argument struct. Clap routes the user input to the correct variant and parses its arguments independently. Nested subcommands are supported by nesting enums.

Can I use Clap for environment variable fallbacks?+

Yes. Use `#[arg(env = 'MY_VAR')]` and Clap reads the environment variable if the user does not provide the argument on the command line. The help text automatically shows the env var name. This is useful for container deployments where config comes from the environment.

Is Clap the same as structopt?+

Structopt was a popular derive layer on top of Clap 2. Starting with Clap 3, the derive functionality was merged into Clap itself, making structopt unnecessary. If you were using structopt, migrate to `clap::Parser` with minimal changes.

Citations (3)
  • Clap GitHub— Clap is the most popular argument parser for Rust
  • Clap Documentation— Supports derive and builder APIs for argument definition
  • clap_complete Docs— Shell completions via clap_complete for bash, zsh, fish, and PowerShell

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets