Boost Custom Validator for Enum
Asked Answered
N

1

25

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.

  1. 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.
  2. Is there a way to overload the validate method for an Enum? EDIT: Not needed.
Newsboy answered 6/3, 2011 at 17:9 Comment(3)
The compiler error would be convenient to know.Wasteland
@netrom Emile Cormier's answer was just what I was looking for. Had I known what I was looking for in the compiler error output, it was saying that it was looking for the overloaded operator>> function.Newsboy
please shorten and format the source.Kew
U
35

In your case, you simply need to overload operator>> to extract a Length::Unit from an istream, as shown here:

#include <iostream>
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>

namespace Length
{

enum Unit {METER, INCH};

std::istream& operator>>(std::istream& in, Length::Unit& unit)
{
    std::string token;
    in >> token;
    if (token == "inch")
        unit = Length::INCH;
    else if (token == "meter")
        unit = Length::METER;
    else 
        in.setstate(std::ios_base::failbit);
    return in;
}

};

typedef std::vector<Length::Unit> UnitList;

int main(int argc, char* argv[])
{
    UnitList units;

    namespace po = boost::program_options;
    po::options_description options("Program options");
    options.add_options()
        ("to-unit",
             po::value<UnitList>(&units)->multitoken(),
             "The unit(s) of length to convert to")
        ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, options), vm);
    po::notify(vm);

    BOOST_FOREACH(Length::Unit unit, units)
    {
        std::cout << unit << " ";
    }
    std::cout << "\n";

    return 0;
}

A custom validator is not necessary.

Unmade answered 6/3, 2011 at 18:43 Comment(3)
Hi, would you explain why in the regex example, a parser is not required? Thanks.Babul
@CandyChiu: I haven't used used Boost.ProgramOptions in ages and forgot how the advanced stuff works. I don't have time to dig through the documentation to answer your comment. You should post your query as a new question; I'm sure there will be some willing to help.Unmade
Marking up, but worked for me only after moving the >> overload into the namespace where the enum was defined (in this case, that would be namespace Length). Otherwise, getting "error: static assertion failed: Target type is neither std::istreamable nor std::wistreamable" at compile time. Solution found here: https://mcmap.net/q/539237/-boost-lexical_cast-not-recognizing-overloaded-istream-operatorAllegorical

© 2022 - 2024 — McMap. All rights reserved.