| 37 | } |
| 38 | |
| 39 | static int |
| 40 | argparse_getvalue(struct argparse *this_, const struct argparse_option *opt, |
| 41 | int flags) |
| 42 | { |
| 43 | const char *s = NULL; |
| 44 | if (!opt->value) |
| 45 | goto skipped; |
| 46 | switch (opt->type) { |
| 47 | case ARGPARSE_OPT_BOOLEAN: |
| 48 | if (flags & OPT_UNSET) { |
| 49 | *(int *)opt->value = *(int *)opt->value - 1; |
| 50 | } else { |
| 51 | *(int *)opt->value = *(int *)opt->value + 1; |
| 52 | } |
| 53 | if (*(int *)opt->value < 0) { |
| 54 | *(int *)opt->value = 0; |
| 55 | } |
| 56 | break; |
| 57 | case ARGPARSE_OPT_BIT: |
| 58 | if (flags & OPT_UNSET) { |
| 59 | *(int *)opt->value &= ~opt->data; |
| 60 | } else { |
| 61 | *(int *)opt->value |= opt->data; |
| 62 | } |
| 63 | break; |
| 64 | case ARGPARSE_OPT_STRING: |
| 65 | if (this_->optvalue) { |
| 66 | *(const char **)opt->value = this_->optvalue; |
| 67 | this_->optvalue = NULL; |
| 68 | } else if (this_->argc > 1) { |
| 69 | this_->argc--; |
| 70 | *(const char **)opt->value = *++this_->argv; |
| 71 | } else { |
| 72 | argparse_error(this_, opt, "requires a value"); |
| 73 | } |
| 74 | break; |
| 75 | case ARGPARSE_OPT_INTEGER: |
| 76 | if (this_->optvalue) { |
| 77 | *(int *)opt->value = strtol(this_->optvalue, (char **)&s, 0); |
| 78 | this_->optvalue = NULL; |
| 79 | } else if (this_->argc > 1) { |
| 80 | this_->argc--; |
| 81 | *(int *)opt->value = strtol(*++this_->argv, (char **)&s, 0); |
| 82 | } else { |
| 83 | argparse_error(this_, opt, "requires a value"); |
| 84 | } |
| 85 | if (*s) |
| 86 | argparse_error(this_, opt, "expects a numerical value"); |
| 87 | break; |
| 88 | default: |
| 89 | assert(0); |
| 90 | } |
| 91 | |
| 92 | skipped: |
| 93 | if (opt->callback) { |
| 94 | return opt->callback(this_, opt); |
| 95 | } |
| 96 |
no test coverage detected