集成“谷歌测试”;和“增强程序选项”
我有一个程序,使用谷歌测试和增强程序选项库来解析选项。 问题是谷歌测试也有它自己的选项解析器,所以我需要在将选项提供给谷歌测试之前过滤掉。
例如,当我运行 hello 时,我使用如下
hello --option1=X --gtest_filter=Footest.*
--option1 是我在将 --gtest_filter 选项传递给 google 测试之前使用的选项。
当我运行以下代码时,出现异常,因为 --gtest_filter
不是我用于 boost 程序选项的选项。如何组合那些 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();
}
I have a program that uses google test, and boost program options library for parsing options.
The problem is that google test also has it's own option parsers, so I need to filter out before giving the options to the google test.
For example, When I run hello I use as follows
hello --option1=X --gtest_filter=Footest.*
--option1 is the option that I use before passing the --gtest_filter option to the google test.
When I run the following code, I got exception as --gtest_filter
is not the option that I use for boost program options. How can I combine those options that boost program options doesn't recognize to give gtest's input?
#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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
InitGoogleTest
将删除 Google Test 知道的选项并将其余部分保留在argv
中。argc
也会相应调整。只需将对InitGoogleTest
的调用放在其他选项解析代码之前即可。InitGoogleTest
will remove the options Google Test knows about and leave the rest inargv
.argc
will also be adjusted accordingly. Simply put the call toInitGoogleTest
before other option-parsing code.我在此页面中找到了忽略未知选项的选项 - http://www.boost.org/doc/libs/1_45_0/doc/html/program_options/howto.html#id2075428
I found an option to ignore unknown options in this page - http://www.boost.org/doc/libs/1_45_0/doc/html/program_options/howto.html#id2075428