From 87cf6ab10f6a671b18735d983f9171d12c716619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 9 May 2019 19:03:07 +0200 Subject: [PATCH] main: use getopt() to parse command line arguments --- main.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 46be1d9..50d9804 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -117,15 +118,47 @@ out: return bar; } +static void +print_usage(const char *prog_name) +{ + printf("Usage: %s [OPTION]...\n", prog_name); + printf("\n"); + printf("Options:\n"); + printf(" -v,--version print f00sel version and quit\n"); +} + int -main(int argc, const char *const *argv) +main(int argc, char *const *argv) { setlocale(LC_ALL, ""); - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { + static const struct option longopts[] = { + {"version", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {NULL, no_argument, 0, 0}, + }; + + while (true) { + int c = getopt_long(argc, argv, ":vh", longopts, NULL); + if (c == -1) + break; + + switch (c) { + case 'v': printf("f00bar version %s\n", F00BAR_VERSION); - return 0; + return EXIT_SUCCESS; + + case 'h': + print_usage(argv[0]); + return EXIT_SUCCESS; + + case ':': + fprintf(stderr, "error: -%c: missing required argument\n", optopt); + return EXIT_FAILURE; + + case '?': + fprintf(stderr, "error: -%c: invalid option\n", optopt); + return EXIT_FAILURE; } }