date: allow extra operand

This commit is contained in:
Sylvestre Ledru
2025-12-22 14:14:09 +01:00
parent 58266a890a
commit 055ba74126
2 changed files with 20 additions and 1 deletions
+12 -1
View File
@@ -171,6 +171,17 @@ fn parse_military_timezone_with_offset(s: &str) -> Option<i32> {
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::<String>(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::<String>(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.
+8
View File
@@ -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"] {