提升program_options异常不替换 %canonical_option% 标记
Boost program_options exception not replacing %canonical_option% tag
一直在将此(版本 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 . . .
相关文章:
- GPS 的交叉编译显示"unrecognized option"
- g++ complie option中的-lutil是什么意思?
- 运行OpenMP,"clang: error: unsupported option '-fopenmp'"的Mac。
- 静态断言失败,出现"Windows headers require the default packing option..."
- GenGetOpt - 只允许"--option",不允许"-option"
- 即使关闭CLR选项,"cannot compile C file <file> with /clr option"也收到错误D8045
- 叮叮当当 警告:"warning: unknown warning option '-Wno-maybe-uninitialized'"
- Tensorflow 1.5 contrib cmake 在使用 SessionOptions GPU Option 时
- xlC 警告,"The NOSTRICT option has the potential to alter the semantics of a program"
- Using g++ on OSX Lion, ld: unknown option: -no_compact_unwin
- Qt:带有Qt OSX Yosemite的CUDA "sed: illegal option -- r"错误
- cc1plus: error: -Werror=literal-suffix: no option -Wliteral-
- 'ld: unknown option: -rpath=./libs' - 在 Mac OS 10.7.5 上构建 OpenFramework 插件时出现链接错误
- Xcode 警告:"directory not found for option"
- VS 2008 QuickWatch option
- Combining CMake option() with add_definitions()
- old option -lrt指的是哪个库(Bionic libc)
- 为什么boost::filesystem::canonical()要求目标路径存在?
- Linux Option of SwitchTothread Windows
- 提升any_range与 "canonical form" - 后者是什么?