Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:56:59+00:00 2026-05-20T09:56:59+00:00

I am trying to validate command line input to an Enum that I’ve defined,

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:57:00+00:00Added an answer on May 20, 2026 at 9:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to validate my site, I get the following error: Line 188, column
I am trying to validate the form input but my validation is not working.
I'm trying validate an X.509 certificate received over a network. I have understood that
I'm trying to validate a textarea making sure that each line starts with 'http://':
Im trying to validate my XML file with an external DTD. But I get
While trying to validate my forms i get the following error: Expected a {
While trying to validate form data on my page i get the following error:
I'm trying to validate an ID. I have this class called ManejadorTickets in which
I am trying to get going with Djangoappengine . I followed the instructions, but
I am currently trying to validate that an Oracle db_link is actually pointing to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.