From 055ba741266eabc8ead8b714aab1bd594faa4898 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 2 Nov 2025 22:38:05 +0100 Subject: [PATCH] date: allow extra operand --- src/uu/date/src/date.rs | 13 ++++++++++++- tests/by-util/test_date.rs | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/uu/date/src/date.rs b/src/uu/date/src/date.rs index 93c085466..4a5c583cf 100644 --- a/src/uu/date/src/date.rs +++ b/src/uu/date/src/date.rs @@ -171,6 +171,17 @@ fn parse_military_timezone_with_offset(s: &str) -> Option { pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?; + // Check for extra operands (multiple positional arguments) + if let Some(formats) = matches.get_many::(OPT_FORMAT) { + let format_args: Vec<&String> = formats.collect(); + if format_args.len() > 1 { + return Err(USimpleError::new( + 1, + translate!("date-error-extra-operand", "operand" => format_args[1]), + )); + } + } + let format = if let Some(form) = matches.get_one::(OPT_FORMAT) { if !form.starts_with('+') { return Err(USimpleError::new( @@ -515,7 +526,7 @@ pub fn uu_app() -> Command { .help(translate!("date-help-universal")) .action(ArgAction::SetTrue), ) - .arg(Arg::new(OPT_FORMAT)) + .arg(Arg::new(OPT_FORMAT).num_args(0..)) } /// Return the appropriate format string for the given settings. diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index a8c353b3f..33fb2e0e5 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -24,6 +24,14 @@ fn test_empty_arguments() { new_ucmd!().args(&["", "", ""]).fails_with_code(1); } +#[test] +fn test_extra_operands() { + new_ucmd!() + .args(&["test", "extra"]) + .fails_with_code(1) + .stderr_contains("extra operand 'extra'"); +} + #[test] fn test_date_email() { for param in ["--rfc-email", "--rfc-e", "-R", "--rfc-2822", "--rfc-822"] {