2

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

例如,当我运行 hello 我使用如下

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

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

当我运行以下代码时,我得到了异常,因为--gtest_filter这不是我用于提升程序选项的选项。如何结合那些提升程序选项无法识别的选项来提供 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();
}
4

2 回答 2

2

InitGoogleTest删除Google Test 知道的选项并将其余的保留在argv. argc也会相应调整。只需将调用放在InitGoogleTest其他选项解析代码之前。

于 2011-03-24T22:12:11.363 回答
1

我在此页面中找到了一个忽略未知选项的选项 - 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);
于 2011-02-17T19:42:40.617 回答