diff options
| author | Tyge Løvset <[email protected]> | 2020-03-30 20:55:41 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-30 20:55:41 +0200 |
| commit | fd2ac8139d488472b85eb0594dfcbfa0256a5272 (patch) | |
| tree | 7f61e7720da555298ec43dcea34225fb1d866612 | |
| parent | e14d91b1f48c1144b4af099f505d4c75311d5bac (diff) | |
| download | STC-modified-fd2ac8139d488472b85eb0594dfcbfa0256a5272.tar.gz STC-modified-fd2ac8139d488472b85eb0594dfcbfa0256a5272.zip | |
Add files via upload
| -rw-r--r-- | c_lib/cgetopt.h | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/c_lib/cgetopt.h b/c_lib/cgetopt.h index fb5e0847..53b613f3 100644 --- a/c_lib/cgetopt.h +++ b/c_lib/cgetopt.h @@ -154,21 +154,22 @@ static int copt_getopt(copt_t *opt, int argc, char *argv[], {"opt", copt_optional_argument, 'o'},
{NULL}
};
- copt_t opt = copt_init;
- int i = 0, c;
const char* optstr = "a:b::c";
- printf("optstr: %s\n", optstr);
+ printf("args -a arg -b [arg] -c --opt [arg] --foo --bar arg [arguments]\n");
+ int c;
+ copt_t opt = copt_init;
while ((c = copt_getopt(&opt, argc, argv, optstr, longopts, true)) != -1) {
if (c == '?')
- opt.opt != 0 ? printf("unknown option: %c\n", opt.opt)
- : printf("unknown option: %s\n", argv[opt.ind - 1]);
+ opt.longidx == -1 ? printf("unknown option: -%c\n", opt.opt)
+ : printf("unknown option: %s\n", argv[opt.ind - 1]);
else if (c == ':')
- printf("missing argument for %s\n", argv[opt.ind - 1]);
- else
+ opt.longidx == -1 ? printf("missing argument for -%c\n", opt.opt)
+ : printf("missing argument for %s\n", argv[opt.ind - 1]);
+ else
printf("option: %c %s\n", c, opt.arg ? opt.arg : "");
}
printf("\nNon-option arguments:");
- for (i = opt.ind; i < argc; ++i)
+ for (int i = opt.ind; i < argc; ++i)
printf(" %s", argv[i]);
putchar('\n');
return 0;
|
