Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions pkgs/id/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -954,7 +954,7 @@ pub enum Command {
#[arg(long)]
dir: Option<String>,
/// 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")]
Expand Down Expand Up @@ -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"]);
Expand Down
4 changes: 2 additions & 2 deletions pkgs/id/src/commands/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down
50 changes: 49 additions & 1 deletion pkgs/id/src/repl/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
/// Return only last N matches
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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 <query>... [--name] [--all] [--first [N]] [--last [N]] [--count] [--exclude PAT] [--dir <dir>] [--file] [>filename]"
Expand Down Expand Up @@ -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 <query>... [--name] [--all] [--first [N]] [--last [N]] [--count] [--exclude PAT] [--dir <dir>] [--file] [>filename]"
Expand Down Expand Up @@ -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 <query>... [--all] [--first [N]] [--last [N]] [--exclude PAT] [-o FILE]"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"];
Expand Down