Commit e8fd26824691d0a25b57930e68c126ffc74f791e

Authored by Henry Fredrick Schreiner
1 parent 05867beb

Adding enum support (basic only)

include/CLI/TypeTools.hpp
... ... @@ -74,8 +74,10 @@ constexpr const char *type_name() {
74 74  
75 75 // Lexical cast
76 76  
77   -/// Integers
78   -template <typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = detail::dummy>
  77 +/// Integers / enums
  78 +template <typename T, enable_if_t<std::is_integral<T>::value
  79 + || std::is_enum<T>::value
  80 + , detail::enabler> = detail::dummy>
79 81 bool lexical_cast(std::string input, T &output) {
80 82 try {
81 83 output = static_cast<T>(std::stoll(input));
... ... @@ -103,7 +105,9 @@ bool lexical_cast(std::string input, T &amp;output) {
103 105 /// String and similar
104 106 template <
105 107 typename T,
106   - enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value, detail::enabler> = detail::dummy>
  108 + enable_if_t<!std::is_floating_point<T>::value
  109 + && !std::is_integral<T>::value
  110 + && !std::is_enum<T>::value, detail::enabler> = detail::dummy>
107 111 bool lexical_cast(std::string input, T &output) {
108 112 output = input;
109 113 return true;
... ...
tests/AppTest.cpp
... ... @@ -203,6 +203,22 @@ TEST_F(TApp, DefaultOpts) {
203 203 EXPECT_EQ("9", s);
204 204 }
205 205  
  206 +TEST_F(TApp, EnumTest) {
  207 + enum Level : std::int32_t {
  208 + High,
  209 + Medium,
  210 + Low
  211 + };
  212 + Level level = Level::Low;
  213 + app.add_option("--level", level);
  214 +
  215 + args = {"--level", "1"};
  216 + run();
  217 + EXPECT_EQ(level, Level::Medium);
  218 +}
  219 +
  220 +// New style enums do not work, since << is not supported. Could be fixed without changing API by duplicating the `add_` methods with and without the extra flag.
  221 +
206 222 TEST_F(TApp, RequiredFlags) {
207 223 app.add_flag("-a")->required();
208 224 app.add_flag("-b")->mandatory(); // Alternate term
... ...