I am trying to validate command line input to an Enum that I've defined, but get compiler errors. I have used Handle complex options with Boost's program_options as an example to work from.
namespace po = boost::program_options;
namespace Length
{
enum UnitType
{
METER,
INCH
};
}
void validate(boost::any& v, const std::vector<std::string>& values, Length::UnitType*, int)
{
Length::UnitType unit;
if (values.size() < 1)
{
throw boost::program_options::validation_error("A unit must be specified");
}
// make sure no previous assignment was made
//po::validators::check_first_occurence(v); // tried this but compiler said it couldn't find it
std::string input = values.at(0);
//const std::string& input = po::validators::get_single_string(values); // tried this but compiler said it couldn't find it
// I'm just trying one for now
if (input.compare("inch") == 0)
{
unit = Length::INCH;
}
v = boost::any(unit);
}
// int main(int argc, char *argv[]) not included
And to spare including more code than what is necessary, I'm adding the option as follows:
po::options_description config("Configuration");
config.add_options()
("to-unit", po::value<std::vector<Length::UnitType> >(), "The unit(s) of length to convert to")
;
If the compiler error is needed I can post it, but was hoping to keep the question simple looking. I've tried looking for examples, but the only other example I could really find was the examples/regex.cpp from the Boost website.
- Is the difference between my scenario and the examples found, except that mine is an Enum where the others are Structs? EDIT: My scenario did not require a custom validator overload.
- Is there a way to overload the validate method for an Enum? EDIT: Not needed.