When I'm using boost::program_options
, after throwing out validation_error
, the error message does not display the full option name.
I have the following simple example source code, compiled with g++ -std=c++11 test.cpp -lboost_program_options
. This program has one valid command line option: --hello-world
, which is supposed to be set to either a
or b
, e.g. ./a.out --hello-world a
is valid but ./a.out --hello-world c
is invalid. Then, execute the code with an invalid option, for example ./a.out --hello-world c
. The error message is like the following:
the argument for option 'world' is invalid
But I would expect the option name to be hello-world
and the invalid value should also be displayed. Is there any way I can change this?
#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;
}