Clap - Add help text for each command line parameter in Rust

help arg

Part of the series about Clap, the Command Line Argument Parser of Rust

As we already saw in the first article in the series Clap automatically provides a default help that can be shown using either the --help or the -h command line parameter. We can easily add extra explanation to each one of the flags by including the help attribute in the arg.

#[arg(long, default_value="", help="The name of the host")]
host: String,

Dependencies in the Cargo.toml file


name = "help-text"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at

clap = { version = "4.4.11", features = ["derive"] }

The full example


use clap::Parser;

struct Cli {
    #[arg(long, default_value="", help="The name of the host")]
    host: String,

fn main() {
    let args = Cli::parse();

Showing the help

$ cargo run -q -- -h
Usage: help-text [OPTIONS]

      --host <HOST>  The name of the host [default:]
  -h, --help         Print help

Related Pages

Clap - getting started accepting command line parameters in Rust
Clap - Command Line Argument Parser for Rust


Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo