Hello,

On 2019-02-15 1:19 p.m., Eric Blake wrote:
On 2/15/19 12:32 PM, Assaf Gordon wrote:
There is at least one change in behavior, not sure if this is
bad enough to be a regression or doesn't really matter:

   $ yes-OLD me -- --help | head -n1
   me -- --help

   $ yes-NEW me -- --help | head -n1
   me --help

I would argue bug-fix.
[...]
So, I would suspect (although I have not yet tesed) that as patched, you
would get:

$ yes-NEW me -- --help | head -n1
me --help
$ POSIXLY_CORRECT=1 yes-NEW me -- --help | head -n1
me -- --help
$ yes-NEW -- me -- --help
me -- --help

Indeed - that's how it behaves with the patch.

Thanks for explaining.

In the gnulib patch:
s/optional/option/

In the coreutils patch:
s/non-options/non-option/

Attached updates with your suggested fixes.


Also, all coreutils callers pass reset_optind==false; does the gnulib
interface still need to provide a reset_optind parameter, given that
setting the parameter true forces reliance on the getopt-gnu module as
currently coded?

The "getopt-gnu" was already a dependency before this patch,
not sure if removing this parameter will save much hassle - what do you
think ?

-assaf


>From 08d0505683cebed0fc10cff082255fd79da2d989 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <m...@bernhard-voelker.de>
Date: Thu, 29 Nov 2018 09:06:26 +0100
Subject: [PATCH] long-options: add parse_gnu_standard_options_only

Discussed in https://bugs.gnu.org/33468 .

* lib/long-options.c (parse_long_options): Use EXIT_SUCCESS instead
of 0.
(parse_gnu_standard_options_only): Add function to
process the GNU default options --help and --version and fail for any other
unknown long or short option. See
https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html .
* lib/long-options.h (parse_gnu_standard_options_only): Declare it.
* modules/long-options (depends-on): Add stdbool, exitfail.
* top/maint.mk (sc_prohibit_long_options_without_use): Update
syntax-check rule, add new function name.
---
 lib/long-options.c   | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/long-options.h   | 17 +++++++++++++
 modules/long-options |  2 ++
 top/maint.mk         |  2 +-
 4 files changed, 87 insertions(+), 2 deletions(-)

diff --git a/lib/long-options.c b/lib/long-options.c
index 037f74b3a..b7acdb040 100644
--- a/lib/long-options.c
+++ b/lib/long-options.c
@@ -29,6 +29,7 @@
 #include <getopt.h>
 
 #include "version-etc.h"
+#include "exitfail.h"
 
 static struct option const long_options[] =
 {
@@ -71,7 +72,7 @@ parse_long_options (int argc,
             va_list authors;
             va_start (authors, usage_func);
             version_etc_va (stdout, command_name, package, version, authors);
-            exit (0);
+            exit (EXIT_SUCCESS);
           }
 
         default:
@@ -87,3 +88,68 @@ parse_long_options (int argc,
      the probably-new parameters when/if getopt is called later.  */
   optind = 0;
 }
+
+/* Process the GNU default long options --help and --version (see also
+   https://gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html),
+   and fail for any other unknown long or short option.
+   Use with SCAN_ALL=true to scan until "--", or with SCAN_ALL=false to stop
+   at the first non-option argument (or "--", whichever comes first).
+
+   if RESET_OPTIND=true, the global optind variable will be reset to zero,
+   preparing (and requiring) a follow-up gnu-compatible getopt() call
+   (non-gnu getopt functions use optreset=optind=1 instead of 0 for reset).
+
+   if RESET_OPTIND=false, optind is left as-is (suitable for programs
+   which do not process further option parameters (but could still
+   process parameters directly by examining argv[optind]).  */
+void
+parse_gnu_standard_options_only (int argc,
+                                 char **argv,
+                                 const char *command_name,
+                                 const char *package,
+                                 const char *version,
+                                 bool scan_all,
+                                 bool reset_optind,
+                                 void (*usage_func) (int),
+                                 /* const char *author1, ...*/ ...)
+{
+  int c;
+  int saved_opterr;
+
+  saved_opterr = opterr;
+
+  /* Print an error message for unrecognized options.  */
+  opterr = 1;
+
+  const char *optstring = scan_all ? "" : "+";
+
+  if ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1)
+    {
+      switch (c)
+        {
+        case 'h':
+          (*usage_func) (EXIT_SUCCESS);
+          break;
+
+        case 'v':
+          {
+            va_list authors;
+            va_start (authors, usage_func);
+            version_etc_va (stdout, command_name, package, version, authors);
+            exit (EXIT_SUCCESS);
+          }
+
+        default:
+          (*usage_func) (exit_failure);
+          break;
+        }
+    }
+
+  /* Restore previous value.  */
+  opterr = saved_opterr;
+
+  /* Reset this to zero so that getopt internals get initialized from
+     the probably-new parameters when/if getopt is called later.  */
+  if (reset_optind)
+    optind = 0;
+}
diff --git a/lib/long-options.h b/lib/long-options.h
index f4d6a83d5..01bea4c27 100644
--- a/lib/long-options.h
+++ b/lib/long-options.h
@@ -17,6 +17,11 @@
 
 /* Written by Jim Meyering.  */
 
+#ifndef LONG_OPTIONS_H_
+# define LONG_OPTIONS_H_ 1
+
+# include <stdbool.h>
+
 void parse_long_options (int _argc,
                          char **_argv,
                          const char *_command_name,
@@ -24,3 +29,15 @@ void parse_long_options (int _argc,
                          const char *_version,
                          void (*_usage) (int),
                          /* const char *author1, ...*/ ...);
+
+void parse_gnu_standard_options_only (int argc,
+                                      char **argv,
+                                      const char *command_name,
+                                      const char *package,
+                                      const char *version,
+                                      bool scan_all,
+                                      bool reset_optind,
+                                      void (*usage_func) (int),
+                                      /* const char *author1, ...*/ ...);
+
+#endif /* LONG_OPTIONS_H_ */
diff --git a/modules/long-options b/modules/long-options
index f1106c3cc..b845d4293 100644
--- a/modules/long-options
+++ b/modules/long-options
@@ -6,7 +6,9 @@ lib/long-options.h
 lib/long-options.c
 
 Depends-on:
+exitfail
 getopt-gnu
+stdbool
 stdlib
 version-etc
 
diff --git a/top/maint.mk b/top/maint.mk
index 4e37efeb5..e9d5ee7d4 100644
--- a/top/maint.mk
+++ b/top/maint.mk
@@ -537,7 +537,7 @@ sc_prohibit_quote_without_use:
 
 # Don't include this header unless you use one of its functions.
 sc_prohibit_long_options_without_use:
-	@h='long-options.h' re='\<parse_long_options *\(' \
+	@h='long-options.h' re='\<parse_(long_options|gnu_standard_options_only) *\(' \
 	  $(_sc_header_without_use)
 
 # Don't include this header unless you use one of its functions.
-- 
2.11.0

>From 7e18fffe4a9a3c312feed2dadf61c2ae02061fe9 Mon Sep 17 00:00:00 2001
From: Bernhard Voelker <m...@bernhard-voelker.de>
Date: Mon, 26 Nov 2018 09:05:37 +0100
Subject: [PATCH] all: detect --help and --version more consistently

For select programs which accept only --help and --version options
(in addition to non-option arguments), process these options before
any other options.

Before:

  $ yes --help me
  yes: unrecognized option '--help'
  Try 'yes --help' for more information.

  $ yes me --help
  me --help
  me --help
  ...

After:
Any occurrence of '--help' in yes's arguments (prior to '--') will
show the help screen.

Discussed in https://bugs.gnu.org/33468 .

* NEWS: Mention change.
* src/cksum.c, src/dd.c, src/hostid.c, src/hostname.c, src/link.c,
src/logname.c, src/nohup.c, src/sleep.c, src/tsort.c, src/unlink.c,
src/uptime.c, src/users.c, src/whoami.c, src/yes.c (main): Replace
parse_long_options() + getopt_long() calls with
parse_gnu_standard_options_only(); Remove <getopt.h> inclusion;
Remove empty 'struct long_options' variable;
* tests/misc/help-version-getopt.sh: New test.
* tests/local.mk (all_tests): Add new test.
---
 NEWS                              |  8 ++++++
 src/cksum.c                       | 13 +++------
 src/dd.c                          | 14 +++-------
 src/hostid.c                      | 13 +++------
 src/hostname.c                    | 13 +++------
 src/link.c                        | 13 +++------
 src/logname.c                     | 13 +++------
 src/nohup.c                       | 13 +++------
 src/sleep.c                       | 13 +++------
 src/tsort.c                       | 13 +++------
 src/unlink.c                      | 13 +++------
 src/uptime.c                      | 13 +++------
 src/users.c                       | 13 +++------
 src/whoami.c                      | 13 +++------
 src/yes.c                         | 13 +++------
 tests/local.mk                    |  1 +
 tests/misc/help-version-getopt.sh | 57 +++++++++++++++++++++++++++++++++++++++
 17 files changed, 108 insertions(+), 141 deletions(-)
 create mode 100755 tests/misc/help-version-getopt.sh

diff --git a/NEWS b/NEWS
index fdde47593..5757602d6 100644
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,14 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** Changes in behavior
 
+  cksum, dd, hostid, hostname, link, logname, sleep, tsort, unlink,
+  uptime, users, whoami, yes: now always process --help and --version options,
+  regardless of any other arguments present before any optional '--'
+  end-of-options marker.
+
+  nohup: now process --help and --version as first options even if other
+  parameters follow.
+
   echo now always processes backslash escapes when the POSIXLY_CORRECT
   environment variable is set.
 
diff --git a/src/cksum.c b/src/cksum.c
index 6974724ed..daf1a8951 100644
--- a/src/cksum.c
+++ b/src/cksum.c
@@ -102,16 +102,10 @@ main (void)
 
 #else /* !CRCTAB */
 
-# include <getopt.h>
 # include "long-options.h"
 # include "die.h"
 # include "error.h"
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 /* Number of bytes to read at once.  */
 # define BUFLEN (1 << 16)
 
@@ -294,10 +288,9 @@ main (int argc, char **argv)
      so that processes running in parallel do not intersperse their output.  */
   setvbuf (stdout, NULL, _IOLBF, 0);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE, Version,
+                                   true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   have_read_stdin = false;
 
diff --git a/src/dd.c b/src/dd.c
index 28611b3e6..92e768283 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -22,7 +22,6 @@
 
 #include <sys/types.h>
 #include <signal.h>
-#include <getopt.h>
 
 #include "system.h"
 #include "close-stream.h"
@@ -46,11 +45,6 @@
   proper_name ("David MacKenzie"), \
   proper_name ("Stuart Kemp")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
    present.  */
 #ifndef SA_NOCLDSTOP
@@ -2396,13 +2390,11 @@ main (int argc, char **argv)
 
   page_size = getpagesize ();
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
-                      usage, AUTHORS, (char const *) NULL);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE, Version,
+                                   true, false, usage, AUTHORS,
+                                   (char const *) NULL);
   close_stdout_required = false;
 
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
-
   /* Initialize translation table to identity translation. */
   for (i = 0; i < 256; i++)
     trans_table[i] = i;
diff --git a/src/hostid.c b/src/hostid.c
index 06b05b9c3..20fd90042 100644
--- a/src/hostid.c
+++ b/src/hostid.c
@@ -18,7 +18,6 @@
 /* Written by Jim Meyering.  */
 
 #include <config.h>
-#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -32,11 +31,6 @@
 
 #define AUTHORS proper_name ("Jim Meyering")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -69,10 +63,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (optind < argc)
     {
diff --git a/src/hostname.c b/src/hostname.c
index b23df32b1..5f91ed2c8 100644
--- a/src/hostname.c
+++ b/src/hostname.c
@@ -17,7 +17,6 @@
 /* Written by Jim Meyering.  */
 
 #include <config.h>
-#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 
@@ -33,11 +32,6 @@
 
 #define AUTHORS proper_name ("Jim Meyering")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 #if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
      defined HAVE_SYS_SYSTEMINFO_H
 # include <sys/systeminfo.h>
@@ -86,10 +80,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (argc == optind + 1)
     {
diff --git a/src/link.c b/src/link.c
index 84b916be2..830e40ec2 100644
--- a/src/link.c
+++ b/src/link.c
@@ -22,7 +22,6 @@
 
 #include <config.h>
 #include <stdio.h>
-#include <getopt.h>
 #include <sys/types.h>
 
 #include "system.h"
@@ -36,11 +35,6 @@
 
 #define AUTHORS proper_name ("Michael Stone")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -72,10 +66,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (argc < optind + 2)
     {
diff --git a/src/logname.c b/src/logname.c
index da97db864..23dc4eb5d 100644
--- a/src/logname.c
+++ b/src/logname.c
@@ -17,7 +17,6 @@
 #include <config.h>
 #include <stdio.h>
 #include <sys/types.h>
-#include <getopt.h>
 
 #include "system.h"
 #include "die.h"
@@ -30,11 +29,6 @@
 
 #define AUTHORS proper_name ("FIXME: unknown")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -67,10 +61,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (optind < argc)
     {
diff --git a/src/nohup.c b/src/nohup.c
index d5ee0f2df..b35cf9ba6 100644
--- a/src/nohup.c
+++ b/src/nohup.c
@@ -17,7 +17,6 @@
 /* Written by Jim Meyering  */
 
 #include <config.h>
-#include <getopt.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <signal.h>
@@ -34,11 +33,6 @@
 
 #define AUTHORS proper_name ("Jim Meyering")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 /* Exit statuses.  */
 enum
   {
@@ -104,10 +98,9 @@ main (int argc, char **argv)
   initialize_exit_failure (exit_internal_failure);
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
-    usage (exit_internal_failure);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, false, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (argc <= optind)
     {
diff --git a/src/sleep.c b/src/sleep.c
index a634f1ae6..1b60111d4 100644
--- a/src/sleep.c
+++ b/src/sleep.c
@@ -17,7 +17,6 @@
 #include <config.h>
 #include <stdio.h>
 #include <sys/types.h>
-#include <getopt.h>
 
 #include "system.h"
 #include "cl-strtod.h"
@@ -35,11 +34,6 @@
   proper_name ("Jim Meyering"), \
   proper_name ("Paul Eggert")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -113,10 +107,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (argc == 1)
     {
diff --git a/src/tsort.c b/src/tsort.c
index 7db35503d..995aefb07 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -23,7 +23,6 @@
 #include <config.h>
 
 #include <assert.h>
-#include <getopt.h>
 #include <sys/types.h>
 
 #include "system.h"
@@ -40,11 +39,6 @@
 
 #define AUTHORS proper_name ("Mark Kettenis")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 /* Token delimiters when reading from a file.  */
 #define DELIM " \t\n"
 
@@ -556,10 +550,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (1 < argc - optind)
     {
diff --git a/src/unlink.c b/src/unlink.c
index 6ee3d670a..f0559c1c6 100644
--- a/src/unlink.c
+++ b/src/unlink.c
@@ -22,7 +22,6 @@
 
 #include <config.h>
 #include <stdio.h>
-#include <getopt.h>
 #include <sys/types.h>
 
 #include "system.h"
@@ -36,11 +35,6 @@
 
 #define AUTHORS proper_name ("Michael Stone")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -71,10 +65,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (argc < optind + 1)
     {
diff --git a/src/uptime.c b/src/uptime.c
index ff1e8796a..bc03458c6 100644
--- a/src/uptime.c
+++ b/src/uptime.c
@@ -17,7 +17,6 @@
 /* Created by hacking who.c by Kaveh Ghazi gh...@caip.rutgers.edu.  */
 
 #include <config.h>
-#include <getopt.h>
 #include <stdio.h>
 
 #include <sys/types.h>
@@ -47,11 +46,6 @@
   proper_name ("David MacKenzie"), \
   proper_name ("Kaveh Ghazi")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 static void
 print_uptime (size_t n, const STRUCT_UTMP *this)
 {
@@ -239,10 +233,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   switch (argc - optind)
     {
diff --git a/src/users.c b/src/users.c
index bf0706ca5..86148633d 100644
--- a/src/users.c
+++ b/src/users.c
@@ -17,7 +17,6 @@
 /* Written by jla; revised by djm */
 
 #include <config.h>
-#include <getopt.h>
 #include <stdio.h>
 
 #include <sys/types.h>
@@ -36,11 +35,6 @@
   proper_name ("Joseph Arceneaux"), \
   proper_name ("David MacKenzie")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 static int
 userid_compare (const void *v_a, const void *v_b)
 {
@@ -133,10 +127,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   switch (argc - optind)
     {
diff --git a/src/whoami.c b/src/whoami.c
index 65a360bb7..1e89c12a5 100644
--- a/src/whoami.c
+++ b/src/whoami.c
@@ -22,7 +22,6 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <pwd.h>
-#include <getopt.h>
 
 #include "system.h"
 #include "die.h"
@@ -35,11 +34,6 @@
 
 #define AUTHORS proper_name ("Richard Mlynarik")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -75,10 +69,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   if (optind != argc)
     {
diff --git a/src/yes.c b/src/yes.c
index a808bb953..a6fe2d7ed 100644
--- a/src/yes.c
+++ b/src/yes.c
@@ -19,7 +19,6 @@
 #include <config.h>
 #include <stdio.h>
 #include <sys/types.h>
-#include <getopt.h>
 
 #include "system.h"
 
@@ -32,11 +31,6 @@
 
 #define AUTHORS proper_name ("David MacKenzie")
 
-static struct option const long_options[] =
-{
-  {NULL, 0, NULL, 0}
-};
-
 void
 usage (int status)
 {
@@ -72,10 +66,9 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                      usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
-    usage (EXIT_FAILURE);
+  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
+                                   Version, true, false, usage, AUTHORS,
+                                   (char const *) NULL);
 
   char **operands = argv + optind;
   char **operand_lim = argv + argc;
diff --git a/tests/local.mk b/tests/local.mk
index 4751886b2..bcb61edc7 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -163,6 +163,7 @@ check-root:
 
 all_tests =					\
   tests/misc/help-version.sh			\
+  tests/misc/help-version-getopt.sh		\
   tests/tail-2/inotify-race.sh			\
   tests/tail-2/inotify-race2.sh			\
   tests/misc/invalid-opt.pl			\
diff --git a/tests/misc/help-version-getopt.sh b/tests/misc/help-version-getopt.sh
new file mode 100755
index 000000000..bd5b05814
--- /dev/null
+++ b/tests/misc/help-version-getopt.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# Ensure --version and --help options are processed before
+# any other options.
+
+# Copyright (C) 2019 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+
+# Terminate any background processes
+cleanup_() { kill $pid 2>/dev/null && wait $pid; }
+
+programs="cksum dd hostid hostname link logname nohup
+sleep tsort unlink uptime users whoami yes"
+
+# All these variations should show the help/version screen
+# regardless of their position on the command line arguments.
+for i in $programs; do
+  # skip if the program is not built (e.g., hostname)
+  expr match "$built_programs" ".*$i" 2>/dev/null || continue
+
+  env $i --help    >/dev/null || fail=1
+  env $i --version >/dev/null || fail=1
+  env $i --help me    >/dev/null || fail=1
+  env $i --version me >/dev/null || fail=1
+
+  # nohup is an exception: stops processing after first non-option parameter.
+  # e.g., "nohup uname --help" should run "uname --help", not "nohup --help".
+  test "$i" = nohup && continue
+
+  env $i me --help    >/dev/null || fail=1
+  env $i me --version >/dev/null || fail=1
+done
+
+# After end-of-options marker (--), the options should not be parsed.
+# Test with 'yes', and assume the common code will work the
+# same for the other programs.
+cat >exp <<\EOF || framework_failure_
+--help
+EOF
+env yes -- --help | head -n1 > out
+compare exp out
+
+
+Exit $fail
-- 
2.11.0

Reply via email to