c++ - boost::program_options 配置文件格式
问题描述
我正在使用 boost::program_options 加载命令行参数。现在我想另外加载一个具有相同参数集的配置文件。我以非常标准的方式使用它:
ifstream ifs(filename.c_str());
if (ifs) {
po::store(po::parse_config_file(ifs, optionsDescription), vm);
po::notify(vm);
}
问题是 parse_config_file 接受以下标准格式的 ini 文件:
key1=value
key2 = value
但是我的文件不使用“等号”来分隔键和值,而只使用空格和/或制表符,如下所示:
key1 value
key2 value
出于兼容性原因,我需要保留这种格式。有没有办法通过 boost program_options 实现这一点?我找到了 command_line parses 的样式选项,但 parse_config_file 似乎没有这样的选项。
解决方案
根据source code, boost 似乎明确地寻找=
符号。
所以没有直接的方法来处理你的文件格式。您可能必须更改 boost 源或将文件加载到内存并将值作为命令行输入处理。
else if ((n = s.find('=')) != string::npos) {
string name = m_prefix + trim_ws(s.substr(0, n));
string value = trim_ws(s.substr(n+1));
bool registered = allowed_option(name);
if (!registered && !m_allow_unregistered)
boost::throw_exception(unknown_option(name));
found = true;
this->value().string_key = name;
this->value().value.clear();
this->value().value.push_back(value);
this->value().unregistered = !registered;
this->value().original_tokens.clear();
this->value().original_tokens.push_back(name);
this->value().original_tokens.push_back(value);
break;
}
推荐阅读
- android - onScrolled 在没有用户滚动的情况下被调用(Recycler View 无限滚动)
- tensorflow - ND张量矩阵乘法与张量流
- yii2 - Yii2 使用 UrlManager::createUrl() 将语言添加到 URL
- sql - where 子句中的星期几和时间
- php - 如何使用 PHP curl 从 ZOHO 图书 API 获取 JSON 数据?
- apache - Apache 不能在 localhost 以外的网站上运行
- php - 输入提交按钮的可见性设置
- c# - 实现 Equals 以使引用相等或基于键的相等?
- macos - 无法从 Mac 将程序上传到 Arduino Leonardo
- python - 如何让变量在 tkinter 中实际显示?