Commit 7d36a3d119826d80e48f397eb070a798f641423e

Peter Hutterer 2020-07-13T12:26:07

tools: switch how-to-type to getopt_long This provides consistency with the other tools that now all take long options. Plus, it's more obvious to have the arguments spelled out. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>

diff --git a/tools/how-to-type.c b/tools/how-to-type.c
index 441db38..903a3ed 100644
--- a/tools/how-to-type.c
+++ b/tools/how-to-type.c
@@ -23,6 +23,7 @@
 
 #include "config.h"
 
+#include <getopt.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
@@ -32,19 +33,17 @@
 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
 
 static void
-usage(const char *argv0)
+usage(const char *argv0, FILE *fp)
 {
-    fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
-            "[-l <layout>] [-v <variant>] [-o <options>] <unicode codepoint>\n",
-            argv0);
-    fprintf(stderr, "Pipe into `column -ts $'\\t'` for nicely aligned output.\n");
-    exit(2);
+    fprintf(fp, "Usage: %s [--rules <rules>] [--model <model>] "
+                "[--layout <layout>] [--variant <variant>] [--options <options>]"
+                " <unicode codepoint>\n", argv0);
+    fprintf(fp, "Pipe into `column -ts $'\\t'` for nicely aligned output.\n");
 }
 
 int
 main(int argc, char *argv[])
 {
-    int opt;
     const char *rules = NULL;
     const char *model = NULL;
     const char *layout_ = NULL;
@@ -61,38 +60,64 @@ main(int argc, char *argv[])
     struct xkb_keymap *keymap = NULL;
     xkb_keycode_t min_keycode, max_keycode;
     xkb_mod_index_t num_mods;
+    enum options {
+        OPT_RULES,
+        OPT_MODEL,
+        OPT_LAYOUT,
+        OPT_VARIANT,
+        OPT_OPTIONS,
+    };
+    static struct option opts[] = {
+        {"help",                 no_argument,            0, 'h'},
+        {"rules",                required_argument,      0, OPT_RULES},
+        {"model",                required_argument,      0, OPT_MODEL},
+        {"layout",               required_argument,      0, OPT_LAYOUT},
+        {"variant",              required_argument,      0, OPT_VARIANT},
+        {"options",              required_argument,      0, OPT_OPTIONS},
+        {0, 0, 0, 0},
+    };
+
+    while (1) {
+        int opt;
+        int option_index = 0;
+
+        opt = getopt_long(argc, argv, "h", opts, &option_index);
+        if (opt == -1)
+            break;
 
-    while ((opt = getopt(argc, argv, "r:m:l:v:o:")) != -1) {
         switch (opt) {
-        case 'r':
+        case OPT_RULES:
             rules = optarg;
             break;
-        case 'm':
+        case OPT_MODEL:
             model = optarg;
             break;
-        case 'l':
+        case OPT_LAYOUT:
             layout_ = optarg;
             break;
-        case 'v':
+        case OPT_VARIANT:
             variant = optarg;
             break;
-        case 'o':
+        case OPT_OPTIONS:
             options = optarg;
             break;
+        case 'h':
+            usage(argv[0], stdout);
+            exit(EXIT_SUCCESS);
         default:
-            usage(argv[0]);
+            usage(argv[0], stderr);
             exit(EXIT_INVALID_USAGE);
         }
     }
     if (argc - optind != 1) {
-        usage(argv[0]);
+        usage(argv[0], stderr);
         exit(EXIT_INVALID_USAGE);
     }
 
     errno = 0;
     val = strtol(argv[optind], &endp, 0);
     if (errno != 0 || endp == argv[optind] || val < 0 || val > 0x10FFFF) {
-        usage(argv[0]);
+        usage(argv[0], stderr);
         exit(EXIT_INVALID_USAGE);
     }
     codepoint = (uint32_t) val;
diff --git a/tools/xkbcli-how-to-type.1.ronn b/tools/xkbcli-how-to-type.1.ronn
index f89ca76..fc3faa4 100644
--- a/tools/xkbcli-how-to-type.1.ronn
+++ b/tools/xkbcli-how-to-type.1.ronn
@@ -13,19 +13,19 @@ Pipe into `column -ts $'\\t'` for nicely aligned output.
 
 ## OPTIONS
 
-  * ` -r <rules>`:
+  * `--rules <rules>`:
       The XKB ruleset
 
-  * ` -m <model>`:
+  * `--model <model>`:
       The XKB model
 
-  * `-l <layout>`:
+  * `--layout <layout>`:
       The XKB layout
 
-  * `-v <variant>`:
+  * `--variant <variant>`:
       The XKB layout variant
 
-  * `-o <options>`:
+  * `--options <options>`:
       The XKB options
 
 ## SEE ALSO