c++ - boost::program_options 配置文件格式

标签 c++ boost delimiter file-format 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 来实现这一点? 我找到了命令行解析的样式选项,但似乎没有与 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;

}

关于c++ - boost::program_options 配置文件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51765993/

相关文章:

scala - 如何用spark-csv解析使用^A(即\001)作为分隔符的csv?

c++ - 在 C++ 中拆分字符串需要帮助

c++ - 在不平滑的情况下将图像大小增加 2 的幂

c++ - 在执行复制省略时,当移动构造函数被删除时,编译器不会在重载决策中考虑复制构造函数。为什么?

c++ - C++打印字节流

c++ - 如何在 OOP 程序中共享结构?

c++ - 奇怪的调用栈,会不会是asio使用openssl的问题?

c++ - boost.spirit x3 move_to 并列出 ast 成员

c++ - 'boost::operator ==' : 4个重载有相似的转换

c++ - 在 C++ 中将字节字符串拆分为 BYTES vector