我从How to accept empty value in boost::program_options中得到了部分解决方案,它建议对可能存在或不存在参数的参数使用implicit_value 方法。所以我初始化“亮度”参数的调用如下所示:
("brightness,b", po::value<string>()->implicit_value(""),
然后我遍历变量映射,对于任何字符串参数,我检查它是否为空,如果是,我打印当前值。该代码如下所示:
// check if we're just printing a feature's current value
bool gotFeature = false;
for (po::variables_map::iterator iter = vm.begin(); iter != vm.end(); ++iter)
{
/// parameter has been given with no value
if (iter->second.value().type() == typeid(string))
if (iter->second.as<string>().empty())
{
gotFeature = true;
printFeatureValue(iter->first, camera);
}
}
// this is all we're supposed to do, time to exit
if (gotFeature)
{
cleanup(dc1394, camera, cameras);
return 0;
}
更新:这改变了上述语法,当使用隐式值时,现在参数,当给定时,必须采用以下形式:
./dc-ctl -b500
代替
./dc-ctl -b 500
希望这对其他人有帮助。