* Parse an integer from an optarg. */
| 182 | * Parse an integer from an optarg. |
| 183 | */ |
| 184 | static intptr_t parseIntOptArg(const char *option, const char *optarg, |
| 185 | intptr_t lb, intptr_t ub, bool hex = false) |
| 186 | { |
| 187 | const char *optarg_0 = optarg; |
| 188 | bool neg = (optarg[0] == '-'); |
| 189 | if (neg) |
| 190 | optarg++; |
| 191 | int base = 10; |
| 192 | if (optarg[0] == '0' && optarg[1] == 'x') |
| 193 | base = 16; |
| 194 | errno = 0; |
| 195 | char *end = nullptr; |
| 196 | intptr_t r = (intptr_t)strtoul(optarg, &end, base); |
| 197 | r = (neg? -r: r); |
| 198 | if (errno != 0 || end == optarg || |
| 199 | (end != nullptr && *end != '\0') || r < lb || r > ub) |
| 200 | { |
| 201 | if (!hex) |
| 202 | error("failed to parse argument \"%s\" for the `%s' option; " |
| 203 | "expected a number within the range %zd..%zd", option, |
| 204 | optarg_0, lb, ub); |
| 205 | else |
| 206 | error("failed to parse argument \"%s\" for the `%s' option; " |
| 207 | "expected a number within the range %s0x%lx..%s0x%lx", |
| 208 | option, optarg_0, |
| 209 | (lb < 0? "-": ""), std::abs(lb), |
| 210 | (ub < 0? "-": ""), std::abs(ub)); |
| 211 | } |
| 212 | return r; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Parse a Boolean from an optarg. |
no test coverage detected