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

时间:2015-08-08 10:24:27

标签: 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

}

1 个答案:

答案 0 :(得分:0)

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

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