groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 08/30: [utils]: Migrate to modern getopt_long(3) usage.


From: G. Branden Robinson
Subject: [groff] 08/30: [utils]: Migrate to modern getopt_long(3) usage.
Date: Sun, 22 Dec 2024 19:39:16 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit cd538ed5abb98f8cca6c8cccfea735abe2728ae6
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Sat Dec 21 15:36:49 2024 -0600

    [utils]: Migrate to modern getopt_long(3) usage.
    
    * src/utils/hpftodit/hpftodit.cpp (main):
    * src/utils/indxbib/indxbib.cpp (main):
    * src/utils/lkbib/lkbib.cpp (main):
    * src/utils/lookbib/lookbib.cpp (main):
    * src/utils/pfbtops/pfbtops.c (main):
    * src/utils/tfmtodit/tfmtodit.cpp (main):
    * src/utils/xtotroff/xtotroff.c (main): Migrate to modern getopt_long(3)
      usage.  Drop `opterr` assignment; prefix the option string with ":"
      instead.  Handle return of `:`, emit an appropriate usage diagnostic,
      and exit with status 2.
---
 ChangeLog                       | 13 +++++++++++++
 src/utils/hpftodit/hpftodit.cpp |  9 +++++++--
 src/utils/indxbib/indxbib.cpp   |  9 +++++++--
 src/utils/lkbib/lkbib.cpp       |  9 +++++++--
 src/utils/lookbib/lookbib.cpp   |  9 +++++++--
 src/utils/pfbtops/pfbtops.c     | 10 ++++++++--
 src/utils/tfmtodit/tfmtodit.cpp | 10 ++++++++--
 src/utils/xtotroff/xtotroff.c   | 10 ++++++++--
 8 files changed, 65 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2e2e95430..764ea108e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-12-21  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/utils/hpftodit/hpftodit.cpp (main):
+       * src/utils/indxbib/indxbib.cpp (main):
+       * src/utils/lkbib/lkbib.cpp (main):
+       * src/utils/lookbib/lookbib.cpp (main):
+       * src/utils/pfbtops/pfbtops.c (main):
+       * src/utils/tfmtodit/tfmtodit.cpp (main):
+       * src/utils/xtotroff/xtotroff.c (main): Migrate to modern
+       getopt_long(3) usage.  Drop `opterr` assignment; prefix the
+       option string with ":" instead.  Handle return of `:`, emit an
+       appropriate usage diagnostic, and exit with status 2.
+
 2024-12-21  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/preproc/eqn/main.cpp (main):
diff --git a/src/utils/hpftodit/hpftodit.cpp b/src/utils/hpftodit/hpftodit.cpp
index 9ffa560f3..2cc107640 100644
--- a/src/utils/hpftodit/hpftodit.cpp
+++ b/src/utils/hpftodit/hpftodit.cpp
@@ -301,8 +301,7 @@ main(int argc, char **argv)
     { "version", no_argument, 0, 'v' },
     { NULL, 0, 0, 0 }
   };
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "adsqvi:", long_options, NULL))
+  while ((opt = getopt_long(argc, argv, ":adsqvi:", long_options, NULL))
         != EOF) {
     switch (opt) {
     case 'a':
@@ -333,6 +332,12 @@ main(int argc, char **argv)
       error("unrecognized command-line option '%1'", char(optopt));
       usage();
       break;
+    case ':':
+      error("command-line option '%1' requires an argument",
+           char(optopt));
+      usage(stderr);
+      exit(2);
+      break;
     default:
       assert(0);
     }
diff --git a/src/utils/indxbib/indxbib.cpp b/src/utils/indxbib/indxbib.cpp
index 03fa1f1fb..9671218ed 100644
--- a/src/utils/indxbib/indxbib.cpp
+++ b/src/utils/indxbib/indxbib.cpp
@@ -141,8 +141,7 @@ int main(int argc, char **argv)
     { "version", no_argument, 0, 'v' },
     { NULL, 0, 0, 0 }
   };
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "c:o:h:i:k:l:t:n:c:d:f:vw",
+  while ((opt = getopt_long(argc, argv, ":c:o:h:i:k:l:t:n:c:d:f:vw",
                            long_options, NULL))
         != EOF)
     switch (opt) {
@@ -199,6 +198,12 @@ int main(int argc, char **argv)
       usage(stderr);
       exit(2);
       break;
+    case ':':
+      error("command-line option '%1' requires an argument",
+           char(optopt));
+      usage(stderr);
+      exit(2);
+      break;
     default:
       assert(0 == "unhandled getopt_long return value");
       break;
diff --git a/src/utils/lkbib/lkbib.cpp b/src/utils/lkbib/lkbib.cpp
index 21bac2e95..be5962fe4 100644
--- a/src/utils/lkbib/lkbib.cpp
+++ b/src/utils/lkbib/lkbib.cpp
@@ -68,8 +68,7 @@ int main(int argc, char **argv)
     { "version", no_argument, 0, 'v' },
     { NULL, 0, 0, 0 }
   };
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "nvVi:t:p:", long_options,
+  while ((opt = getopt_long(argc, argv, ":nvVi:t:p:", long_options,
          NULL)) != EOF)
     switch (opt) {
     case 'V':
@@ -112,6 +111,12 @@ int main(int argc, char **argv)
       usage(stderr);
       exit(2);
       break;
+    case ':':
+      error("command-line option '%1' requires an argument",
+           char(optopt));
+      usage(stderr);
+      exit(2);
+      break;
     default:
       assert(0);
     }
diff --git a/src/utils/lookbib/lookbib.cpp b/src/utils/lookbib/lookbib.cpp
index 897116478..25171e257 100644
--- a/src/utils/lookbib/lookbib.cpp
+++ b/src/utils/lookbib/lookbib.cpp
@@ -75,8 +75,7 @@ int main(int argc, char **argv)
     { "version", no_argument, 0, 'v' },
     { NULL, 0, 0, 0 }
   };
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "vVi:t:", long_options, NULL))
+  while ((opt = getopt_long(argc, argv, ":vVi:t:", long_options, NULL))
         != EOF)
     switch (opt) {
     case 'V':
@@ -114,6 +113,12 @@ int main(int argc, char **argv)
       usage(stderr);
       exit(2);
       break;
+    case ':':
+      error("command-line option '%1' requires an argument",
+           char(optopt));
+      usage(stderr);
+      exit(2);
+      break;
     default:
       assert(0 == "unhandled case of command-line option");
     }
diff --git a/src/utils/pfbtops/pfbtops.c b/src/utils/pfbtops/pfbtops.c
index 274ca48a4..77beb284c 100644
--- a/src/utils/pfbtops/pfbtops.c
+++ b/src/utils/pfbtops/pfbtops.c
@@ -178,8 +178,7 @@ int main(int argc, char **argv)
 
   program_name = argv[0];
 
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "v", long_options, NULL))
+  while ((opt = getopt_long(argc, argv, ":v", long_options, NULL))
         != EOF) {
     switch (opt) {
     case 'v':
@@ -196,6 +195,13 @@ int main(int argc, char **argv)
       usage(stderr);
       exit(2);
       break;
+    // in case we ever accept options that take arguments
+    case ':':
+      fprintf(stderr, "%s: error: command-line option '%c' requires an"
+             " argument\n", program_name, (char) optopt);
+      usage(stderr);
+      exit(2);
+      break;
     default:
       assert(0 == "unhandled case of command-line option");
     }
diff --git a/src/utils/tfmtodit/tfmtodit.cpp b/src/utils/tfmtodit/tfmtodit.cpp
index 77a70bffd..08666d4b4 100644
--- a/src/utils/tfmtodit/tfmtodit.cpp
+++ b/src/utils/tfmtodit/tfmtodit.cpp
@@ -705,8 +705,7 @@ int main(int argc, char **argv)
     { "version", no_argument, 0, 'v' },
     { NULL, 0, 0, 0 }
   };
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "svg:k:", long_options, NULL))
+  while ((opt = getopt_long(argc, argv, ":svg:k:", long_options, NULL))
         != EOF)
     switch (opt) {
     case 'g':
@@ -739,7 +738,14 @@ int main(int argc, char **argv)
       error("unrecognized command-line option '%1'", char(optopt));
       usage(stderr);
       exit(2);
+    case ':':
+      error("command-line option '%1' requires an argument",
+           char(optopt));
+      usage(stderr);
+      exit(2);
+      break;
       break;
+    // XXX ?!
     case EOF:
       assert(0 == "EOF encountered in option processing");
     }
diff --git a/src/utils/xtotroff/xtotroff.c b/src/utils/xtotroff/xtotroff.c
index f9be2570a..0aa8a6bd6 100644
--- a/src/utils/xtotroff/xtotroff.c
+++ b/src/utils/xtotroff/xtotroff.c
@@ -291,8 +291,7 @@ int main(int argc, char **argv)
 
   program_name = argv[0];
 
-  opterr = 0;
-  while ((opt = getopt_long(argc, argv, "d:gr:s:v", long_options,
+  while ((opt = getopt_long(argc, argv, ":d:gr:s:v", long_options,
                            NULL)) != EOF) {
     switch (opt) {
     case 'd':
@@ -321,6 +320,13 @@ int main(int argc, char **argv)
       usage(stderr);
       xtotroff_exit(2);
       break;
+    case ':':
+      fprintf(stderr, "%s: error: command-line option '%c' requires an"
+             " argument\n", program_name, (char) optopt);
+      usage(stderr);
+      exit(2);
+      break;
+    // XXX: need assert() for default case
     }
   }
   if (argc - optind != 1) {



reply via email to

[Prev in Thread] Current Thread [Next in Thread]