Skip to content

Commit b5be44c

Browse files
committed
Add better enum support in the library
1 parent 17d05b0 commit b5be44c

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

examples/enum.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#include <CLI/CLI.hpp>
22
#include <map>
3-
#include <sstream>
43

54
enum class Level : int { High, Medium, Low };
65

76
int main(int argc, char **argv) {
87
CLI::App app;
98

10-
std::map<std::string, Level> map = {{"High", Level::High}, {"Medium", Level::Medium}, {"Low", Level::Low}};
11-
129
Level level;
10+
std::map<std::string, Level> map = {{"High", Level::High}, {"Medium", Level::Medium}, {"Low", Level::Low}};
1311

1412
app.add_option("-l,--level", level, "Level settings")
1513
->required()

include/CLI/StringTools.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ inline std::vector<std::string> split(const std::string &s, char delim) {
4646
std::vector<std::string> elems;
4747
// Check to see if empty string, give consistent result
4848
if(s.empty())
49-
elems.emplace_back("");
49+
elems.emplace_back();
5050
else {
5151
std::stringstream ss;
5252
ss.str(s);
@@ -70,6 +70,21 @@ template <typename T> std::string join(const T &v, std::string delim = ",") {
7070
return s.str();
7171
}
7272

73+
/// Simple function to join a string from processed elements
74+
template <typename T,
75+
typename Callable,
76+
typename = typename std::enable_if<!std::is_constructible<std::string, Callable>::value>::type>
77+
std::string join(const T &v, Callable func, std::string delim = ",") {
78+
std::ostringstream s;
79+
size_t start = 0;
80+
for(const auto &i : v) {
81+
if(start++ > 0)
82+
s << delim;
83+
s << func(i);
84+
}
85+
return s.str();
86+
}
87+
7388
/// Join a string in reverse order
7489
template <typename T> std::string rjoin(const T &v, std::string delim = ",") {
7590
std::ostringstream s;

include/CLI/TypeTools.hpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Distributed under the 3-Clause BSD License. See accompanying
44
// file LICENSE or https://github.com/CLIUtils/CLI11 for details.
55

6+
#include "StringTools.hpp"
67
#include <exception>
78
#include <memory>
89
#include <string>
@@ -140,9 +141,16 @@ template <typename T, enable_if_t<is_vector<T>::value, detail::enabler> = detail
140141
constexpr const char *type_name() {
141142
return "VECTOR";
142143
}
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+
}
143149

150+
/// Print for all other types
144151
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,
146154
detail::enabler> = detail::dummy>
147155
constexpr const char *type_name() {
148156
return "TEXT";
@@ -229,10 +237,22 @@ bool lexical_cast(std::string input, T &output) {
229237
return true;
230238
}
231239

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+
232252
/// Non-string parsable
233253
template <typename T,
234254
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,
236256
detail::enabler> = detail::dummy>
237257
bool lexical_cast(std::string input, T &output) {
238258
std::istringstream is;

0 commit comments

Comments
 (0)