c++ - boost::program_options 忽略配置文件中的选项

标签 c++ boost-program-options

您能帮我解决boost::program_options吗?

我希望解析器忽略保存在配置文件中的未知选项。 我知道 allow_unregistered() 可用于 cmd 行选项,我该如何处理文本文件?

这里是剥离的代码:

namespace po = boost::program_options;
try {
    string config_file;
    string gps_source;
    int op_baud;

    po::options_description generic("Generic options");
    generic.add_options()

        ("ssdvpacksize", po::value<int>(),
              "ssdv packets size in bytes")
        ("ssdvdir", po::value<string>()->default_value("/ARY1/ssdv"),
              "ssdv image dir")

        //unused
        //I have to specify these even if they're unused
        ("ssdvproc_dir", po::value<string>(), "")
        ;

    po::options_description file_options;
    file_options.add(generic);

    po::options_description cli_options("command line interface options");
    cli_options.add(generic);
    cli_options.add_options()
        ("config", po::value<string>(&config_file)->default_value("/boot/ary-1.cfg"), "name of a file of a configuration.");


    po::variables_map vm;
    store( po::command_line_parser(ac, av).options(cli_options).allow_unregistered().run(), vm );
    //store( po::basic_command_line_parser<char>(ac, av).options(cli_options).allow_unregistered().run(), vm );

    notify(vm);

    ifstream ifs(config_file.c_str());
    if (!ifs)
    {
        cout << "Can not open config file: " << config_file << "\n";
    }
    else
    {
        // probably smth. to do here ?
        //store(parse_config_file(ifs, file_options).allow_unregistered(), vm); // does not work
        store(parse_config_file(ifs, file_options), vm);
        notify(vm);
    }

    // ...
    // rest of program

}

最佳答案

好吧,解决方案非常简单。 第 44 行应该是:

store(parse_config_file(ifs, file_options, true/*allow unregistered*/), vm);

关于c++ - boost::program_options 忽略配置文件中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892097/

相关文章:

c++ - Boost.Program_options 在 Clang 下没有正确链接

c++ - Boost parse_config_file,空键值

c++ - Boost Program_options 配置文件注释

c++ - 根据对父类(super class)的引用确定对象标识

c++ - C++ 中的参数查找

c++ - 使用 boost program_options 处理帮助消息,删除默认值或重新格式化帮助消息

c++ - boost program_options 是否支持关闭开关(例如 --no-myswitch 或 -s=no)

c++ - 为什么我的 AMD CPU 在编译应用程序时遇到问题?

c++ - 如何在 qt creator 中使用 clang 5 和 qt?

c++ - 视差图opencv