Skip to content

Commit 055ba74

Browse files
committed
date: allow extra operand
1 parent 58266a8 commit 055ba74

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/uu/date/src/date.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ fn parse_military_timezone_with_offset(s: &str) -> Option<i32> {
171171
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
172172
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
173173

174+
// Check for extra operands (multiple positional arguments)
175+
if let Some(formats) = matches.get_many::<String>(OPT_FORMAT) {
176+
let format_args: Vec<&String> = formats.collect();
177+
if format_args.len() > 1 {
178+
return Err(USimpleError::new(
179+
1,
180+
translate!("date-error-extra-operand", "operand" => format_args[1]),
181+
));
182+
}
183+
}
184+
174185
let format = if let Some(form) = matches.get_one::<String>(OPT_FORMAT) {
175186
if !form.starts_with('+') {
176187
return Err(USimpleError::new(
@@ -515,7 +526,7 @@ pub fn uu_app() -> Command {
515526
.help(translate!("date-help-universal"))
516527
.action(ArgAction::SetTrue),
517528
)
518-
.arg(Arg::new(OPT_FORMAT))
529+
.arg(Arg::new(OPT_FORMAT).num_args(0..))
519530
}
520531

521532
/// Return the appropriate format string for the given settings.

tests/by-util/test_date.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ fn test_empty_arguments() {
2424
new_ucmd!().args(&["", "", ""]).fails_with_code(1);
2525
}
2626

27+
#[test]
28+
fn test_extra_operands() {
29+
new_ucmd!()
30+
.args(&["test", "extra"])
31+
.fails_with_code(1)
32+
.stderr_contains("extra operand 'extra'");
33+
}
34+
2735
#[test]
2836
fn test_date_email() {
2937
for param in ["--rfc-email", "--rfc-e", "-R", "--rfc-2822", "--rfc-822"] {

0 commit comments

Comments
 (0)