【问题标题】:Integrating 'google test' and 'boost program options'集成“谷歌测试”和“增强程序选项”
【发布时间】:2011-02-17 19:01:01
【问题描述】:

我有一个使用 google test 的程序,以及用于解析选项的 boost 程序选项库。 问题是谷歌测试也有它自己的选项解析器,所以我需要在将选项提供给谷歌测试之前过滤掉。

例如,当我运行你好时,我使用如下

hello --option1=X --gtest_filter=Footest.*

--option1 是我在将 --gtest_filter 选项传递给 google 测试之前使用的选项。

当我运行以下代码时,我得到了异常,因为 --gtest_filter 不是我用于提升程序选项的选项。如何组合那些 boost 程序选项无法识别的选项来提供 gtest 的输入?

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;

#include <gtest/gtest.h>   

int main(int argc, char **argv) {
    // filter out the options so that only the google related options survive
    try {
        int opt;
        string config_file;

        po::options_description generic("Generic options");
        generic.add_options()
            ("option1,o", "print version string")
            ;

            ...           
    }
    catch(exception& e) // *****************
    {
        cout << e.what() << "\n";
        return 1;
    } 

    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

【问题讨论】:

    标签: c++ boost boost-program-options googletest


    【解决方案1】:

    InitGoogleTestremove Google Test 知道的选项,其余的留在 argvargc 也会相应调整。只需在其他选项解析代码之前调用InitGoogleTest

    【讨论】:

    • 这是正确答案,应该被接受为最佳答案。
    【解决方案2】:

    我在此页面中找到了忽略未知选项的选项 - http://www.boost.org/doc/libs/1_45_0/doc/html/program_options/howto.html#id2075428

    store(po::command_line_parser(argc, argv).
             options(cmdline_options).positional(p).allow_unregistered().run(), vm);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-11
      • 2013-07-13
      • 2012-05-29
      • 2014-02-21
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多