diff --git a/include/CLI/TypeTools.hpp b/include/CLI/TypeTools.hpp index 475e2fa2..e43d1e2c 100644 --- a/include/CLI/TypeTools.hpp +++ b/include/CLI/TypeTools.hpp @@ -74,8 +74,10 @@ constexpr const char *type_name() { // Lexical cast -/// Integers -template ::value, detail::enabler> = detail::dummy> +/// Integers / enums +template ::value + || std::is_enum::value + , detail::enabler> = detail::dummy> bool lexical_cast(std::string input, T &output) { try { output = static_cast(std::stoll(input)); @@ -103,7 +105,9 @@ bool lexical_cast(std::string input, T &output) { /// String and similar template < typename T, - enable_if_t::value && !std::is_integral::value, detail::enabler> = detail::dummy> + enable_if_t::value + && !std::is_integral::value + && !std::is_enum::value, detail::enabler> = detail::dummy> bool lexical_cast(std::string input, T &output) { output = input; return true; diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index ca19f33a..8e3922a9 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -203,6 +203,22 @@ TEST_F(TApp, DefaultOpts) { EXPECT_EQ("9", s); } +TEST_F(TApp, EnumTest) { + enum Level : std::int32_t { + High, + Medium, + Low + }; + Level level = Level::Low; + app.add_option("--level", level); + + args = {"--level", "1"}; + run(); + EXPECT_EQ(level, Level::Medium); +} + +// 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. + TEST_F(TApp, RequiredFlags) { app.add_flag("-a")->required(); app.add_flag("-b")->mandatory(); // Alternate term