blob: 05969a5c6e8299629022da3362bf313c7f4c11da (
plain) (
blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
use clap::{Command, FromArgMatches};
use clap::ColorChoice::Auto;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
pub async fn read_cli<Cmd>(prefix: String, entry: &String, cmd: Command) -> Option<Cmd>
where Cmd: FromArgMatches {
let input: String = {
let mut buffer = String::new();
let mut stdin = tokio::io::BufReader::new(tokio::io::stdin());
let mut stdout = tokio::io::stdout();
stdout.write_all(prefix.as_bytes()).await.ok().unwrap();
stdout.flush().await.ok().unwrap();
stdin.read_line(&mut buffer).await.ok().unwrap();
buffer.trim().to_string()
};
process_debug_cli(entry, input, cmd).await
}
async fn process_debug_cli<Cmd>(entry: &String, input: String, cmd: Command) -> Option<Cmd>
where Cmd: FromArgMatches {
if input.trim().is_empty() {
return None;
}
let cmd = cmd
.color(Auto)
.help_template(
"{subcommands}{options}"
)
.disable_help_flag(true)
.disable_version_flag(true);
let args = shell_words::split(input.as_str()).unwrap_or_else(|_e| {
["".to_string()].to_vec()
});
let full_args = std::iter::once(entry.into()).chain(args);
match cmd.try_get_matches_from(full_args) {
Ok(matches) => {
match Cmd::from_arg_matches(&matches) {
Ok(cmd) => {
Some(cmd)
}
Err(_err) => {
None
}
}
}
Err(err) => {
println!("{}", err);
None
}
}
}
|