title: Boost Program Options and validate function
body: <p>I am trying to write my own validate function for program boost options. However, I am getting:</p>
  
  <p>"
  terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
    what():  boost::bad_any_cast: failed conversion using boost::any_cast
  "</p>
  
  <p>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... </p>
  
  <p>In one of the header files I have:</p>
  
  <pre><code>enum class LogSeverityLevel : std::int8_t { E_LOG_TRACE = 0,
                                              E_LOG_DEBUG,
                                              E_LOG_INFO,
                                              E_LOG_WARN,
                                              E_LOG_ERROR};
  </code></pre>
  
  <p>And in my main.cpp file:</p>
  
  <pre><code>void validate(boost::any&amp; v,
            const std::vector&lt;std::string&gt;&amp; 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&amp; s = po::validators::get_single_string(values);
  
      const std::string&amp; 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);
      }
  }
  </code></pre>
  
  <p>and then, later on in main.cpp:</p>
  
  <pre><code>    desc.add_options()
      ("help,h", "print this help")
      ("version,v", "show application version")
      ("log-level", po::value&lt;std::string&gt;()-&gt;default_value("DEBUG"), "set log level")
      ;
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>    if (vm.count("log-level")) {
          setLogSeverityLevel(vm["log-level"].as&lt;LogSeverityLevel&gt;());
  </code></pre>
  
  <p>I will be thankful for any advice ;></p>