Sub topic 6
Help and version output
This chapter assumes you can build the derived parser from the earlier chapters: a struct with #[derive(Parser)], positional arguments, and options. Here you will see how clap builds --help and --version output and how to shape it.
What clap gives you for free
Any derived parser accepts -h, --help, -V, and --version. The --help and -h flags exist by default. --version and -V appear only when you add version to the #[command(...)] attribute. Clap prints the text to stdout and exits with code 0, so these are successes, not errors.
use clap::Parser;
use std::path::PathBuf;
/// Print lines of a file that contain a pattern.
///
/// The file is read line by line. Without --filter,
/// every line is printed.
#[derive(Parser, Debug)]
#[command(version, after_help = "Example: linegrep notes.txt -f todo")]
struct Cli {
/// File to search
path: PathBuf,
/// Only print lines containing this text
///
/// The match is case-sensitive.
#[arg(short, long, value_name = "TEXT")]
filter: Option<String>,
/// Print the search settings before the results
#[arg(short, long, help_heading = "Output")]
verbose: bool,
}
fn main() {
let cli = Cli::parse();
println!("{cli:?}");
}Assume the Cargo package is named linegrep with version = "0.1.0". Running linegrep --version prints linegrep 0.1.0. Clap reads both values from Cargo.toml at compile time. Bump the version there and the output follows.
Where the text comes from
Clap fills the help screen from these sources:
- Doc comments (
///) on the struct set the program description. Doc comments on a field set that argument's help. versionwith no value readsCARGO_PKG_VERSION.value_namechanges the placeholder shown for a value, here--filter <TEXT>.help_headinggroups arguments under their own heading in the help screen.after_helpandbefore_helpadd free text below or above the generated sections.
You can override a doc comment by writing #[command(about = "...")] or #[arg(help = "...")]. Use an explicit string when the doc comment is aimed at Rust readers rather than at users of the tool.
Short and long help
Clap keeps two versions of each description. A doc comment's first paragraph is the short text. The whole comment is the long text.
| Flag | Shows | Typical use |
|---|---|---|
| `-h` | First paragraph only | Quick reminder |
| `--help` | Full comment, with extra paragraphs | Detailed reference |
In the example, -h shows only "Only print lines containing this text" for --filter. --help also shows "The match is case-sensitive." Put the one-line summary first and the detail after a blank /// line.
Common mistakes
- Forgetting
version. Without it,--versionis rejected as an unknown argument. Add it to#[command(...)]. - Writing doc comments for developers. Text like "The parsed path" ends up in the help screen. Write for the person at the terminal.
- Defining your own
-hor-V. Clap reserves these short names, and a clash makesdebug_assertfail. Rename the short option, or disable the built-in flag withdisable_help_flag = true.
Exercise
A struct has the doc comment "Count words in a file." followed by a blank /// line and a second paragraph explaining the counting rules. Which flag shows the second paragraph, and what exit code does the program return after printing it?
Show answer
--help shows the second paragraph, while -h shows only the first. Help output is not an error, so the program exits with code 0.