Skip to content

validator equivalent in Clap 4 #5402

Closed Answered by epage
ditek asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this is the new way. The idea is to put the emphasis on parsing into a validated type ("parse, don't validate") but it does not preclude validating to a non-validated type.

Say you want to validate a string, you can use clap::builder::StringValueParser::new().try_map(|s| ...). A short-cut for this is to pass in a Fn(&str) -> Result<T, E> (and you can just return a String as T) as a value parser.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ditek
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants