是否有一种简单的方法可以检查选项的参数是否在一组预定义的选项中?这里的“简单”意味着没有定义特设类。
假设我选择--myoption
,其值必须为"myvalue1"
或"myvalue2"
例如在python中使用choices option in optparse
非常容易答案 0 :(得分:0)
正如我刚刚意识到的那样,您可以定义两个互斥的选项,只需定义一个小函数,如real.cpp中所述。例如,您可以指定两个定义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 + "'.");
}
}
然后调用
conflicting_options (vm, "quiet", "verbose");
在boost::program_options::store()
检查--myoption
是否等于myvalue1
或myvalue2
,这只是函数调用的问题。
我仍然不明白是否可以定义两个互斥的位置选项,但这应该是另一个问题。