diff options
author | Taylor Holberton <tay10r@protonmail.com> | 2020-06-03 18:00:11 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-03 18:00:11 -0400 |
commit | 522a27aa163335a01fd709687d36165729a3f083 (patch) | |
tree | 03549f2da9901bb8c40cbee466805c1cdafdc6cd /utils/tinypcminfo.c | |
parent | 88a791f68b35ca0e4c7979d24cc9954422168dcd (diff) | |
parent | d86996deeef8aa6d6c15d2d4e2ce636b866324f9 (diff) |
Merge pull request #161 from E5ten/optparse
Add public domain header-only option-parsing library optparse
Diffstat (limited to 'utils/tinypcminfo.c')
-rw-r--r-- | utils/tinypcminfo.c | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/utils/tinypcminfo.c b/utils/tinypcminfo.c index 0dd381d..3116b7c 100644 --- a/utils/tinypcminfo.c +++ b/utils/tinypcminfo.c @@ -31,6 +31,9 @@ #include <stdlib.h> #include <string.h> +#define OPTPARSE_IMPLEMENTATION +#include "optparse.h" + #ifndef ARRAY_SIZE #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) #endif @@ -102,27 +105,32 @@ int main(int argc, char **argv) unsigned int device = 0; unsigned int card = 0; int i; - - if ((argc == 2) && (strcmp(argv[1], "--help") == 0)) { - fprintf(stderr, "Usage: %s -D card -d device\n", argv[0]); - return 1; - } - + struct optparse opts; + struct optparse_long long_options[] = { + { "help", 'h', OPTPARSE_NONE }, + { "card", 'D', OPTPARSE_REQUIRED }, + { "device", 'd', OPTPARSE_REQUIRED }, + { 0, 0, 0 } + }; + + (void)argc; /* silence -Wunused-parameter */ /* parse command line arguments */ - argv += 1; - while (*argv) { - if (strcmp(*argv, "-D") == 0) { - argv++; - if (*argv) - card = atoi(*argv); - } - if (strcmp(*argv, "-d") == 0) { - argv++; - if (*argv) - device = atoi(*argv); + optparse_init(&opts, argv); + while ((i = optparse_long(&opts, long_options, NULL)) != -1) { + switch (i) { + case 'D': + card = atoi(opts.optarg); + break; + case 'd': + device = atoi(opts.optarg); + break; + case 'h': + fprintf(stderr, "Usage: %s -D card -d device\n", argv[0]); + return 0; + case '?': + fprintf(stderr, "%s\n", opts.errmsg); + return EXIT_FAILURE; } - if (*argv) - argv++; } printf("Info for card %u, device %u:\n", card, device); |