I am using boost::program_options to pass configuration files for my program. In particular I use often command line overriding of some of the options. For example if I register two options "opt1" and "opt2" I can successfully override the default values by running my program with
myProgram.exe --opt1=option_value_1 --opt2=option_value_2
All good, but it happened already few times that I run my program mistakenly as
myProgram.exe --opt1=option_value_1 opt2=option_value_2
In such a case (missing double hyphen) no error is thrown. In fact I can apparently run myProgram as
myProgram.exe list of any unregistered and unknown values
and it still runs correctly. I would expect to at least get informed that something unexpected happened. Is there a solution to my problem?
You should remove allow_unregistered() from your parse command. You command should simply be
po::store(parse_command_line(argc, argv, desc), vm);
then exception will be thrown on unknown options.
http://www.boost.org/doc/libs/1_54_0/doc/html/program_options/howto.html#idp123440592
If you want exception/error, if option has no "--" you should write extra parser, something like this, can help you
std::pair<std::string, std::string> fix_option(const std::string& value)
{
std::string name = value;
std::string val;
std::string::size_type pos = name.find("=");
if (pos != std::string::npos)
{
val = name.substr(pos + 1);
name = name.substr(0, pos);
}
if (name.substr(0, 2) != "--")
{
throw std::logic_error(std::string("invalid command, no -- in command: ") + name);
}
return std::make_pair(name.substr(2), val);
}
code example
results:
./new --help=j
output: j
./new help=j
output:
terminate called after throwing an instance of 'std::logic_error'
what(): invalid command, no -- in command: help
It seems that boost::program_options does not recognize positional arguments per default. This means that non-option arguments like opt2=option_value_2 are ignored. However, the documentation is not clear about it. You can enable handling of positional arguments with basic_command_line_parser::positional().
By Example
try {
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(desc).positional({}).run(),
vm);
po::notify(vm);
} catch (po::too_many_positional_options_error &e) {
// A positional argument like `opt2=option_value_2` was given
cerr << e.what() << endl;
exit(1);
} catch (po::error_with_option_name &e) {
// Another usage error occurred
cerr << e.what() << endl;
exit(1);
}
Explanation
Basically,
po::store(po::parse_command_line(argc, argv, desc), vm);
has been replaced with
po::store(po::command_line_parser(argc, argv)
.options(desc).positional({}).run(),
vm);
As I understand the documentation, parse_command_line(argc, argv, desc) is a shorthand for command_line_parser(argc, argv).options(desc).run(). Through adding a call to positional(), we are enabling handling of positional arguments. Through specifying {}, no positional arguments are allowed. An instance of too_many_positional_options_error is thrown when too many positional arguments are given.
Related
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?
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.
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
When I'm using boost::program_options, after throwing out validation_error, the error message does not display the full option name.
I have the following simple example source code, compiled with g++ -std=c++11 test.cpp -lboost_program_options. This program has one valid command line option: --hello-world, which is supposed to be set to either a or b, e.g. ./a.out --hello-world a is valid but ./a.out --hello-world c is invalid. Then, execute the code with an invalid option, for example ./a.out --hello-world c. The error message is like the following:
the argument for option 'world' is invalid
But I would expect the option name to be hello-world and the invalid value should also be displayed. Is there any way I can change this?
#include <boost/program_options.hpp>
int main(int argc, const char** argv)
{
namespace po = boost::program_options;
char v;
po::options_description desc("Options");
desc.add_options()
("hello-world", po::value<>(&v)->default_value('a')->notifier(
[](char x)
{
if (x != 'a' && x != 'b')
throw po::validation_error(
po::validation_error::invalid_option_value,
"hello-world", std::string(1, x));
}),
"An option must be set to either 'a' or 'b'");
try
{
po::variables_map vm;
po::store(po::command_line_parser(argc, argv). options(desc).run(), vm);
po::notify(vm);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
The name of your option is invalid "hello-world". Option names can not contain a dash, "-".
Typically the message will look like:
the argument ('32768') for option '--the_int16' is invalid
I have a cfg file as the following:
parameter1="hello"
parameter2=22
parameter3=12
Using boost_program to read all the parameters works fine with this code:
po::options_description options("Options");
options.add_options()
("help,h", "produce help message")
("parameter1", po::value<string>(¶meter1)->default_value("bye"),
"parameter1")
("parameter2", po::value<int>(¶meter2)->default_value(2),
"parameter2")
("parameter3", po::value<int>(¶meter3)->default_value(4),
"parameter3");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);
notify(vm);
try
{
po::store(po::parse_config_file< char >(filePath, options), vm);
}
catch (const std::exception& e)
{
std::cerr << "Error parsing file: " << filePath << ": " << e.what() << std::endl;
}
...
But when I try to do a generic method where i just want to read one parameter given from a call, I have an error parsing.
I want to read the second parameter for instance, so I write this:
const char parameter_string = "parameter2";
int default = 30;
int parameter;
getparameter(parameter_string,parameter,default);
and goes to the method getsparameter where this is what I have this time:
...
po::options_description options("Options");
options.add_options()
("help,h", "produce help message")
(parameter_string, po::value<int>(¶meter)->default_value(default),
"reading parameter");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);
notify(vm);
but the error is:
Error parsing file: file.cfg: unknown option parameter1
So my question is if it is posible to read only one parameter from a file or it is necessary to parse all the parameters with boost_program in options.add_option including as many lines as parameters I write in the config file and then take the value from the parameter I want.
Use allow_unregistered function :
Specifies that unregistered options are allowed and should be passed
though. For each command like token that looks like an option but does
not contain a recognized name, an instance of basic_option will
be added to result, with 'unrecognized' field set to 'true'. It's
possible to collect all unrecognized options with the
'collect_unrecognized' funciton.
As I an using "parse_config_file" I see in the documentation that "allow_unregistered" is set to false by default.
template<typename charT>
BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options< charT >
parse_config_file(std::basic_istream< charT > &,
const options_description &,
bool allow_unregistered = false);
So i modified my line like this:
Old code:
po::store(po::parse_config_file< char >(filePath, options), vm);
New code:
po::store(po::parse_config_file< char >(filePath, options, true), vm);
And as I said, It works. Thank you for your answer.
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.