Sub topic 4
Positional arguments
This chapter assumes you can define a #[derive(Parser)] struct and run it with cargo run. It also assumes you know Option, Vec, and how Result works in Rust.
What makes an argument positional
A positional argument is a value identified by where it appears on the command line, not by a name like --filter. In cp a.txt b.txt, the first value is the source and the second is the destination.
In a derived struct, a field is positional unless you add #[arg(short)] or #[arg(long)]. Fields are matched to values in the order you declare them. Reordering the fields changes the command line your program accepts.
use clap::Parser;
use std::path::PathBuf;
/// Search files for a pattern.
#[derive(Parser, Debug)]
struct Cli {
/// Text to look for
pattern: String,
/// Files to search
#[arg(required = true)]
files: Vec<PathBuf>,
}
fn main() {
let cli = Cli::parse();
println!("{cli:?}");
}If the binary is called finder, this call fills both fields:
$ finder fn src/main.rs src/lib.rs
Cli { pattern: "fn", files: ["src/main.rs", "src/lib.rs"] }The field type sets the arity
Clap reads the Rust type to decide how many values a positional accepts and whether it is required.
| Field type | Behavior |
|---|---|
| `String`, `PathBuf`, `u32` | Required, exactly one value |
| `Option<T>` | Optional, at most one value |
| `Vec<T>` | Zero or more values |
| `Vec<T>` with `required = true` | One or more values |
In the example above, Vec<PathBuf> alone would accept zero files, so required = true makes clap demand at least one. If you leave a required value out, clap prints an error and exits with code 2. The exact wording varies by clap version:
$ finder fn
error: the following required arguments were not provided:
<FILES>...
Usage: finder <PATTERN> <FILES>...Ordering rules
Clap checks your definition for impossible layouts. These rules cause most positional-argument panics:
- A positional that takes many values (
Vec<T>) must be the last positional. Otherwise clap cannot tell where it ends. - A required positional cannot follow an optional one, because the parser cannot tell which value was skipped.
- Only one positional can take many values.
These mistakes are bugs in your definition, not in user input. Clap reports them as a panic when the parser is built in a debug build. Later chapters show how debug_assert catches them in a test.
Values that start with a dash
By default, clap reads -5 or -draft.txt as an option. The user can end option parsing with --. Everything after it is treated as positional:
$ finder -- -draft src/main.rsIf a positional should accept negative numbers, such as a temperature, set allow_negative_numbers = true on it. To accept any dash-prefixed value, use allow_hyphen_values = true. The second setting hides typos in option names, because a misspelled --verbos would then become a value. Use it only when you must.
Exercise
You add a field output: Option<PathBuf> after files: Vec<PathBuf> in the struct above. What happens when the parser is built, and why?
Show answer
Clap panics in a debug build because a positional that takes many values must be the last positional. Clap cannot tell where files stops and output starts. To fix it, make output an option (#[arg(long)]) or put it before files.