diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index 957cf6625c..5ccd48deae 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -614,9 +614,8 @@ impl FindArgs<'_> { if self.first.is_some() && self.last.is_some() { anyhow::bail!("--first and --last cannot be used together"); } - // #3059: an explicit zero limit is an error, never a silent empty result. (A - // nonnumeric value keeps the #2009 behaviour: it falls back to the default of 1 - // in the parsers, so only a parsed 0 reaches this check.) + // #3059: an explicit zero limit is an error, never a silent empty result. + // Nonnumeric option values are rejected by the argument parsers. if self.first == Some(0) { anyhow::bail!("--first must be at least 1 (got 0)"); } @@ -707,26 +706,36 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> Result() { - args.first = Some(n); - i += 1; - } else { - args.first = Some(1); // default to 1 - } + // --first with optional number argument; a `>file` + // redirection token is not the number (it is handled by + // the output branch below), so `--first >out.txt` keeps + // the default limit of 1. + if i + 1 < rest.len() + && !rest[i + 1].starts_with('-') + && !rest[i + 1].starts_with('>') + { + let value = rest[i + 1]; + args.first = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--first requires an integer (got {value:?})") + })?); + i += 1; } else { args.first = Some(1); // default to 1 } } else if arg == "--last" { - // --last with optional number argument - if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { - if let Ok(n) = rest[i + 1].parse::() { - args.last = Some(n); - i += 1; - } else { - args.last = Some(1); // default to 1 - } + // --last with optional number argument; same + // redirection exclusion as --first. + if i + 1 < rest.len() + && !rest[i + 1].starts_with('-') + && !rest[i + 1].starts_with('>') + { + let value = rest[i + 1]; + args.last = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--last requires an integer (got {value:?})") + })?); + i += 1; } else { args.last = Some(1); // default to 1 } @@ -1263,23 +1272,23 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> } } else if arg == "--first" { if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { - if let Ok(n) = rest[i + 1].parse::() { - find_args.first = Some(n); - i += 1; - } else { - find_args.first = Some(1); - } + let value = rest[i + 1]; + find_args.first = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--first requires an integer (got {value:?})") + })?); + i += 1; } else { find_args.first = Some(1); } } else if arg == "--last" { if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { - if let Ok(n) = rest[i + 1].parse::() { - find_args.last = Some(n); - i += 1; - } else { - find_args.last = Some(1); - } + let value = rest[i + 1]; + find_args.last = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--last requires an integer (got {value:?})") + })?); + i += 1; } else { find_args.last = Some(1); } @@ -1289,9 +1298,11 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> i += 1; } } else if arg == "--lines" || arg == "-n" { - if i + 1 < rest.len() - && let Ok(n) = rest[i + 1].parse::() - { + if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { + let value = rest[i + 1]; + let n = value + .parse::() + .map_err(|_| anyhow::anyhow!("--lines requires an integer (got {value:?})"))?; peek_opts.lines = n; i += 1; } @@ -1611,16 +1622,36 @@ mod tests { parse_find_args(&["q", "--last"], "tag").unwrap().last, Some(1) ); - assert_eq!( - parse_find_args(&["q", "--first", "abc"], "tag") - .unwrap() - .first, - Some(1) - ); + assert_eq!(parse_peek_args(&["q", "--first"]).unwrap().0.first, Some(1)); + assert_eq!(parse_peek_args(&["q", "--last"]).unwrap().0.last, Some(1)); // Peek without --lines keeps the default of 5. assert_eq!(parse_peek_args(&["q"]).unwrap().1.lines, 5); } + #[test] + fn test_parse_rejects_nonnumeric_result_limit_values() { + for (flag, value) in [("--first", "two"), ("--last", "abc")] { + let find_error = parse_find_args(&["q", flag, value], "tag") + .err() + .expect("find parser should reject a nonnumeric limit"); + assert!(find_error.to_string().contains(flag)); + assert!(find_error.to_string().contains(value)); + + let peek_error = parse_peek_args(&["q", flag, value]) + .err() + .expect("peek parser should reject a nonnumeric limit"); + assert!(peek_error.to_string().contains(flag)); + assert!(peek_error.to_string().contains(value)); + } + + for flag in ["--lines", "-n"] { + let error = parse_peek_args(&["q", flag, "abc"]) + .err() + .expect("peek parser should reject a nonnumeric line count"); + assert!(error.to_string().contains("abc")); + } + } + fn test_parse_find_args_basic() { let rest = vec!["query1", "query2"]; let args = parse_find_args(&rest, "union").unwrap(); @@ -1731,6 +1762,24 @@ mod tests { assert!(parse_find_args(&["query", "--last", "3"], "union").is_ok()); } + #[test] + fn test_parse_find_args_first_before_redirection_keeps_default() { + // `--first >out.txt`: the redirection token is the output + // option, not the optional number (Codex P2 on PR #4072). + let args = parse_find_args(&["query", "--first", ">out.txt"], "union").unwrap(); + assert_eq!(args.first, Some(1)); + assert_eq!(args.output_file, Some("out.txt")); + assert!(args.to_file); + } + + #[test] + fn test_parse_find_args_last_before_redirection_keeps_default() { + let args = parse_find_args(&["query", "--last", ">out.txt"], "union").unwrap(); + assert_eq!(args.last, Some(1)); + assert_eq!(args.output_file, Some("out.txt")); + assert!(args.to_file); + } + #[test] fn test_parse_find_args_count() { let rest = vec!["query", "--count"];