Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print help for both normal and positional args with boost::program_options

When you use Boost library program_options it is very easy to print help for your program:

boost::program_options::variables_map options;
boost::program_options::options_description optionsDesc;
boost::program_options::positional_options_description positionalOptionsDesc;
//...
if(options.count("help"))
{
    cerr << optionsDesc << endl;
}

But how do you add the options from positional_options_description to the help message? In the tutorial I can see the output of such set-up, at the end of the section:

http://www.boost.org/doc/libs/1_52_0/doc/html/program_options/tutorial.html#id2607297

The option input-file is printed in help and it is positional. But I can't see the code. Is there an build-in way to print it, like with options_description or you have to do it manually? Apparently the << does not work for positional_options_description, the compilation error is:

error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
like image 550
nuoritoveri Avatar asked Jan 02 '13 17:01

nuoritoveri


People also ask

What is boost :: Program_options?

Boost C++ Libraries The program_options library allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.

How do I use Boost program options?

To add options, you use the add_options() function and append all your options, each one surrounded with parenthesis. Here, we declared two options (help and version). They can be set with --help and --version. You can use operator on the description to output all the options of program.


2 Answers

Notice that streaming description only prints out the options. It does not print the name of the program or the actual description of what the program does. You should manually print any positional parameter you have as part of the output message:

Instead of

if (vm.count("help")) {
    cout << "Usage: options_description [options]\n";
    cout << desc;
    return 0;
}

You could easily say

if (vm.count("help")) {
    cout << "Usage: " << argv[0] << " [options] <description of positional 1> <description of positional 2> ...\n";
    cout << desc;
    return 0;
}
like image 98
Bee Avatar answered Oct 20 '22 19:10

Bee


Have a look at boost::program_options::positional_options_description.name_for_position(i)

The error message is something unrelated, I forget what eactly something to do with cpp11

like image 36
nishantjr Avatar answered Oct 20 '22 19:10

nishantjr