diff --git a/pkgs/id/src/cli.rs b/pkgs/id/src/cli.rs index f8e103b9eb..db00e25d33 100644 --- a/pkgs/id/src/cli.rs +++ b/pkgs/id/src/cli.rs @@ -887,7 +887,7 @@ pub enum Command { /// - `tag`: each match with its query /// - `group`: matches grouped by query /// - `union`: deduplicated by hash - #[arg(long, default_value = "tag")] + #[arg(long, default_value = "tag", value_parser = ["tag", "group", "union"])] format: String, /// Return only the first N matches (default 1 if no number given). #[arg(long, num_args = 0..=1, default_missing_value = "1")] @@ -954,7 +954,7 @@ pub enum Command { #[arg(long)] dir: Option, /// Output format: tag, group, or union. - #[arg(long, default_value = "tag")] + #[arg(long, default_value = "tag", value_parser = ["tag", "group", "union"])] format: String, /// Return only the first N matches (default 1 if no number given). #[arg(long, num_args = 0..=1, default_missing_value = "1")] @@ -2653,6 +2653,38 @@ mod tests { } } + #[test] + fn test_cli_parse_find_with_invalid_format() { + for bad in ["gorup", "yaml", "GROUP", ""] { + assert!( + Cli::try_parse_from(["id", "find", "--format", bad, "query"]).is_err(), + "find --format {bad:?} must be rejected at parse time" + ); + } + for good in ["tag", "group", "union"] { + assert!( + Cli::try_parse_from(["id", "find", "--format", good, "query"]).is_ok(), + "find --format {good:?} must parse" + ); + } + } + + #[test] + fn test_cli_parse_search_with_invalid_format() { + for bad in ["gorup", "yaml", "GROUP", ""] { + assert!( + Cli::try_parse_from(["id", "search", "--format", bad, "query"]).is_err(), + "search --format {bad:?} must be rejected at parse time" + ); + } + for good in ["tag", "group", "union"] { + assert!( + Cli::try_parse_from(["id", "search", "--format", good, "query"]).is_ok(), + "search --format {good:?} must parse" + ); + } + } + #[test] fn test_cli_parse_search() { let cli = Cli::parse_from(["id", "search", "query"]); diff --git a/pkgs/id/src/commands/find.rs b/pkgs/id/src/commands/find.rs index 0dbb3bec45..5d47f95b6e 100644 --- a/pkgs/id/src/commands/find.rs +++ b/pkgs/id/src/commands/find.rs @@ -23,9 +23,9 @@ //! //! Both commands support multiple output formats via `--format`: //! -//! - **tag**: Shows query tag with each match +//! - **tag**: Default format, shows query tag with each match //! - **group**: Groups matches by query -//! - **union**: Default format, shows all matches with query suffix +//! - **union**: Shows all matches with query suffix //! //! # Filtering and Limiting //! diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index c63fd80500..c546e06ce6 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -587,6 +587,10 @@ struct FindArgs<'a> { to_file: bool, /// Output format: "tag", "group", or "union" format: &'a str, + /// Set when `--format` named an unknown value: the handlers surface + /// this error instead of letting the value reach the printers, whose + /// fallthrough arm would silently render it as the tag format. + format_error: Option<&'a str>, /// Return only first N matches first: Option, /// Return only last N matches @@ -633,6 +637,7 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> FindArgs<'a dir: None, to_file: false, format: default_format, + format_error: None, first: None, last: None, count: false, @@ -663,7 +668,12 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> FindArgs<'a } } else if arg == "--format" { if i + 1 < rest.len() { - args.format = rest[i + 1]; + let value = rest[i + 1]; + if matches!(value, "tag" | "group" | "union") { + args.format = value; + } else { + args.format_error = Some(value); + } i += 1; } } else if arg == "--tag" { @@ -732,6 +742,11 @@ async fn handle_find_command( ) -> Result<()> { let args = parse_find_args(rest, "union"); + if let Some(bad) = args.format_error { + println!("error: invalid --format \"{bad}\": expected tag, group, or union"); + return Ok(()); + } + if args.queries.is_empty() { println!( "usage: find ... [--name] [--all] [--first [N]] [--last [N]] [--count] [--exclude PAT] [--dir ] [--file] [>filename]" @@ -793,6 +808,11 @@ async fn handle_find_command( async fn handle_search_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<()> { let args = parse_find_args(rest, "union"); + if let Some(bad) = args.format_error { + println!("error: invalid --format \"{bad}\": expected tag, group, or union"); + return Ok(()); + } + if args.queries.is_empty() { println!( "usage: search ... [--name] [--all] [--first [N]] [--last [N]] [--count] [--exclude PAT] [--dir ] [--file] [>filename]" @@ -845,6 +865,11 @@ async fn handle_search_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<( async fn handle_show_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<()> { let args = parse_find_args(rest, "union"); + if let Some(bad) = args.format_error { + println!("error: invalid --format \"{bad}\": expected tag, group, or union"); + return Ok(()); + } + if args.queries.is_empty() { println!( "usage: show ... [--all] [--first [N]] [--last [N]] [--exclude PAT] [-o FILE]" @@ -1191,6 +1216,7 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> (FindArgs<'a>, PeekOptions) { dir: None, to_file: false, format: "union", + format_error: None, first: None, last: None, count: false, @@ -1564,6 +1590,28 @@ mod tests { assert_eq!(args.format, "union"); } + #[test] + fn test_parse_find_args_invalid_format_is_error() { + let rest = ["--format", "gorup", "hello"]; + let args = parse_find_args(&rest, "union"); + assert_eq!(args.format_error, Some("gorup")); + assert_eq!(args.format, "union"); + + let rest = ["--format", "yaml", "hello"]; + let args = parse_find_args(&rest, "tag"); + assert_eq!(args.format_error, Some("yaml")); + } + + #[test] + fn test_parse_find_args_valid_format_is_stored() { + for good in ["tag", "group", "union"] { + let rest = ["--format", good, "hello"]; + let args = parse_find_args(&rest, "union"); + assert_eq!(args.format_error, None); + assert_eq!(args.format, good); + } + } + #[test] fn test_parse_find_args_first_with_number() { let rest = vec!["query", "--first", "5"];