tools: switch how-to-type to getopt_long
authorPeter Hutterer <peter.hutterer@who-t.net>
Mon, 13 Jul 2020 02:26:07 +0000 (12:26 +1000)
committerRan Benita <ran@unusedvar.com>
Sat, 25 Jul 2020 08:05:14 +0000 (11:05 +0300)
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>
tools/how-to-type.c
tools/xkbcli-how-to-type.1.ronn

index 441db38..903a3ed 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "config.h"
 
+#include <getopt.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
 #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;
index f89ca76..fc3faa4 100644 (file)
@@ -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