Choosing Options

Is there an easy way to check if a parameter argument is inside a set of predefined options? “Simple” here means without defining an ad-hoc class.

Suppose I have an option --myoptionthat should have a value "myvalue1"or"myvalue2"

For example, in python it’s very easy to use the optparse selection option

+5
source share
1 answer

As I just understood, you can define two mutually exclusive parameters by simply defining a small function, as described in real.cpp . For example, you can specify two conflicting parameters defining a function conflicting_options():

void conflicting_options(const boost::program_options::variables_map & vm,
                         const std::string & opt1, const std::string & opt2)
{
    if (vm.count(opt1) && !vm[opt1].defaulted() &&
        vm.count(opt2) && !vm[opt2].defaulted())
    {
        throw std::logic_error(std::string("Conflicting options '") +
                               opt1 + "' and '" + opt2 + "'.");
    }
}

and then call

conflicting_options (vm, "quiet", "verbose");

right after boost::program_options::store()

, --myoption myvalue1 myvalue2, .

, , .

0

All Articles