From 49192dbb98e0ab1f2a66b4786fac86d63f69be64 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Mon, 9 Jun 2025 01:44:36 +0800 Subject: 重构所有部分 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/service/cli_addition/utils.rs | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 core/src/service/cli_addition/utils.rs (limited to 'core/src/service/cli_addition/utils.rs') 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(prefix: String, entry: &String, cmd: Command) -> Option +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(entry: &String, input: String, cmd: Command) -> Option +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 -- cgit