]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
usage: reflect ./configure choices in available command line options
authorSami Kerola <kerolasa@iki.fi>
Sun, 21 Aug 2016 09:00:11 +0000 (10:00 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 21 Aug 2016 15:19:15 +0000 (16:19 +0100)
Features that are turned off should not be available to users as an options.
This commit also ensures matching long and short options, and makes the
long-only --displaymode option to be totally unreachable with short options.

mtr.c

diff --git a/mtr.c b/mtr.c
index cc015377c633e2cf1706e53d658808175b72c8c2..bce7822d51347561590d0e5f6930741f9f8ba8d9 100644 (file)
--- a/mtr.c
+++ b/mtr.c
@@ -155,14 +155,20 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
   fputs(" -C, --csv                  output comma separated values\n", out);
   fputs(" -l, --raw                  output raw format\n", out);
   fputs(" -p, --split                split output\n", out);
+#ifdef HAVE_NCURSES
   fputs(" -t, --curses               use curses terminal interface\n", out);
+#endif
   fputs("     --displaymode MODE     select initial display mode\n", out);
+#ifdef HAVE_GTK
   fputs(" -g, --gtk                  use GTK+ xwindow interface\n", out);
+#endif
   fputs(" -n, --no-dns               do not resove host names\n", out);
   fputs(" -b, --show-ips             show IP numbers and host names\n", out);
   fputs(" -o, --order FIELDS         select output fields\n", out);
+#ifdef HAVE_IPINFO
   fputs(" -y, --ipinfo NUMBER        select ip information in output\n", out);
   fputs(" -z, --aslookup             display AS number\n", out);
+#endif
   fputs(" -h, --help                 display this help and exit\n", out);
   fputs(" -v, --version              output version information and exit\n", out);
   fputs("\n", out);
@@ -306,29 +312,36 @@ void parse_arg (int argc, char **argv)
   /* IMPORTANT: when adding or modifying an option:
        0/ try to find a somewhat logical order;
        1/ add the long option name in "long_options" below;
-       2/ add the short option name in the "getopt_long" call;
-       3/ update the man page (use the same order);
-       4/ update the help message (see PrintHelp).
+       2/ update the man page (use the same order);
+       3/ update the help message (see usage() function).
    */
+  enum {
+    OPT_DISPLAYMODE = CHAR_MAX + 1
+  };
   static struct option long_options[] = {
     /* option name, has argument, NULL, short name */
     { "help",           0, NULL, 'h' },
     { "version",        0, NULL, 'v' },
 
     { "inet",           0, NULL, '4' }, /* IPv4 only */
+#ifdef ENABLE_IPV6
     { "inet6",          0, NULL, '6' }, /* IPv6 only */
-
+#endif
     { "filename",       1, NULL, 'F' },
 
     { "report",         0, NULL, 'r' },
     { "report-wide",    0, NULL, 'w' },
     { "xml",            0, NULL, 'x' },
+#ifdef HAVE_NCURSES
     { "curses",         0, NULL, 't' },
+#endif
+#ifdef HAVE_GTK
     { "gtk",            0, NULL, 'g' },
+#endif
     { "raw",            0, NULL, 'l' },
     { "csv",            0, NULL, 'C' },
     { "json",           0, NULL, 'j' },
-    { "displaymode",    1, NULL, 'd' },
+    { "displaymode",    1, NULL, OPT_DISPLAYMODE },
     { "split",          0, NULL, 'p' }, /* BL */
                                         /* maybe above should change to -d 'x' */
 
@@ -362,11 +375,26 @@ void parse_arg (int argc, char **argv)
 #endif
     { 0, 0, 0, 0 }
   };
+  static const size_t num_options = sizeof(long_options) / sizeof(struct option);
+  char short_options[num_options * 2];
+  size_t n, p;
+
+  for (n = p = 0; n < num_options; n++) {
+    if (CHAR_MAX < long_options[n].val) {
+      continue;
+    }
+    short_options[p] = long_options[n].val;
+    p++;
+    if (long_options[n].has_arg == 1) {
+      short_options[p] = ':';
+      p++;
+    }
+    /* optional options need two ':', but ignore them now as they are not in use */
+  }
 
   opt = 0;
   while(1) {
-    opt = getopt_long(argc, argv,
-                     "hv46F:rwxtglCjpnbo:y:zi:c:s:B:Q:ea:f:m:U:uTSP:L:Z:G:M:", long_options, NULL);
+    opt = getopt_long(argc, argv, short_options, long_options, NULL);
     if(opt == -1)
       break;
 
@@ -385,12 +413,16 @@ void parse_arg (int argc, char **argv)
       reportwide = 1;
       DisplayMode = DisplayReport;
       break;
+#ifdef HAVE_NCURSES
     case 't':
       DisplayMode = DisplayCurses;
       break;
+#endif
+#ifdef HAVE_GTK
     case 'g':
       DisplayMode = DisplayGTK;
       break;
+#endif
     case 'p':                 /* BL */
       DisplayMode = DisplaySplit;
       break;
@@ -407,7 +439,7 @@ void parse_arg (int argc, char **argv)
       DisplayMode = DisplayXML;
       break;
 
-    case 'd':
+    case OPT_DISPLAYMODE:
       display_mode = (atoi (optarg)) % 3;
       break;
     case 'c':