]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
argconfig: Increase the flag value
authorDaniel Wagner <dwagner@suse.de>
Thu, 10 Feb 2022 16:57:01 +0000 (17:57 +0100)
committerDaniel Wagner <dwagner@suse.de>
Thu, 10 Feb 2022 17:36:27 +0000 (18:36 +0100)
If the flag is provided several time increase the flag value. This is
a pretty ugly fix for how getopt_long_only() handles '--verbose' vs
'-v'. getopt_long_only() returns 0 when --verbose is used and 'v' for
-v hence we will not usually not take the short option path for
--verbose.

Break out in the short option parsing when non flag options are used.

Another uglyliness here is that it sets up long options differently
depending on short or long version is used. For the short version we
have to use value_addr.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
util/argconfig.c

index 27d5e94fc830df110b0b15e0e0547b1339d939f8..de6e8bec35f29cbd49849930fc4e8b19234c480d 100644 (file)
@@ -219,7 +219,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
                        }
                        if (option_index == options_count)
                                continue;
-                       if (long_opts[option_index].flag) {
+                       if (long_opts[option_index].flag &&
+                           options[option_index].config_type == CFG_NONE) {
                                *(uint8_t *)(long_opts[option_index].flag) = 1;
                                continue;
                        }
@@ -292,7 +293,10 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
                         *
                         * So we need to increase 'val', not 'value_addr'.
                         */
-                       long_opts[option_index].val++;
+                       if (!c)
+                               long_opts[option_index].val++;
+                       else
+                               *((int *)value_addr) += 1;
                } else if (s->config_type == CFG_LONG) {
                        *((unsigned long *)value_addr) = strtoul(optarg, &endptr, 0);
                        if (errno || optarg == endptr) {