Reputation: 37
I am writing a program that would receive as parameters a filename followed by multiple strings. Optionally it could take a -count
argument;
./program [-count n] <filename> <string1> <string2> ...
This is the code I wrote:
PO::positional_options_description l_positionalOptions;
l_positionalOptions.add("file", 1);
l_positionalOptions.add("strings", -1);
PO::options_description l_optionsDescription;
l_optionsDescription.add_options()
("count", PO::value<int>()->default_value(1), "How many times to write"),
("file", PO::value<std::string>(), "Output file name"),
("strings", PO::value<std::vector<std::string>>()->multitoken()->zero_tokens()->composing(), "Strings to be written to the file");
PO::command_line_parser l_parser {argc, argv};
l_parser.options(l_optionsDescription)
.positional(l_positionalOptions)
.allow_unregistered();
PO::variables_map l_userOptions;
try {
PO::store(l_parser.run(), l_userOptions);
}
catch (std::exception &ex) {
std::cerr << ex.what() << std::endl;
exit(1);
}
However, when I run ./program file.out str1 str2 str3
it fails with:
unrecognised option 'str1'
What am I doing wrong? Is this even possible with boost::program_options?
Upvotes: 0
Views: 96
Reputation: 37
I figured it out. It's as dumb as it can get.
The issue was that I had a ,
after every entry in add_options()
.
This made it so only the first entry would get saved.
Upvotes: 1