Sub topic 1
What a command-line interface is made of
Every command-line program receives the same raw input: a list of text strings. This chapter names the parts of that list. clap's job in later chapters is to turn those parts into typed Rust values.
Prerequisites
This chapter assumes you know the following:
- Basic Rust: structs, enums,
Option, andResult. - How to create, build, and run a project with Cargo.
- How to run programs in a terminal, including passing flags and reading exit codes.
Arguments are a list of strings
The shell splits a command line on spaces, respecting quotes, and hands the program a list of strings. Rust exposes that list through std::env::args.
fn main() {
for (i, arg) in std::env::args().enumerate() {
println!("{i}: {arg}");
}
}Saved as a binary named tasks, running tasks add "Write report" --priority high prints:
0: tasks
1: add
2: Write report
3: --priority
4: highIndex 0 is the program name. The list has no types and no meaning attached. Deciding that --priority takes a value, and that high is that value, is the work of an argument parser.
The four building blocks
Most command lines combine four kinds of element.
| Element | Example | What it is |
|---|---|---|
| Positional argument | `"Write report"` | A value identified by its position, not by a name |
| Option | `--priority high`, `-p high` | A named argument that takes a value |
| Flag | `--verbose`, `-v` | A named argument with no value. Its presence switches something on |
| Subcommand | `add` | A word that selects a mode of the program, with its own arguments |
A few conventions apply to these elements:
- Options and flags have a long form with two dashes (
--verbose) and often a one-letter short form with one dash (-v). - Short flags can be combined, so
-vvmeans-v -v. - A value can follow a space or an equals sign:
--priority highand--priority=highare equivalent. - A bare
--conventionally marks the end of options. Everything after it is treated as positional, even if it starts with a dash.
What the parser must decide
Given the raw strings, a parser answers several questions:
- Which strings are names (
--priority) and which are values? - Does this option take a value, or is it a flag?
- Is every required argument present, and is each value valid, for example a number in range?
- Which subcommand was chosen, and which arguments belong to it?
- What should happen when the user asks for
--helpor--version?
Hand-written parsers tend to get these details wrong, especially the edge cases: combined short flags, -- handling, and clear error messages. clap implements them once, so your program receives either valid typed data or a clean error.
Output, errors, and exit codes
A command-line program also has an interface on the way out:
- stdout carries normal results, which other programs can pipe onward.
- stderr carries diagnostics and errors, so they do not pollute piped output.
- The exit code tells the caller whether the program succeeded.
0means success. Any non-zero value means failure.
Scripts and CI systems depend on these conventions, so a well-behaved tool follows them. Later chapters show how clap handles this for parse errors and how you handle it for application errors.
Exercise
In the command tasks list --all -v, which parts are the subcommand, a flag with a long name, and a flag with a short name?
Show answer
list is the subcommand, --all is the flag with a long name, and -v is the flag with a short name. None of them takes a value.
Exercise
A user runs tasks add -- --urgent. How should a conventional parser treat --urgent, and why?
Show answer
As a positional argument. The bare -- ends option parsing, so everything after it is a value even if it starts with a dash.