opt.c (813B)
1 #include <assert.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include "cext/cext.h" 7 #include "cext/opt.h" 8 9 int 10 main(int argc, char **argv) { 11 opt_ctx opt; 12 13 opt_init(&opt, &argc, argv); 14 while (opt_parse(&opt)) { 15 if (opt.s == 'a') { 16 assert(opt.avail); 17 printf("short a: %s\n", opt_arg(&opt)); 18 } else if (opt.s == 'b') { 19 printf("short b\n"); 20 } else if (!strcmp(opt.l, "xxx")) { 21 assert(opt.avail); 22 printf("long xxx: %s\n", opt_arg(&opt)); 23 } else if (!strcmp(opt.l, "yyy")) { 24 assert(!opt.attached); 25 printf("long yyy\n"); 26 } else { 27 if (opt.s) 28 printf("unknown short opt: %c\n", opt.s); 29 else 30 printf("unknown long opt: %s\n", opt.l); 31 exit(1); 32 } 33 } 34 35 printf("\n%d arguments:\n", argc-1); 36 for (size_t i = 1; i < argc; i++) 37 printf("\t%s\n", argv[i]); 38 }