| 84 | namespace console |
| 85 | { |
| 86 | template <class T, class V = T(*)(const char*, const char**)> int |
| 87 | parse_generic (V convert_func, int argc, const char* const* argv, const char* str, T& val) |
| 88 | { |
| 89 | char *endptr = nullptr; |
| 90 | int index = find_argument (argc, argv, str) + 1; |
| 91 | errno = 0; |
| 92 | |
| 93 | if (index > 0 && index < argc ) |
| 94 | { |
| 95 | val = convert_func (argv[index], &endptr); // similar to strtol, strtod, strtof |
| 96 | // handle out-of-range, junk at the end and no conversion |
| 97 | if (errno == ERANGE || *endptr != '\0' || str == endptr) |
| 98 | { |
| 99 | return -1; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return (index - 1); |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | parse_argument (int argc, const char * const * argv, const char * str, long int &val) noexcept |
no outgoing calls
no test coverage detected