]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
date: --debug: diagnose discarded -d or -s options
authorPádraig Brady <P@draigBrady.com>
Sun, 24 Jul 2022 18:24:18 +0000 (19:24 +0100)
committerPádraig Brady <P@draigBrady.com>
Sun, 24 Jul 2022 19:40:34 +0000 (20:40 +0100)
* src/date.c: (main): Track and diagnose whether any
-d or -s options are dropped, as users may think
multiple options are supported, given they can be relative.
* tests/misc/date-debug.sh: Add a test case.
* NEWS: Mention the improvement.

NEWS
src/date.c
tests/misc/date-debug.sh

diff --git a/NEWS b/NEWS
index b5b8990f869e97d3602b0f8ce9a0350080d7fbff..3113d42363cc0815f017a18d5804e395b004cb2e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -36,6 +36,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   factor now accepts the --exponents (-h) option to print factors
   in the form p^e, rather than repeating the prime p, e times.
 
+** Improvements
+
+  date --debug now diagnoses if multiple --date or --set options are
+  specified, as only the last specified is significant in that case.
+
 
 * Noteworthy changes in release 9.1 (2022-04-15) [stable]
 
index 9a282e2f563bfa93eecb946414ba443d3aecf5e9..7f2ac801d7c7a483136f1d0855fc3811956ca6c5 100644 (file)
@@ -403,6 +403,8 @@ main (int argc, char **argv)
   char *reference = NULL;
   struct stat refstats;
   bool ok;
+  bool discarded_datestr = false;
+  bool discarded_set_datestr = false;
 
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
@@ -420,6 +422,8 @@ main (int argc, char **argv)
       switch (optc)
         {
         case 'd':
+          if (datestr)
+            discarded_datestr = true;
           datestr = optarg;
           break;
         case DEBUG_DATE_PARSING_OPTION:
@@ -469,6 +473,8 @@ main (int argc, char **argv)
           new_format = rfc_email_format;
           break;
         case 's':
+          if (set_datestr)
+            discarded_set_datestr = true;
           set_datestr = optarg;
           set_date = true;
           break;
@@ -511,6 +517,12 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }
 
+  if (discarded_datestr && (parse_datetime_flags & PARSE_DATETIME_DEBUG))
+    error (0, 0, _("only using last of multiple -d options"));
+
+  if (discarded_set_datestr && (parse_datetime_flags & PARSE_DATETIME_DEBUG))
+    error (0, 0, _("only using last of multiple -s options"));
+
   if (optind < argc)
     {
       if (optind + 1 < argc)
index 0b5217611009ef2010cb3accbfeaed6c42e7e27c..5c38dee41bb8f8f8ead892228adba0b1ff55e578 100755 (executable)
@@ -298,4 +298,14 @@ sed '1s/(Y-M-D) [0-9][0-9][0-9][0-9]-/(Y-M-D) XXXX-/' out9_t > out9 \
 compare exp9 out9 || fail=1
 
 
+# Diagnose discarded -d arguments
+echo 'date: only using last of multiple -d options' > exp10 \
+    || framework_failure_
+cat exp9 >> exp10 || framework_failure_
+date -u --debug -d 'discard' -d 'Apr 11 22:59:00 2011' > out10_t 2>&1 || fail=1
+sed '2s/(Y-M-D) [0-9][0-9][0-9][0-9]-/(Y-M-D) XXXX-/' out10_t >> out10 \
+    || framework_failure_
+compare exp10 out10 || fail=1
+
+
 Exit $fail