0Pricing
Learn Rust Coding · Lesson

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

  1. clap Basics
  2. Subcommands
  3. Derive API
  4. Validation and Help
← Back to Learn Rust Coding