expression

Clap non-exiting parse errors

Use try_parse_from to inspect parser errors without terminating the process. Distinguish invalid input from help and version requests. Assert a stable error category rather than an entire formatted error message. Uses a fixed synthetic argument list or local data. Expected stdout (not executed): invalid value and help distinguished Common mistake: Treating --help as an application failure or comparing fragile full error strings.

Formal Syntax & Structure

rust syntaxrust
Cli::try_parse_from(...).unwrap_err().kind()

Working Example

Example Snippetrust
fn main() {    use clap::{Parser, error::ErrorKind};    #[derive(Debug, Parser)]    struct Cli { #[arg(long)] limit: u16 }    let err = Cli::try_parse_from(["report", "--limit", "bad"]).unwrap_err();    assert_eq!(err.kind(), ErrorKind::ValueValidation);    let help = Cli::try_parse_from(["report", "--help"]).unwrap_err();    assert_eq!(help.kind(), ErrorKind::DisplayHelp);    println!("invalid value and help distinguished");}
Rust Reference