boost::program_options config file option with multiple tokens

前端 未结 3 1431
迷失自我
迷失自我 2021-02-07 10:17

I can not seem to be able to read from config file multitoken options like I can from command line. What is the syntax for the config file?

This is how the option descri

3条回答
  •  青春惊慌失措
    2021-02-07 10:41

    During finding myself confronted with a similar problem, I took the code above from Rob's answer (from May 4th, 2011), but had to change a few things due to changes in the boost architecture and C++11. I only cite the parts that I changed (or would have changed). The rest that is not within the validate function stays the same. For conformity reasons, I added the necessary std:: prefixes.

    namespace po = boost::program_options;
    
    void validate(boost::any& v,
      const std::vector& values,
      coordinate*, int) {
      coordinate c;
      std::vector dvalues;
      for(const auto& val : values)  {
        std::stringstream ss(val);
        std::copy(std::istream_iterator(ss), std::istream_iterator(),
          std::back_inserter(dvalues));
        if(!ss.eof()) {
          throw po::invalid_option_value("Invalid coordinate specification");
        }
      }
      if(dvalues.size() != 2) {
        throw po::invalid_option_value("Invalid coordinate specification");
      }
      c.x = dvalues[0];
      c.y = dvalues[1];
      v = c;
    }
    

    The shift from po::validation_error to po::invalid_option_value was hinted in https://stackoverflow.com/a/12186109/4579106

提交回复
热议问题