boost::program_options:如何使用完整的选项名称和更多信息进行validation_error
boost::program_options: how to make validation_error with the full option name and more informative
当我使用boost::program_options
时,抛出validation_error
后,错误消息不会显示完整的选项名称。
我有以下用g++ -std=c++11 test.cpp -lboost_program_options
编译的简单示例源代码。该程序有一个有效的命令行选项:--hello-world
,应该设置为a
或b
,例如./a.out --hello-world a
有效,但./a.out --hello-world c
无效。然后,执行带有无效选项的代码,例如./a.out --hello-world c
。错误消息如下:
the argument for option 'world' is invalid
但我希望选项名称为hello-world
,并且还应该显示无效值。我有什么办法可以改变这一点吗?
#include <boost/program_options.hpp>
int main(int argc, const char** argv)
{
namespace po = boost::program_options;
char v;
po::options_description desc("Options");
desc.add_options()
("hello-world", po::value<>(&v)->default_value('a')->notifier(
[](char x)
{
if (x != 'a' && x != 'b')
throw po::validation_error(
po::validation_error::invalid_option_value,
"hello-world", std::string(1, x));
}),
"An option must be set to either 'a' or 'b'");
try
{
po::variables_map vm;
po::store(po::command_line_parser(argc, argv). options(desc).run(), vm);
po::notify(vm);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
选项的名称是无效的"hello-world"。选项名称不能包含短划线"-"。
通常情况下,消息看起来像:
the argument ('32768') for option '--the_int16' is invalid
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 如何设置一个范围来提取我想要获得的信息
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- 为什么 gcovr 会生成空覆盖率统计信息?
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- Lua+Luabind,"runtime error"后堆栈顶部没有错误信息
- 编译Qt-base: error: libQt5Core.so.5:无版本信息