5

I have the following boost::program_options program.

boost::program_options::options_description opts("Allowed options");
opts.add_options()
    ("help", "produce help message"),
    ("mingw", boost::program_options::value<std::string>(), "Set the install path for MinGW"),
    ("triple", boost::program_options::value<std::string>(), "Set the target triple"),
    ("output", boost::program_options::value<std::string>(), "Set the output file"),
    ("input", boost::program_options::value<std::vector<std::string>>(), "Set an input file."),
    ("include", boost::program_options::value<std::vector<std::string>>(), "Set an include path.")
;

boost::program_options::positional_options_description posopts;
posopts.add("input", -1);

boost::program_options::variables_map vm;
try {
    boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(opts).positional(posopts).run(), vm);
} catch(std::exception& e) {
    std::cout << e.what();
    std::cin.get();
}
boost::program_options::notify(vm);

if (vm.find("help") != vm.end()) {
    std::cout << opts << "\n";
    std::cin.get();
    return 1;
}
// Actual program logic

However, when I specify --mingw="stuff" on the command line, I found that it was rejected. After issuing the --help command, it seems that only the first option of the options in the list was actually registered with opts- even though chaining it in this way is what the tutorial recommends.

What's going wrong with this simple sample program? It's basically direct from the tutorial.

manlio
  • 18,345
  • 14
  • 76
  • 126
Puppy
  • 144,682
  • 38
  • 256
  • 465

2 Answers2

12

Looking at the tutorial, I don't see commas between the options. ie:

desc.add_options()
    ("help", "produce help message")  // no comma here!
    ("compression", po::value<int>(), "set compression level")
;

Try removing the commas you have at the end of each option.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
0

I had the same problem with boost_1_63 with open SUSE Leap 42.2. With recompiling all boost libs with ./b2 .... --build-type=complete and reinstalling the problem did not show up any longer. Hopefully this will help at least some of you.

  • Compiling the boost libs with --build-type=complete generates all versions (multithreading, debug, etc.) of the boost libs.I suppose, that when all versions of the program_options lib are installed, the best fitting version is used for linking, and thus program_options does not crash on parsing the command line. Probably there is no need to recompile all boost libs, but I have no time at the moment to explore the exact cause of the crash. – zsoltoery Mar 09 '17 at 17:20