]> git.kaiwu.me - klib.git/commitdiff
remember longidx
authorHeng Li <lh3@me.com>
Thu, 30 Aug 2018 06:50:53 +0000 (20:50 -1000)
committerHeng Li <lh3@me.com>
Thu, 30 Aug 2018 06:50:53 +0000 (20:50 -1000)
ketopt.c
ketopt.h

index aa6685c0f39af55e2d26b43b2222a1f08f16fa34..55d086c2e6573d73d9193a73012859d16cc2c8db 100644 (file)
--- a/ketopt.c
+++ b/ketopt.c
@@ -20,7 +20,7 @@ int ketopt(ketopt_t *s, int argc, char *argv[], int permute, const char *ostr, c
                while (s->i < argc && (argv[s->i][0] != '-' || argv[s->i][1] == '\0'))
                        ++s->i, ++s->n_args;
        }
-       s->arg = 0, i0 = s->i;
+       s->arg = 0, s->longidx = -1, i0 = s->i;
        if (s->i >= argc || argv[s->i][0] != '-' || argv[s->i][1] == '\0') {
                s->ind = s->i - s->n_args;
                return -1;
@@ -40,7 +40,7 @@ int ketopt(ketopt_t *s, int argc, char *argv[], int permute, const char *ostr, c
                                if (strncmp(&argv[s->i][2], longopts[k].name, j - 2) == 0)
                                        ++n_matches, o = &longopts[k];
                        if (n_matches == 1) {
-                               s->opt = opt = o->val;
+                               s->opt = opt = o->val, s->longidx = o - longopts;
                                if (argv[s->i][j] == '=') s->arg = &argv[s->i][j + 1];
                                if (o->has_arg == 1 && argv[s->i][j] == '\0') {
                                        if (s->i < argc - 1) s->arg = argv[++s->i];
index 786e17e632843909427906f0c9b0caf204f7a306..eb8b60b1bed2699a92d623588b270f7bcd9212e6 100644 (file)
--- a/ketopt.h
+++ b/ketopt.h
@@ -9,6 +9,7 @@ typedef struct {
        int ind;   /* equivalent to optind */
        int opt;   /* equivalent to optopt */
        char *arg; /* equivalent to optarg */
+       int longidx; /* index of a long option; or -1 if short */
        /* private variables not intended for external uses */
        int i, pos, n_args;
 } ketopt_t;