【问题标题】:boost::program_options ignore options in config fileboost::program_options 忽略配置文件中的选项
【发布时间】:2015-08-08 10:24:26
【问题描述】:

你能帮我处理一下boost::program_options吗?

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

这里是剥离代码:

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

}

【问题讨论】:

    标签: c++ boost-program-options


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2020-03-02
      相关资源
      最近更新 更多