From 6bd344793e5c2e84361475d6e221007ef21faaad Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 25 Feb 2026 10:58:39 +0800 Subject: Remove ambiguous command error and auto-select longest match --- src/bin/jvn.rs | 12 ------------ src/systems/cmd/errors.rs | 4 +--- src/systems/cmd/macros.rs | 5 ++++- src/systems/cmd/processer.rs | 11 +++++------ 4 files changed, 10 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/bin/jvn.rs b/src/bin/jvn.rs index 1dbc517..7ec1f4d 100644 --- a/src/bin/jvn.rs +++ b/src/bin/jvn.rs @@ -101,18 +101,6 @@ async fn main() { CmdProcessError::NoMatchingCommand => { handle_no_matching_command_error(args); } - CmdProcessError::AmbiguousCommand(nodes) => { - let nodes_list = nodes - .iter() - .enumerate() - .map(|(i, node)| format!("{}. {}", i + 1, node)) - .collect::>() - .join("\n"); - eprintln!( - "{}", - md(t!("process_error.ambiguous_command", nodes = nodes_list)) - ); - } CmdProcessError::ParseError(help) => { if help.trim().len() < 1 { eprintln!("{}", md(t!("process_error.parse_error"))); diff --git a/src/systems/cmd/errors.rs b/src/systems/cmd/errors.rs index 0b01abf..abde5de 100644 --- a/src/systems/cmd/errors.rs +++ b/src/systems/cmd/errors.rs @@ -8,6 +8,7 @@ pub enum CmdPrepareError { #[error("{0}")] Error(String), + // Workspace Reader Errors #[error("LocalWorkspace not found")] LocalWorkspaceNotFound, @@ -110,9 +111,6 @@ pub enum CmdProcessError { #[error("No matching command found")] NoMatchingCommand, - #[error("Ambiguous command, multiple matches found")] - AmbiguousCommand(Vec), - #[error("Parse error")] ParseError(String), diff --git a/src/systems/cmd/macros.rs b/src/systems/cmd/macros.rs index c7d576d..23a277c 100644 --- a/src/systems/cmd/macros.rs +++ b/src/systems/cmd/macros.rs @@ -161,6 +161,9 @@ macro_rules! command_template { #[macro_export] macro_rules! cmd_output { ($v:expr, $t:ty) => { - Ok((Box::new($v), stringify!($t).to_string())) + Ok(( + Box::new($v) as Box, + stringify!($t).to_string(), + )) }; } diff --git a/src/systems/cmd/processer.rs b/src/systems/cmd/processer.rs index 4bcaaeb..196764a 100644 --- a/src/systems/cmd/processer.rs +++ b/src/systems/cmd/processer.rs @@ -37,12 +37,11 @@ pub async fn jv_cmd_process( } _ => { // Multiple matching nodes found - return Err(CmdProcessError::AmbiguousCommand( - matching_nodes - .iter() - .map(|s| s.to_string()) - .collect::>(), - )); + // Find the node with the longest length (most specific match) + let matched_prefix = matching_nodes.iter().max_by_key(|node| node.len()).unwrap(); + let prefix_len = matched_prefix.split_whitespace().count(); + let trimmed_args: Vec = args.into_iter().cloned().skip(prefix_len).collect(); + return jv_cmd_process_node(matched_prefix, trimmed_args, ctx, renderer_override).await; } } } -- cgit