"Multiple occurrences" exception for boost program_options - c++

I am writing the following code on boost's program_options (version 1.42). This seems straight-forward and taken pretty much as is from the tutorial. However, I get a "multiple_occurrences" error. Further investigation discovers that it's (probably) the "filename" parameter that raises it.
The parameters I am giving are:
3 1 test.txt 100
I have no insight to it whatsoever.. any help will be appreciated.
po::options_description common("Common options");
common.add_options()
("help", "produce help message")
("motif_size", po::value<int>(&motif_size), "Size of motif (subgraph)")
("prob", po::value<double>(&prob), "Probably to continue examining an edge")
("filename", po::value<string>(&input_filename), "Filename of the input graph")
("repeats", po::value<int>(&n_estimates), "Number of estimates")
;
po::options_description all;
all.add(common);
po::positional_options_description p;
p.add("motif_size", 0).add("prob", 1).add("filename", 2).add("repeats", 3);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(all).positional(p).run(), vm);
po::notify(vm);

EDIT:
the second parameter to po::positional_options_description::add is the max count, not the position. The position is implied in the order you specify the positional options. So
p.add("motif_size", 0).add("prob", 1).add("filename", 2).add("repeats", 3);
should be
p.add("motif_size", 1).add("prob", 1).add("filename", 1).add("repeats", 1);
Here's a compilable snippet
include <boost/program_options.hpp>
#include <iostream>
#include <string>
int
main(unsigned argc, char** argv)
{
namespace po = boost::program_options;
po::options_description common("Common options");
common.add_options()
("help", "produce help message")
("motif_size", po::value<int>(), "Size of motif (subgraph)")
("prob", po::value<double>(), "Probably to continue examining an edge")
("filename", po::value<std::string>(), "Filename of the input graph")
("repeats", po::value<int>(), "Number of estimates")
;
po::options_description all;
all.add(common);
po::positional_options_description p;
p.add("motif_size", 1).add("prob", 1).add("filename", 1).add("repeats", 1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).
options(all).positional(p).run(), vm);
po::notify(vm);
} catch ( const boost::program_options::error& e ) {
std::cerr << e.what() << std::endl;
}
return 0;
}
and sample invocation.
macmini:~ samm$ g++ parse.cc -lboost_program_options
macmini:~ samm$ ./a.out 3 1 test.txt 100
macmini:~ samm$
My original answer is below.
What version of program_options? I had the same problem using boost 1.39, to solve it I ended up using boost 1.42.
Here's a link to the ticket describing the problem, and a patch to apply if you don't want to or can't upgrade your copy of boost. To use the new functionality, do something like this
try {
// argument parsing goes here
} catch ( const boost::program_options::multiple_occurrences& e ) {
std::cerr << e.what() << " from option: " << e.get_option_name() << std::endl;
}

Related

How to force boost program_options to check options strictly?

This is my minimal example, basically copied from boost web site:
#include <boost/program_options.hpp>
#include <iostream>
namespace po = boost::program_options;
int main(int argc, char **argv) {
po::options_description desc("Test options");
desc.add_options()
("help", "Print help")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
}
}
When I run:
./main --help
it prints help, as expected. When I run:
./main --foo
it throws unknown_option error, as expected. But when I run:
./main foobar
it ignores the argument and no error is raised. I specify no positional arguments, so I expect boost to raise error here as well.
Why does boost behave this way (I expect that there is some logical reason that I don't see yet)?
What can I do to force boost to strictly check the options provided and raise error on anything else?

How to add a description to boost::program_options' positional options?

I would like to make a positional, list program option with boost_program_options that do not allow named program options (like --files).
I have the following snippet of code:
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace po = boost::program_options;
int main(int argc, const char* argv[]) {
po::options_description desc("Allowed options");
desc.add_options()("help", "produce help message")
( "files", po::value<std::vector<std::string>>()->required(), "list of files");
po::positional_options_description pos;
pos.add("files", -1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).options(desc).positional(pos).run(), vm);
po::notify(vm);
} catch(const po::error& e) {
std::cerr << "Couldn't parse command line arguments properly:\n";
std::cerr << e.what() << '\n' << '\n';
std::cerr << desc << '\n';
return 1;
}
if(vm.count("help") || !vm.count("files")) {
std::cout << desc << "\n";
return 1;
}
}
The problem is that I can read files list as positional arguments lists as follows:
./a.out file1 file2 file3
but unfortunately like this as well ( which I would like to disable )
./a.out --files file1 file2 file3
The problem is also with the help which yields:
./a.out
Couldn't parse command line arguments properly:
the option '--files' is required but missing
Allowed options:
--help produce help message
--files arg list of files
So my desired scenario would be more like (os similar):
./a.out
Couldn't parse command line arguments properly:
[FILES ...] is required but missing
Allowed options:
--help produce help message
--optionx some random option used in future
[FILE ...] list of files
After I remove files options from desc.add_option()(...) it stop working so I believe I need it there.
As to the question posed in the title, "How to add a description to boost::program_options' positional options?", there's no functionality provided for this in the library. You need to handle that part yourself.
As for the body of the question... it's possible, but in a slightly round-about way.
The positional options map each position to a name, and the names need to exist. From what I can tell in the code (cmdline.cpp), the unregistered flag won't be set for arguments that are positional. [1], [2]
So, to do what you want, we can do the following:
Hide the --files option from showing up in the help. You will need to display appropriate help for the positional options yourself, but this is no different than before.
Add our own validation between parsing and storing of the parsed options to the variables_map.
Hiding --files from help
Here we take advantage of the the fact that we can create composite options_description using the add(...) member function:
po::options_description desc_1;
// ...
po::options_description desc_2;
// ...
po::options_description desc_composite;
desc_composite.add(desc_1).add(desc_2);
We can therefore place our files option into a hidden options_description, and create a composite that we will use only for the parsing stage. (see code below)
Preventing explicit --files
We need to intercept the list of options between parsing and storing them into the variables_map.
The run() method of command_line_parser returns an instance of basic_parsed_options, whose member options holds a vector of basic_options. There is an element for each parsed argument, and any positional options are enumerated starting from 0, any non-positional options have position -1. We can use this to perform our own validation and raise an error when we see --files as an explicit (non-positional) argument.
Example Source Code
See on Coliru
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace po = boost::program_options;
int main(int argc, const char* argv[])
{
std::vector<std::string> file_names;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("test", "test option");
std::string const FILES_KEY("files");
// Hide the `files` options in a separate description
po::options_description desc_hidden("Hidden options");
desc_hidden.add_options()
(FILES_KEY.c_str(), po::value(&file_names)->required(), "list of files");
// This description is used for parsing and validation
po::options_description cmdline_options;
cmdline_options.add(desc).add(desc_hidden);
// And this one to display help
po::options_description visible_options;
visible_options.add(desc);
po::positional_options_description pos;
pos.add(FILES_KEY.c_str(), -1);
po::variables_map vm;
try {
// Only parse the options, so we can catch the explicit `--files`
auto parsed = po::command_line_parser(argc, argv)
.options(cmdline_options)
.positional(pos)
.run();
// Make sure there were no non-positional `files` options
for (auto const& opt : parsed.options) {
if ((opt.position_key == -1) && (opt.string_key == FILES_KEY)) {
throw po::unknown_option(FILES_KEY);
}
}
po::store(parsed, vm);
po::notify(vm);
} catch(const po::error& e) {
std::cerr << "Couldn't parse command line arguments properly:\n";
std::cerr << e.what() << '\n' << '\n';
std::cerr << visible_options << '\n';
return 1;
}
if (vm.count("help") || !vm.count("files")) {
std::cout << desc << "\n";
return 1;
}
if (!file_names.empty()) {
std::cout << "Files: \n";
for (auto const& file_name : file_names) {
std::cout << " * " << file_name << "\n";
}
}
}
Test Output
Valid options:
>example a b c --test d e
Files:
* a
* b
* c
* d
* e
Invalid options:
>example a b c --files d e
Couldn't parse command line arguments properly:
unrecognised option 'files'
Allowed options:
--help produce help message
--test test option

How to properly use boost::program_options::implicit_value for vector of string?

Proof of code:
boost::program_options::options_description options;
Parser::Parser(): options("Allowed options")
{
options.add_options()
("help,h", "produce help message")
("type,t", po::value<std::string>()->required()->implicit_value(""), "Type")
}
This line is ok:
("type,t", po::value<std::string>()->required()->implicit_value(""), "Type")
How can I add this line to work correctly?:
("file,f", po::value< std::vector<std::string> >()->required()->multitoken()->implicit_value(std::vector<std::string>(0,"")), "File(s)")
Here is vector of string-s.
You just need to help the options-description to know how to present the default value to the end user.
That is, usually implicit_value would use lexical_cast<> to get the textual representation, but that (obviously) doesn't work for vector<string>. So, supply your own textual representation:
("file,f", po::value<strings>()->required()
->implicit_value(strings { "santa", "claus" }, "santa,claus"), "File(s)");
Full Demo
Live On Coliru
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char** argv) {
po::options_description options/*("Allowed options")*/;
using strings = std::vector<std::string>;
options.add_options()
("help,h", "produce help message")
("file,f", po::value<strings>()->required()->implicit_value(strings { "santa", "claus" }, "santa,claus"), "File(s)");
std::cout << options << "\n";
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options, po::command_line_style::default_style), vm);
po::notify(vm);
auto types = vm["file"].as<strings>();
for (auto t : types)
std::cout << "Got: " << t << "\n";
}
Prints:
-h [ --help ] produce help message
-f [ --file ] [=arg(=santa,claus)] File(s)
Got: santa
Got: claus

boost::program_options ignore options in config file

Could you please help me with boost::program_options?
I want the parser to ignore unknown options that are saved in config file.
I know that allow_unregistered() can be used for cmd line options, how do I proceed with text files?
Here is stripped code:
namespace po = boost::program_options;
try {
string config_file;
string gps_source;
int op_baud;
po::options_description generic("Generic options");
generic.add_options()
("ssdvpacksize", po::value<int>(),
"ssdv packets size in bytes")
("ssdvdir", po::value<string>()->default_value("/ARY1/ssdv"),
"ssdv image dir")
//unused
//I have to specify these even if they're unused
("ssdvproc_dir", po::value<string>(), "")
;
po::options_description file_options;
file_options.add(generic);
po::options_description cli_options("command line interface options");
cli_options.add(generic);
cli_options.add_options()
("config", po::value<string>(&config_file)->default_value("/boot/ary-1.cfg"), "name of a file of a configuration.");
po::variables_map vm;
store( po::command_line_parser(ac, av).options(cli_options).allow_unregistered().run(), vm );
//store( po::basic_command_line_parser<char>(ac, av).options(cli_options).allow_unregistered().run(), vm );
notify(vm);
ifstream ifs(config_file.c_str());
if (!ifs)
{
cout << "Can not open config file: " << config_file << "\n";
}
else
{
// probably smth. to do here ?
//store(parse_config_file(ifs, file_options).allow_unregistered(), vm); // does not work
store(parse_config_file(ifs, file_options), vm);
notify(vm);
}
// ...
// rest of program
}
OK, the solution is embarrassingly easy.
Line 44 should be:
store(parse_config_file(ifs, file_options, true/*allow unregistered*/), vm);

boost::program_options crashes on help when implicit_value used somewhere else

I have the following code snippet to accept runtime program options. Everything works well as long as I don't have --help on the command line. On invoking --help I receive
malloc: * error for object 0x7fff7b646570: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
on the boost::any::holder class. If the implicit_value setting is removed everything works well (even with --help). Am I missing something here?
TIA,
Nikhil
// program options descritor
po::options_description allOpts("");
// general
po::options_description genOpt("General options");
genOpt.add_options()
("help", "produce help message")
;
// mandatory options
po::options_description manOpt("Mandatory options");
manOpt.add_options()
("tilesetData", po::value<std::string>()->required(),
"tile set image file (required)")
;
// modifiables
po::options_description modifiers("Modifiable options");
modifiers.add_options()
("takeSnaps", po::value<std::string>()->implicit_value("gameShots"),
"take screen shots after every display refresh")
("music", po::value<std::string>()->implicit_value("NOT_SPECIFIED.mp3"),
"play the music specified by the file")
;
// compile all options
allOpts.add(genOpt).add(manOpt).add(modifiers);
// parse command line
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, allOpts), vm);
// create help message
if (vm.count("help")) {
std::cout << allOpts << std::endl;;
return false;
}
// check program options
try {
po::notify(vm);
}
catch (std::exception& e){
std::cerr << "Error: " << e.what() << std::endl;
return false;
}
catch(...){
std::cerr << "Unknown error!" << std::endl;
return false;
}
I suspect this is due to an incompatibility between compiler versions. Probably the boost version you are using was compiled with an older version of gcc than the version you are using to compile the program. The solution is to use the same compiler to build boost and for compiling your program.