From 7d36a3d119826d80e48f397eb070a798f641423e Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Mon, 13 Jul 2020 12:26:07 +1000 Subject: 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 --- tools/how-to-type.c | 57 +++++++++++++++++++++++++++++------------ tools/xkbcli-how-to-type.1.ronn | 10 ++++---- 2 files changed, 46 insertions(+), 21 deletions(-) (limited to 'tools') 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 #include #include #include @@ -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 ] [-m ] " - "[-l ] [-v ] [-o ] \n", - argv0); - fprintf(stderr, "Pipe into `column -ts $'\\t'` for nicely aligned output.\n"); - exit(2); + fprintf(fp, "Usage: %s [--rules ] [--model ] " + "[--layout ] [--variant ] [--options ]" + " \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 `: The XKB ruleset - * ` -m `: + * `--model `: The XKB model - * `-l `: + * `--layout `: The XKB layout - * `-v `: + * `--variant `: The XKB layout variant - * `-o `: + * `--options `: The XKB options ## SEE ALSO -- cgit v1.2.1