Integrating 'google test' and 'boost program options'

1.2k Views Asked by At

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();
}
2

There are 2 best solutions below

0
On BEST ANSWER

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

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

InitGoogleTest will remove the options Google Test knows about and leave the rest in argv. argc will also be adjusted accordingly. Simply put the call to InitGoogleTest before other option-parsing code.