1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 05:33:53 +00:00

Adding enum support (basic only)

This commit is contained in:
Henry Fredrick Schreiner 2017-06-02 11:33:03 -04:00
parent 05867bebe5
commit e8fd268246
2 changed files with 23 additions and 3 deletions

View File

@ -74,8 +74,10 @@ constexpr const char *type_name() {
// Lexical cast
/// Integers
template <typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = detail::dummy>
/// Integers / enums
template <typename T, enable_if_t<std::is_integral<T>::value
|| std::is_enum<T>::value
, detail::enabler> = detail::dummy>
bool lexical_cast(std::string input, T &output) {
try {
output = static_cast<T>(std::stoll(input));
@ -103,7 +105,9 @@ bool lexical_cast(std::string input, T &output) {
/// String and similar
template <
typename T,
enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value, detail::enabler> = detail::dummy>
enable_if_t<!std::is_floating_point<T>::value
&& !std::is_integral<T>::value
&& !std::is_enum<T>::value, detail::enabler> = detail::dummy>
bool lexical_cast(std::string input, T &output) {
output = input;
return true;

View File

@ -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