I am trying to write my own validate function for program boost options. However, I am getting:
" terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): boost::bad_any_cast: failed conversion using boost::any_cast "
I saw similar posts even here on stackoverflow, but I cannot get that working... below is my code. Hmm I suppose, that somehow I have to use lexical_cast and write this casting by myself, but I failed...
In one of the header files I have:
enum class LogSeverityLevel : std::int8_t { E_LOG_TRACE = 0,
E_LOG_DEBUG,
E_LOG_INFO,
E_LOG_WARN,
E_LOG_ERROR};
And in my main.cpp file:
void validate(boost::any& v,
const std::vector<std::string>& values,
LogSeverityLevel*, int)
{
// Make sure no previous assignment to 'a' was made.
po::validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string& s = po::validators::get_single_string(values);
const std::string& s_capitalized = boost::to_upper_copy(s);
if (s_capitalized== "ERROR") {
v = boost::any(LogSeverityLevel::E_LOG_ERROR);
} else if (s_capitalized == "WARN") {
v = boost::any(LogSeverityLevel::E_LOG_WARN);
} else if (s_capitalized == "INFO") {
v = boost::any(LogSeverityLevel::E_LOG_INFO);
} else if (s_capitalized == "DEBUG") {
v = boost::any(LogSeverityLevel::E_LOG_DEBUG);
} else if (s_capitalized == "TRACE") {
v = boost::any(LogSeverityLevel::E_LOG_TRACE);
} else {
throw po::validation_error(po::validation_error::invalid_option_value);
}
}
and then, later on in main.cpp:
desc.add_options()
("help,h", "print this help")
("version,v", "show application version")
("log-level", po::value<std::string>()->default_value("DEBUG"), "set log level")
;
and then:
if (vm.count("log-level")) {
setLogSeverityLevel(vm["log-level"].as<LogSeverityLevel>());
I will be thankful for any advice ;>