mirror of
https://github.com/clap-rs/clap
synced 2025-01-05 17:28:42 +00:00
Merge pull request #194 from sru/master
Fixing indentation of examples and new custom validator example
This commit is contained in:
commit
d67d28d571
7 changed files with 225 additions and 189 deletions
36
examples/15_CustomValidator.rs
Normal file
36
examples/15_CustomValidator.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
extern crate clap;
|
||||
|
||||
use clap::{App, Arg};
|
||||
|
||||
fn main() {
|
||||
// You can define a function (or a closure) to use as a validator to argument values. The
|
||||
// function must accept a String and return Result<(), String> where Err(String) is the message
|
||||
// displayed to the user.
|
||||
|
||||
let matches = App::new("myapp")
|
||||
// Application logic goes here...
|
||||
.arg(Arg::with_name("input")
|
||||
.help("the input file to use")
|
||||
.index(1)
|
||||
.required(true)
|
||||
|
||||
.validator(|val| {
|
||||
// val is the argument value passed in by the user
|
||||
// val has type of String.
|
||||
|
||||
if val.ends_with(".png") {
|
||||
Ok(())
|
||||
} else {
|
||||
// clap automatically adds "error: " to the beginning
|
||||
// of the message.
|
||||
Err(String::from("the file format must be png."))
|
||||
}
|
||||
|
||||
// Of course, you can do more complicated validation as
|
||||
// well, but for the simplicity, this example only checks
|
||||
// if the value passed in ends with ".png" or not.
|
||||
}))
|
||||
.get_matches();
|
||||
|
||||
// Continued program logic here...
|
||||
}
|
Loading…
Reference in a new issue