Boost logo

Boost Users :

Subject: [Boost-users] [program_options] Custom validate for asio endpoints
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2009-10-28 19:11:46


I'm trying to validate an ip address and port that is being passed in
through the command line arguments. I could retrieve the string and do
the validation outside program options. I thought it would be better to
use the custom validate function shown in the documentation. I'm trying
to retrieve the information as a boost::asio::ip::udp::endpoint. Even
after creating the validate function the compiler is complaining that
there is no user defined function or stream operator. I've include some
code to show what I'm doing. Any help would be great.

Ryan

class CommandLine {

  static void validate(boost::any & v, std::vector<std::string> const&
values) {
    using namespace boost::program_options;

    // Make sure no previous assignment to 'v' was made.
    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.
    std::string const& s = validators::get_single_string(values);

    // Do regex match and convert the interesting part to int.
    if (validIPandPort(s)) {
      v = boost::any(createEndpoint(s));
    }
    else {
      throw validation_error("invalid value");
    }
  }

  static ApplicationArguments process(const int argc, char * argv[]) {
    ApplicationArguments returnApplicationArguments;

    //Create an options menu with all parameters allowed, to be passed
in through the command line arguments.
    boost::program_options::options_description menu("Usage: program.exe
-a ip:port,ip:port -r ip:port,ip:port\nOptions:");
    menu.add_options()
        ("ip1,a" ,
boost::program_options::value<boost::asio::ip::udp::endpoint>() ,
"description")
        ("ip2,r" ,
boost::program_options::value<boost::asio::ip::udp::endpoint>() ,
"description")
    ;

    //Process the command line arguments and place them in a map.
    boost::program_options::variables_map vm;
    
boost::program_options::store(boost::program_options::parse_command_line(argc,
argv, menu), vm);
    boost::program_options::notify(vm);

    return returnApplicationArguments;
  }
};

static void validate(boost::any & v, std::vector<std::string> const&
values, boost::asio::ip::udp::endpoint * target_type, int) {
  CommandLine::validate(v, values);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net