Search code examples
boostboost-program-options

Boost.program_options silently fails only after adding positional option to the parser


I am trying to use Boost.program_options, but positional arguments do not work. I am a newbie at this.

#include "Application.h"

#include <iostream>
#include <vector>
#include <fstream>
#include <istream>
#include <string>

#include <boost/program_options.hpp>

namespace po = boost::program_options;
using namespace std;


int Application::run(int ac, char *av[])
{
    po::options_description opts_desc("Allowed options");
    opts_desc.add_options()
            ("help,h", "produce help message")
            ;

    po::positional_options_description pos_opts_desc;
    pos_opts_desc.add("xyz", -1); // +1 doesnt work either


    po::variables_map vm {};
    cout << "Before store" << endl;
    po::store(po::command_line_parser(ac, av)
    .options(opts_desc)
    .positional(pos_opts_desc)
    .run(), vm);

    
    cout << "After store" << endl;

return 1;
}

It is very simple piece of code that does nothing. However, it does not work, on calling po::store, the program silently exits, the "After store" message doesn't get printed.

I've got gdb setup in vscode and it can be seen that the fail occurs at store.

However, there is no error message, nothing at all.

I am using boost 1.77 packaged with some distribution of MinGW.

I am not sure what more details to provide.

When I comment out the .positional function call, the program finishes as it should.

What could I be doing wrong?


Solution

  • Turns out it was once again only my stupidity:

    Positional options need to be declared also as options, in options_description.

    The documentation of Boost is quite sparse, for me at least, but it was included in the one example, which I missed.

    What is more, I simply did not catch the right exception type, when catching boost::program_options::error, the error is reported.

    I guess I also expected the debugger to help me with exceptions.