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
83 changes: 75 additions & 8 deletions pkgs/id/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,20 @@ pub enum Command {
#[arg(short = 'o', long)]
output: Option<String>,
/// Return only the first N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "last",
num_args = 0..=1,
default_missing_value = "1"
)]
first: Option<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
)]
last: Option<usize>,
/// Exclude matches where name or hash contains this pattern (repeatable).
#[arg(long, action = clap::ArgAction::Append)]
Expand Down Expand Up @@ -804,10 +814,20 @@ pub enum Command {
#[arg(long)]
all: bool,
/// Return only the first N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "last",
num_args = 0..=1,
default_missing_value = "1"
)]
first: Option<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
)]
last: Option<usize>,
/// Exclude matches where name or hash contains this pattern (repeatable).
#[arg(long, action = clap::ArgAction::Append)]
Expand Down Expand Up @@ -890,10 +910,20 @@ pub enum Command {
#[arg(long, default_value = "tag")]
format: String,
/// Return only the first N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "last",
num_args = 0..=1,
default_missing_value = "1"
)]
first: Option<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
)]
last: Option<usize>,
/// Print count of matches instead of the matches themselves.
#[arg(long)]
Expand Down Expand Up @@ -957,10 +987,20 @@ pub enum Command {
#[arg(long, default_value = "tag")]
format: String,
/// Return only the first N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "last",
num_args = 0..=1,
default_missing_value = "1"
)]
first: Option<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(long, num_args = 0..=1, default_missing_value = "1")]
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
)]
last: Option<usize>,
/// Print count of matches instead of the matches themselves.
#[arg(long)]
Expand Down Expand Up @@ -2850,6 +2890,33 @@ mod tests {
Cli::command().debug_assert();
}

#[test]
fn test_cli_rejects_first_and_last_together() {
std::thread::Builder::new()
.stack_size(8 * 1024 * 1024)
.spawn(|| {
assert!(
Cli::try_parse_from(["id", "show", "--first", "2", "--last", "3", "query"])
.is_err()
);
assert!(
Cli::try_parse_from(["id", "peek", "--first", "2", "--last", "3", "query"])
.is_err()
);
assert!(
Cli::try_parse_from(["id", "find", "--first", "2", "--last", "3", "query"])
.is_err()
);
assert!(
Cli::try_parse_from(["id", "search", "--first", "2", "--last", "3", "query"])
.is_err()
);
})
.unwrap()
.join()
.unwrap();
}

// Tests for Show command
#[test]
fn test_cli_parse_show() {
Expand Down
Loading