-
Notifications
You must be signed in to change notification settings - Fork 6
/
input.rs
40 lines (38 loc) · 1.07 KB
/
input.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use demand::Input;
fn main() {
let notempty_minlen = |s: &str| {
if s.is_empty() {
return Err("Name cannot be empty");
}
if s.len() < 8 {
return Err("Name must be at least 8 characters");
}
Ok(())
};
let input = Input::new("What's your name?")
.description("We'll use this to personalize your experience.")
.placeholder("Enter your name")
.prompt("Name: ")
.suggestions(&[
"Adam Grant",
"Danielle Steel",
"Eveline Widmer-Schlumpf",
"Robert De Niro",
"Ronaldo Rodrigues de Jesus",
"Sarah Michelle Gellar",
"Yael Naim",
"Zack Snyder",
])
.validation(notempty_minlen);
let _ = match input.run() {
Ok(value) => value,
Err(e) => {
if e.kind() == std::io::ErrorKind::Interrupted {
println!("Input cancelled");
return;
} else {
panic!("Error: {}", e);
}
}
};
}