0

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;
}
xuhdev
  • 8,018
  • 2
  • 41
  • 69

1 Answers1

0

The name of your option is invalid "hello-world". Option names can not contain a dash, "-".

Typically the message will look like:

the argument ('32768') for option '--the_int16' is invalid
Nathan G
  • 9
  • 1
  • The official example here `input-file` uses a dash in its option name: http://www.boost.org/doc/libs/1_63_0/doc/html/program_options/tutorial.html#idp523371328 – xuhdev Apr 11 '17 at 05:55