c++ - 使用 boost::program_options 解析配置文件

标签 c++ boost boost-program-options

你好,

我写了一个类来通过 boost::program_options 解析配置文件。这是我的(缩短):

namespace nsProOp = boost::program_options;
nsProOp::variables_map m_variableMap;
nsProOp::options_description m_description;



// To add options to the variableMap, e.g. "addOption<int>("money_amount");"
template <class T>
    void addOption(const std::string& option, const std::string& helpDescription = "") {
        m_description.add_options()(option.c_str(), nsProOp::value<T > (), helpDescription.c_str());
    }



// And this is how i actually read the file:
void ConfigFile::parse() {
    std::ifstream file;
    file.open(m_pathToFile.c_str());

    nsProOp::store(nsProOp::parse_config_file(file, m_description, true), m_variableMap);
    nsProOp::notify(m_variableMap);      
}

好的,这很好用。但我希望能够再次解析同一个文件,以便我始终使用用户提供的最新条目! boost 文档说的是“store”:

"Stores in 'm' all options that are defined in 'options'. If 'm' already has a non-defaulted value of an option, that value is not changed, even if 'options' specify some value."

因此,如果我再次调用“parse()”,什么也不会发生,因为 m_variableMap 已填充。我尝试调用 m_variableMap.clear() 并没有解决我的问题,因此存储只在第一次起作用。

有人对我有什么建议吗?如果我的问题不清楚,请告诉我。谢谢!

最佳答案

至少在 boost 1.50 中,variables_map::clear() 将允许通过 store 正确地重新填充变量映射。至少可以追溯到 boost 1.37 的替代解决方案是在调用 store 之前将默认构造的变量映射分配给变量映射。

void ConfigFile::parse() {
  std::ifstream file;
  file.open(m_pathToFile.c_str());
  m_variableMap = nsProOp::variables_map(); // Clear m_variableMap.
  nsProOp::store(nsProOp::parse_config_file(file, m_description, true), 
                 m_variableMap);
  nsProOp::notify(m_variableMap);      
}

这是一个示例程序:

#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <string>

namespace po = boost::program_options;

void write_settings(const char* value)
{
  std::ofstream settings_file("settings.ini");
  settings_file << "name = " << value;
}

void read_settings(po::options_description& desc,
                   po::variables_map& vm)
{
  std::ifstream settings_file("settings.ini");

  // Clear the map.
  vm = po::variables_map();

  po::store(po::parse_config_file(settings_file , desc), vm);
  po::notify(vm);    
}

int main()
{
  std::string name;

  // Setup options.
  po::options_description desc("Options");
  desc.add_options()
    ("name", po::value<std::string>(&name), "name");
  po::variables_map vm;

  // Write, read, and print settings.
  write_settings("test");
  read_settings(desc, vm);
  std::cout << "name = " << name << std::endl;

  // Write, read, and print newer settings.
  write_settings("another test");
  read_settings(desc, vm);
  std::cout << "name = " << name << std::endl;
}

产生以下输出:

name = test
name = another test

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

相关文章:

c++ - 搜索/替换 boost 正则表达式 C++

c++ - 在命名空间的类中使用带有枚举的 boost::program_options

c++ - 如何仅在所有任务完成后才使用c++中的boost向线程池添加新任务

c++ - 如何从 C++ 代码运行自定义 GPU tensorflow::op?

c++ - 测试数字是否可以被 4 整除,最多 100 位

python - 安装 dlib python 模块出现段错误

c++ - 如何包含 VS 2008 的 Boost 库(命令行)

c++ - 使用 '--' 作为带有 boost::program_options 的选项结束标记

c++ - 使用 Boost.Program_options 的输入流运算符查找顺序

c++ - 如何在 C++ 中将十六进制转换为十进制字符串?