这个问题已经在Boost parse_config_file, empty key value之前被问过了。但是,由于那里没有提供适当的解决方案,因此我再次要求希望有人可以提供更好的解决方案。
与上述问题不同,就我而言,代码未引发Boost错误:
boost::program_options::invalid_option_value
相反,对于
string options
,它将值设置为empty string("")
;对于bool
,将值设置为true
。我的代码的目的是确定是否在配置文件中设置了某些选项。我希望使用vm[optionName].count()
(其中vm是variables_map对象)进行测试,但是在未像option=
这样指定值的情况下,此方法返回true,因此无法使用。我还尝试了-
vm[optionName].defaulted()
,vm[optionName].empty()
,implicit_value()
和default_value()
,同时添加了选项,但是它们都不起作用。
最佳答案
更新最初的答案是错误的主意。这是更新。
因此,您希望foo=
(无值)的行为就像配置中的该行都不存在一样。
这意味着默认值语义(即通知时发生的事情-将状态从解析器组件迁移到存储组件)是不好的。
您可以通过发明自己的值(value)语义(可以说是mybool_switch)和/或在添加流操作的value<my_particulat_bool>
中解决问题来铺平道路,以使该选项按照您想要的方式运行。换句话说,使用佳能拍摄苍蝇。
但是,到目前为止,最简单的选择是在解析器阶段进行干预,在parsed_options
之前更改notify()
。
这是一个带有现场演示的完整示例:
Live On Coliru
#include <boost/program_options/config.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include <iomanip>
namespace po = boost::program_options;
int main() {
po::options_description desc;
desc.add_options()
("foo", po::bool_switch())
("bar", po::bool_switch()->default_value(false))
("qux", po::bool_switch()->implicit_value(false))
;
std::set<std::string> const bool_switches {"foo", "bar", "qux" };
for (std::string contents :
{ "", "foo=", "foo=true",
"bar=", "bar=true",
"qux=", "qux=true"})
{
std::istringstream iss(contents);
po::parsed_options parsed = po::parse_config_file(iss, desc, false);
std::cout << "\n---\n" << std::quoted(contents) << "\n";
// the magic is here:
for (auto it = parsed.options.begin(); it!= parsed.options.end();) {
using V = std::vector<std::string>;
V const& v = it->value;
if (bool_switches.count(it->string_key) && (v==V{} || v==V{""})) {
std::cout << "*** Discarding config key without a value: " << it->string_key << "\n";
it = parsed.options.erase(it);
} else {
++it;
}
}
po::variables_map vm;
po::store(parsed, vm);
for (auto& key : bool_switches) {
auto& entry = vm[key];
std::cout << " " << key << " ->" << std::boolalpha
<< (entry.empty()?" .empty()":"")
<< (entry.defaulted()?" .defaulted()":"");
if (entry.empty())
std::cout << " (no value)\n";
else
std::cout << " value:" << entry.as<bool>() << "\n";
}
}
}
哪个会打印
---
""
bar -> .defaulted() value:false
foo -> .defaulted() value:false
qux -> .defaulted() value:false
---
"foo="
*** Discarding config key without a value: foo
bar -> .defaulted() value:false
foo -> .defaulted() value:false
qux -> .defaulted() value:false
---
"foo=true"
bar -> .defaulted() value:false
foo -> value:true
qux -> .defaulted() value:false
---
"bar="
*** Discarding config key without a value: bar
bar -> .defaulted() value:false
foo -> .defaulted() value:false
qux -> .defaulted() value:false
---
"bar=true"
bar -> value:true
foo -> .defaulted() value:false
qux -> .defaulted() value:false
---
"qux="
*** Discarding config key without a value: qux
bar -> .defaulted() value:false
foo -> .defaulted() value:false
qux -> .defaulted() value:false
---
"qux=true"
bar -> .defaulted() value:false
foo -> .defaulted() value:false
qux -> value:true
关于c++ - 在Boost program_options中的配置文件中处理无值选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61735349/