提升program_options异常不替换 %canonical_option% 标记

Boost program_options exception not replacing %canonical_option% tag

本文关键字:%canonical option% 替换 标记 异常 program options 提升      更新时间:2023-10-16

一直在将此(版本 1.52.0)集成到我的应用程序中,但偶然发现了上述问题。

在附加的示例中,异常 what() 方法始终保持 %canonical_option% 标签不变,并且不会替换为我的选项名称。

我正在使用VS2008,禁用了unicode(选项"无")并从我的项目中删除了所有其他文件,它只是主文件中的.cpp代码。

还是我弄错了这一切,并且我应该调用其他内容来使用正确的参数名称格式化异常消息?

#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;
}

当我再次查看代码时,在正确浏览了提升代码之后,答案变得更加明显。

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

应该是

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

如果没有引用,我们得到"对象切片",这里解释得很好:

通过引用捕获异常

不使用引用意味着我们丢失了执行模板替换的覆盖的"what"方法。

我刚刚花了一个小时来调试这个 - 这实际上是一个有趣的行为 - 我认为你的代码的唯一问题是你正在捕捉po::error

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

如果您将捕获更改为上面的行

catch(po::required_option e)
{
   cout << e.what() << std::endl;
    return 0;
}

您收到以下错误消息。

the option '--log_severity' is required but missing
Press any key to continue . . .

所以基本上看起来替换只在派生的异常中完成。

编辑:

经过一些阅读后,您实际上可以抓住std::exception,当您拨打what()时,它会打印出正确的消息。有关所有详细信息,请参阅下面的链接。

http://www.boost.org/doc/libs/1_52_0/libs/exception/doc/boost-exception.html

我还发现有一种方法可以帮助您诊断引发异常时发生的情况:

#include <boost/exception/diagnostic_information.hpp>
...
catch(...)
{
    std::cerr << "Unhandled exception!" << std::endl <<
    boost::current_exception_diagnostic_information();
    return 0;
}

例如,按上述方式更改程序,它会打印出如下所示的内容:

Unhandled exception!
Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type: class boost::exception_detail::clone_impl<struct    
                               boost::exception_detail::error_info_injector<class 
                               boost::program_options::required_option> >
std::exception::what: the option '--log_severity' is required but missing
Press any key to continue . . .