3 #include "nlopt-getopt.h"
5 int opterr = 1, /* if error message should be printed */
6 optind = 1, /* index into parent argv vector */
7 optopt, /* character checked for validity */
8 optreset; /* reset getopt */
9 char *optarg; /* argument associated with option */
11 #define BADCH (int)'?'
12 #define BADARG (int)':'
17 * Parse argc/argv argument vector.
19 int getopt(int nargc, char * const nargv[], const char *ostr)
21 static char *place = EMSG; /* option letter processing */
22 const char *oli; /* option letter list index */
23 if (optreset || !*place) { /* update scanning pointer */
25 if (optind >= nargc || *(place = nargv[optind]) != '-') {
29 if (place[1] && *++place == '-') { /* found "--" */
34 } /* option letter okay? */
35 if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr, optopt))) {
36 /* if the user didn't specify '-' as an option, assume it means -1. */
37 if (optopt == (int)'-')
41 if (opterr && *ostr != ':')
42 (void) printf("illegal option -- %c\n", optopt);
45 if (*++oli != ':') { /* don't need argument */
50 else { /* need an argument */
51 if (*place) /* no white space */
53 else if (nargc <= ++optind) { /* no arg */
58 (void) printf("option requires an argument -- %c\n", optopt);
61 else /* white space */
62 optarg = nargv[optind];
66 return (optopt); /* dump back option letter */