Questions tagged [boost-program-options]

Boost.Program_options is a C++ library that allows program developers to obtain (name, value) pairs from the user via conventional methods such as command line and config file.

Boost.Program_options is a C++ library that allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.

Why would you use such a library, and why is it better than parsing your command line by straightforward hand-written code?

  • It's easier. The syntax for declaring options is simple, and the library itself is small. Things like conversion of option values to desired type and storing into program variables are handled automatically.
  • Error reporting is better. All the problems with the command line are reported, while hand-written code can just misparse the input. In addition, the usage message can be automatically generated, to avoid falling out of sync with the real list of options.
  • Options can be read from anywhere. Sooner or later the command line will be not enough for your users, and you'll want config files or maybe even environment variables. These can be added without significant effort on your part.
381 questions
2
votes
1 answer

Handle no value option in Config file in Boost program_options

This question has been already been asked here before - Boost parse_config_file, empty key value. But since there was no proper solution provided there, I am asking this again hoping someone could provide a better solution. Unlike the above…
abhishek gupta
  • 359
  • 1
  • 12
2
votes
2 answers

boost::program_options::positional_options_description termination

The following program aborts with pointer being freed was not allocated: #include int main(int argc, char* argv[]) { boost::program_options::positional_options_description positional; return 0; } I compiled and…
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
2
votes
1 answer

Boost.Program_Options: Why does options_description_easy_init::operator() not have an overload for std::string?

Consider this MCVE: #include #include #include namespace po = boost::program_options; using namespace std; po::options_description createOptions(const std::string& description, const map
andreee
  • 4,459
  • 22
  • 42
2
votes
2 answers

How do I define operator >> (istream &, ...) for a standard type that lacks a pre-defined operator without extending namespace `std`?

Apparently it's Undefined Behaviour to add (almost) anything to the std namespace. I'm using C++14 which has no std::chrono::parse() (C++20 only) but I need to deserialise values of type std::chrono::milliseconds (a specialisation of…
davidA
  • 12,528
  • 9
  • 64
  • 96
2
votes
1 answer

Using boost::program_options::validate with own class in namespace

The following adapted example compiles if magic_number is not in a namespace, but fails if it is in a namespace. Why doesn't it find the validate function if the target class is in a namespace? The error produced on MSVC is:…
wally
  • 10,717
  • 5
  • 39
  • 72
2
votes
2 answers

boost::program_options config file format

I am using boost::program_options to load command line arguments. Now I want to additionally load a config file with the same arguments set. I use it in very standard way: ifstream ifs(filename.c_str()); if (ifs) { …
Martin
  • 165
  • 1
  • 15
2
votes
2 answers

boost::program_options bool_switch used multiple times

What I currently have is ("someOption,s", po::bool_switch(&variable)->default_value(false), "") I would like to be able to specify the argument multiple times and each occurence would switch the value of this bool. Examples: default value =…
Martin
  • 165
  • 1
  • 15
2
votes
3 answers

invalid option value exception using boost program-options

I have a Visual Studio 2008 C++ application using boost v1.45.0 program-options. I would like to be able to parse a command-line option that looks like this: foo.exe -x 1,2, 4-7 such that it produces a std::vector< int > with the values [1, 2, 4, 5,…
PaulH
  • 7,759
  • 8
  • 66
  • 143
2
votes
1 answer

Using boost program options under Visual Studio MSVC 14.0 Assertion failed

I have compiled Boost 1.66 under MSVC 14.0. I'm trying to port an existing project which works fine under Boost on Linux to Visual Studio. That's the piece of code that fails: options.add_options() ("help", "Produce help message") …
2
votes
1 answer

boost program_option case insensitive parse of a configuration file

I would like to use boost::program_options to read the options from a configuration file, allowing for a case insensitive parsing. Consider, e.g., the following simple code: #include #include #include #include…
francesco
  • 7,189
  • 7
  • 22
  • 49
2
votes
2 answers

Integrating 'google test' and 'boost program options'

I have a program that uses google test, and boost program options library for parsing options. The problem is that google test also has it's own option parsers, so I need to filter out before giving the options to the google test. For example, When…
prosseek
  • 182,215
  • 215
  • 566
  • 871
2
votes
3 answers

Read/Write inifiles with boost::{program_options,property_tree}

Utilizing boost, I would like to read options from an inifile, abort if an unknown option is encountered in the inifile and save them later in another inifile. The first part can be done with boost::program_options: try{ …
Oscillon
  • 165
  • 3
  • 13
2
votes
2 answers

Using boost program_options, select sets of options based upon one mandatory 'mode' option

Lets presume I have a number of option groups called modeA modeB common. I'll refer to these within {} - ie {common} expands to any option described in that group. I'd like to be able to enforce the following command lines command A {common}…
Gray-M
  • 302
  • 2
  • 12
2
votes
1 answer

Getting boost program options to persist after function scope

I have a function which takes a boost::program_options::options_description, adds some options, then returns the thing back. It seems as though my options are not persisting after the scope of the function ends, and my options disappear. How do I…
Him
  • 5,257
  • 3
  • 26
  • 83
2
votes
1 answer

boost program_options disable no spaces in command line

I am trying to fix a problem in a program using boost program_options to process the command line. There are two different commands starting with s. One is defined in add_options as something like this: "start,s",po::value() The other is defined…
user3407352
  • 105
  • 6