| 98 | } |
| 99 | |
| 100 | int Option::validate(const Option::value_t &new_value, std::string *err) const |
| 101 | { |
| 102 | // Generic validation: min |
| 103 | if (min != value_t{}) { |
| 104 | if (new_value < min) { |
| 105 | std::ostringstream oss; |
| 106 | oss << "Value '" << new_value << "' is below minimum " << min; |
| 107 | *err = oss.str(); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Generic validation: max |
| 113 | if (max != value_t{}) { |
| 114 | if (new_value > max) { |
| 115 | std::ostringstream oss; |
| 116 | oss << "Value '" << new_value << "' exceeds maximum " << max; |
| 117 | *err = oss.str(); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Generic validation: enum |
| 123 | if (!enum_allowed.empty() && type == Option::TYPE_STR) { |
| 124 | auto found = std::find(enum_allowed.begin(), enum_allowed.end(), |
| 125 | std::get<std::string>(new_value)); |
| 126 | if (found == enum_allowed.end()) { |
| 127 | std::ostringstream oss; |
| 128 | oss << "'" << new_value << "' is not one of the permitted " |
| 129 | "values: " << joinify(enum_allowed.begin(), |
| 130 | enum_allowed.end(), |
| 131 | std::string(", ")); |
| 132 | *err = oss.str(); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int Option::parse_value( |
| 141 | const std::string& raw_val, |