Validation and Help
Polished CLIs.
Polishing your CLI
A great CLI validates input early and offers clear help. clap supports both with attributes and value parsers.
- Reject bad values with friendly messages
- Document every argument
Value ranges
Restrict a numeric argument to a range with value_parser. Out-of-range input is rejected automatically.
use clap::Parser;
#[derive(Parser)]
struct Cli {
#[arg(long, value_parser = clap::value_parser!(u16).range(1..=65535))]
port: u16,
}All lessons in this course
- clap Basics
- Subcommands
- Derive API
- Validation and Help