From 92e42a3d9acf2dc19628033d76a9fdd31869d00d Mon Sep 17 00:00:00 2001 From: buffet Date: Tue, 19 Mar 2019 21:58:29 +0100 Subject: [PATCH] Not worth implementing my own parser for those 3 options --- kiwmi/main.c | 90 +++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 65 deletions(-) diff --git a/kiwmi/main.c b/kiwmi/main.c index 48acc42..c888063 100644 --- a/kiwmi/main.c +++ b/kiwmi/main.c @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -16,26 +17,11 @@ #include "kiwmi/server.h" -struct kiwmi_config { - int verbosity; - const char *frontend_path; -}; - -struct option { - const char *name; - int val; -}; - -static void -parse_arguments(int argc, char **argv, struct kiwmi_config *config) +int +main(int argc, char **argv) { - static struct option options[] = { - {"help", 'h'}, - {"version", 'v'}, - {"verbose", 'V'}, - - {0, 0}, - }; + int verbosity = 0; + const char *frontend_path = NULL; const char *usage = "Usage: kiwmi [options] FRONTEND\n" @@ -44,61 +30,35 @@ parse_arguments(int argc, char **argv, struct kiwmi_config *config) " -v, --version Show version number and quit\n" " -V, --verbose Increase verbosity Level\n"; - config->verbosity = 0; - config->frontend_path = NULL; - - for (int i = 1; i < argc; ++i) { - const char *opt = argv[i]; - char val = '?'; - - if (opt[0] == '-') { - if (opt[1] == '-') { - for (const struct option *current = options; current->name; - ++current) { - if (strcmp(&opt[2], current->name)) { - val = current->val; - break; - } - } - } else { - val = opt[1]; - } - - switch (val) { - case 'h': - printf("%s", usage); - exit(EXIT_SUCCESS); - break; - case 'v': - printf("kiwmi version " KIWMI_VERSION "\n"); - exit(EXIT_SUCCESS); - break; - case 'V': - ++config->verbosity; - break; - default: - fprintf(stderr, "%s", usage); - exit(EXIT_FAILURE); - } - } else { - config->frontend_path = opt; + int option; + while ((option = getopt(argc, argv, "hvV")) != -1) { + switch (option) { + case 'h': + printf("%s", usage); + exit(EXIT_SUCCESS); break; + case 'v': + printf("kiwmi version " KIWMI_VERSION "\n"); + exit(EXIT_SUCCESS); + break; + case 'V': + ++verbosity; + break; + default: + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); } } - if (!config->frontend_path) { + // no frontend passsed + if (optind >= argc) { fprintf(stderr, "%s", usage); exit(EXIT_FAILURE); } -} -int -main(int argc, char **argv) -{ - struct kiwmi_config config; - parse_arguments(argc, argv, &config); + frontend_path = argv[optind]; - switch (config.verbosity) { + switch (verbosity) { case 0: wlr_log_init(WLR_ERROR, NULL); break;