|
3 | 3 | // Distributed under the 3-Clause BSD License. See accompanying
|
4 | 4 | // file LICENSE or https://github.com/CLIUtils/CLI11 for details.
|
5 | 5 |
|
| 6 | +#include "StringTools.hpp" |
6 | 7 | #include <exception>
|
7 | 8 | #include <memory>
|
8 | 9 | #include <string>
|
@@ -140,9 +141,16 @@ template <typename T, enable_if_t<is_vector<T>::value, detail::enabler> = detail
|
140 | 141 | constexpr const char *type_name() {
|
141 | 142 | return "VECTOR";
|
142 | 143 | }
|
| 144 | +/// Print name for enumeration types |
| 145 | +template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy> |
| 146 | +constexpr const char *type_name() { |
| 147 | + return "ENUM"; |
| 148 | +} |
143 | 149 |
|
| 150 | +/// Print for all other types |
144 | 151 | template <typename T,
|
145 |
| - enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value && !is_vector<T>::value, |
| 152 | + enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value && !is_vector<T>::value && |
| 153 | + !std::is_enum<T>::value, |
146 | 154 | detail::enabler> = detail::dummy>
|
147 | 155 | constexpr const char *type_name() {
|
148 | 156 | return "TEXT";
|
@@ -229,10 +237,22 @@ bool lexical_cast(std::string input, T &output) {
|
229 | 237 | return true;
|
230 | 238 | }
|
231 | 239 |
|
| 240 | +/// enumerations |
| 241 | +template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy> |
| 242 | +bool lexical_cast(std::string input, T &output) { |
| 243 | + typename std::underlying_type<T>::type val; |
| 244 | + bool retval = detail::lexical_cast(input, val); |
| 245 | + if(!retval) { |
| 246 | + return false; |
| 247 | + } |
| 248 | + output = static_cast<T>(val); |
| 249 | + return true; |
| 250 | +} |
| 251 | + |
232 | 252 | /// Non-string parsable
|
233 | 253 | template <typename T,
|
234 | 254 | enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
|
235 |
| - !std::is_assignable<T &, std::string>::value, |
| 255 | + !std::is_assignable<T &, std::string>::value && !std::is_enum<T>::value, |
236 | 256 | detail::enabler> = detail::dummy>
|
237 | 257 | bool lexical_cast(std::string input, T &output) {
|
238 | 258 | std::istringstream is;
|
|
0 commit comments