diff options
| author | 1992414357@qq.com <1992414357@qq.com> | 2025-06-09 01:44:36 +0800 |
|---|---|---|
| committer | 1992414357@qq.com <1992414357@qq.com> | 2025-06-09 01:44:36 +0800 |
| commit | 49192dbb98e0ab1f2a66b4786fac86d63f69be64 (patch) | |
| tree | 7cdf27c7cab5fb6b1aabc2ac7c44b7b492558945 /core/src/service/cli_addition/utils.rs | |
| parent | c9dbba0d288becb7f05cebe526be25c76e5a850a (diff) | |
重构所有部分
Diffstat (limited to 'core/src/service/cli_addition/utils.rs')
| -rw-r--r-- | core/src/service/cli_addition/utils.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/core/src/service/cli_addition/utils.rs b/core/src/service/cli_addition/utils.rs new file mode 100644 index 0000000..05969a5 --- /dev/null +++ b/core/src/service/cli_addition/utils.rs @@ -0,0 +1,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 + } + } +}
\ No newline at end of file |
