| 47 | } |
| 48 | |
| 49 | static int |
| 50 | argparse_getvalue(struct argparse *self, const struct argparse_option *opt, |
| 51 | int flags) |
| 52 | { |
| 53 | const char *s = NULL; |
| 54 | if (!opt->value) |
| 55 | goto skipped; |
| 56 | switch (opt->type) { |
| 57 | case ARGPARSE_OPT_BOOLEAN: |
| 58 | if (flags & OPT_UNSET) { |
| 59 | *(int *)opt->value = *(int *)opt->value - 1; |
| 60 | } else { |
| 61 | *(int *)opt->value = *(int *)opt->value + 1; |
| 62 | } |
| 63 | if (*(int *)opt->value < 0) { |
| 64 | *(int *)opt->value = 0; |
| 65 | } |
| 66 | break; |
| 67 | case ARGPARSE_OPT_BIT: |
| 68 | if (flags & OPT_UNSET) { |
| 69 | *(int *)opt->value &= ~opt->data; |
| 70 | } else { |
| 71 | *(int *)opt->value |= opt->data; |
| 72 | } |
| 73 | break; |
| 74 | case ARGPARSE_OPT_STRING: |
| 75 | if (self->optvalue) { |
| 76 | *(const char **)opt->value = self->optvalue; |
| 77 | self->optvalue = NULL; |
| 78 | } else if (self->argc > 1) { |
| 79 | self->argc--; |
| 80 | *(const char **)opt->value = *++self->argv; |
| 81 | } else { |
| 82 | argparse_error(self, opt, "requires a value", flags); |
| 83 | } |
| 84 | break; |
| 85 | case ARGPARSE_OPT_INTEGER: |
| 86 | errno = 0; |
| 87 | if (self->optvalue) { |
| 88 | *(int *)opt->value = strtol(self->optvalue, (char **)&s, 0); |
| 89 | self->optvalue = NULL; |
| 90 | } else if (self->argc > 1) { |
| 91 | self->argc--; |
| 92 | *(int *)opt->value = strtol(*++self->argv, (char **)&s, 0); |
| 93 | } else { |
| 94 | argparse_error(self, opt, "requires a value", flags); |
| 95 | } |
| 96 | if (errno == ERANGE) |
| 97 | argparse_error(self, opt, "numerical result out of range", flags); |
| 98 | if (s[0] != '\0') // no digits or contains invalid characters |
| 99 | argparse_error(self, opt, "expects an integer value", flags); |
| 100 | break; |
| 101 | case ARGPARSE_OPT_FLOAT: |
| 102 | errno = 0; |
| 103 | if (self->optvalue) { |
| 104 | *(float *)opt->value = strtof(self->optvalue, (char **)&s); |
| 105 | self->optvalue = NULL; |
| 106 | } else if (self->argc > 1) { |
no test coverage detected