diff --git a/codex-rs/shell-command/src/parse_command.rs b/codex-rs/shell-command/src/parse_command.rs index 01768d0a7a..aeb5a1dcea 100644 --- a/codex-rs/shell-command/src/parse_command.rs +++ b/codex-rs/shell-command/src/parse_command.rs @@ -1230,15 +1230,33 @@ mod tests { }], ); - // fd with query and path should capture both - assert_parsed( - &shlex_split_safe("fd main src"), - vec![ParsedCommand::Search { - cmd: "fd main src".to_string(), - query: Some("main".to_string()), - path: Some("src".to_string()), - }], - ); + for (cmd, query, path) in [ + ("fd main src", "main", "src"), + ("fd -Hd 1 . repro/scripts", ".", "scripts"), + ("fd -d 1 -- -foo repro/scripts", "-foo", "scripts"), + ("fd -Hemd 1 repro/scripts", "1", "scripts"), + ( + "fd --max-depth 1 --type f -e py . repro/scripts", + ".", + "scripts", + ), + ("fd --min-depth 1 -d 2 . repro/scripts", ".", "scripts"), + ( + "fd --maxdepth 1 --mindepth 1 . repro/scripts", + ".", + "scripts", + ), + ("fd 1 --exact-depth 2 repro/scripts", "1", "scripts"), + ] { + assert_parsed( + &shlex_split_safe(cmd), + vec![ParsedCommand::Search { + cmd: cmd.to_string(), + query: Some(query.to_string()), + path: Some(path.to_string()), + }], + ); + } } #[test] @@ -1735,14 +1753,14 @@ fn skip_flag_values<'a>(args: &'a [String], flags_with_vals: &[&str]) -> Vec<&'a } fn first_non_flag_operand(args: &[String], flags_with_vals: &[&str]) -> Option { - positional_operands(args, flags_with_vals) + positional_operands(args, |arg| flags_with_vals.contains(&arg)) .into_iter() .next() .cloned() } fn single_non_flag_operand(args: &[String], flags_with_vals: &[&str]) -> Option { - let mut operands = positional_operands(args, flags_with_vals).into_iter(); + let mut operands = positional_operands(args, |arg| flags_with_vals.contains(&arg)).into_iter(); let first = operands.next()?; if operands.next().is_some() { return None; @@ -1750,7 +1768,7 @@ fn single_non_flag_operand(args: &[String], flags_with_vals: &[&str]) -> Option< Some(first.clone()) } -fn positional_operands<'a>(args: &'a [String], flags_with_vals: &[&str]) -> Vec<&'a String> { +fn positional_operands(args: &[String], takes_value: impl Fn(&str) -> bool) -> Vec<&String> { let mut out = Vec::new(); let mut after_double_dash = false; let mut skip_next = false; @@ -1770,7 +1788,7 @@ fn positional_operands<'a>(args: &'a [String], flags_with_vals: &[&str]) -> Vec< if arg.starts_with("--") && arg.contains('=') { continue; } - if flags_with_vals.contains(&arg.as_str()) { + if takes_value(arg) { if i + 1 < args.len() { skip_next = true; } @@ -1929,26 +1947,29 @@ pub fn is_pathish(s: &str) -> bool { } fn parse_fd_query_and_path(tail: &[String]) -> (Option, Option) { - let args_no_connector = trim_at_connector(tail); - // fd has several flags that take values (e.g., -t/--type, -e/--extension). - // Skip those values when extracting positional operands. - let candidates = skip_flag_values( - &args_no_connector, - &[ - "-t", - "--type", - "-e", - "--extension", - "-E", - "--exclude", - "--search-path", - ], - ); - let non_flags: Vec<&String> = candidates - .into_iter() - .filter(|p| !p.starts_with('-')) - .collect(); - match non_flags.as_slice() { + let args = trim_at_connector(tail); + let operands = positional_operands(&args, |arg| { + if let Some(long) = arg.strip_prefix("--") { + return matches!( + long, + "type" + | "extension" + | "exclude" + | "max-depth" + | "maxdepth" + | "min-depth" + | "mindepth" + | "exact-depth" + | "search-path" + ); + } + // Skip grouped switches. In -Hd the depth follows, but in -Hemd the extension is md. + arg.strip_prefix('-').is_some_and(|short| { + let option = short.trim_start_matches(|c: char| "HIusigFalLp01q".contains(c)); + matches!(option, "d" | "t" | "e" | "E") + }) + }); + match operands.as_slice() { [one] => { if is_pathish(one) { (None, Some(short_display_path(one))) diff --git a/codex-rs/tui/src/chatwidget/tests/exec_flow.rs b/codex-rs/tui/src/chatwidget/tests/exec_flow.rs index 8e6d4613aa..f54130cc67 100644 --- a/codex-rs/tui/src/chatwidget/tests/exec_flow.rs +++ b/codex-rs/tui/src/chatwidget/tests/exec_flow.rs @@ -776,16 +776,27 @@ async fn exec_history_shows_unified_exec_tool_calls() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; chat.on_task_started(); - let begin = begin_exec_with_source( - &mut chat, - "call-startup", - "ls", - ExecCommandSource::UnifiedExecStartup, - ); - end_exec(&mut chat, begin, "", "", /*exit_code*/ 0); + for (id, command) in [ + ("call-startup", "ls"), + ( + "call-search", + "fd --max-depth 1 --type f -e py . repro/scripts", + ), + ] { + let begin = begin_exec_with_source( + &mut chat, + id, + command, + ExecCommandSource::UnifiedExecStartup, + ); + end_exec(&mut chat, begin, "", "", /*exit_code*/ 0); + } - let blob = active_blob(&chat); - assert_eq!(blob, "• Explored\n └ List ls\n"); + insta::assert_snapshot!(active_blob(&chat), @r" +• Explored + └ List ls + Search . in scripts +"); } #[tokio::test]