Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost program_options exception not replacing %canonical_option% tag

Have been integrating this (version 1.52.0) into my app, but have stumbled upon the problem as described above.

In the example attached the exception what() method always still has the %canonical_option% tag intact and is not replaced with my option name.

I'm using VS2008, have disabled unicode (option 'none') and removed all other files from my project, it's only this code in a main.cpp file.

Or have I got this all wrong and there is something else I should be calling to format the exception message with the correct parameter name?

#include <boost/program_options.hpp>

namespace po = boost::program_options;

using namespace std;

int main(int argc, char* argv[])
{

    try {

        po::options_description optionalParams("optional");

        optionalParams.add_options() 
            ("log_severity,l", po::value<int>()->required(), "Minimum severity logging level")
            ("log_file,g", po::value<string>(), "Full path to log file")
            ;

        po::variables_map optMap;

        po::parsed_options parsed = po::command_line_parser(argc, argv)
            .options(optionalParams)
            .allow_unregistered()
            .run();

        po::store(parsed, optMap);

        po::notify(optMap);

    }
    catch(po::error e)
    {
        cout << e.what();
        return 0;
    }

    return 0;
}
like image 455
LarryDavid Avatar asked Nov 16 '12 14:11

LarryDavid


1 Answers

When I look at the code again, after having a proper browse through the boost code, the answer becomes more obvious.

catch(po::error e)
{
    cout << e.what();
    return 0;
}

Should be

catch(po::error& e)
{
    cout << e.what();
    return 0;
}

Without the reference, we get 'object slicing' which is explained well here:

Catching exceptions by reference

Not using the reference means we lose the overridden 'what' method which does the template replacement.

like image 182
LarryDavid Avatar answered Nov 13 '22 02:11

LarryDavid