clap Basics
Argument parsing.
Why clap?
clap (Command Line Argument Parser) is the de-facto standard crate for building CLIs in Rust.
- Parses arguments, flags, and options
- Generates
--helpand--versionautomatically - Validates input and reports friendly errors
Without it you would manually iterate over std::env::args() and handle every edge case yourself.
Reading args by hand
Before reaching for clap, it helps to see the raw approach. std::env::args() yields an iterator where the first item is the program name.
fn main() {
let args: Vec<String> = std::env::args().collect();
println!("Program: {}", args[0]);
println!("Got {} extra arg(s)", args.len() - 1);
}All lessons in this course
- clap Basics
- Subcommands
- Derive API
- Validation and Help