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
7
votes
1 answer

Boost program options - get all entries in section

According to documentation I can parse config files in style : [main section] string = hello world. [foo] message = Hi ! But I need to parse list of plugins : [plugins] somePlugin. HelloWorldPlugin AnotherPlugin [settings] type = hello…
user1307957
  • 541
  • 3
  • 8
  • 19
7
votes
4 answers

boost::program_options "polymorphic" argument

I would like to use boost::program_options to create an executable which can be called as follows: ./example --nmax=0,10 # nmax is chosen randomly between 0 and 10 ./example --nmax=9 # nmax is set to 9 ./example # nmax is set to…
user1202136
  • 11,171
  • 4
  • 41
  • 62
6
votes
1 answer

boost program_options: help vs. meaningful options

Is there an easy way to separate the help-option from the 'real' program options? In fact, is it possible to define a hierarchy of options, a la BNF: options := help_options | program_options help_options := '-h' program_options := '-m1'…
xtofl
  • 40,723
  • 12
  • 105
  • 192
6
votes
1 answer

How to support commandline syntax "-DEVICE:iphone" in Boost::Program_Options?

The default syntax for Boost::Program_Options is "--DEVICE iphone". How can I support syntax "-DEVICE:iphone" or "-DEVICE=iphone"?
MQ Gu
  • 643
  • 2
  • 9
  • 19
6
votes
3 answers

Insert into boost::program_options::variables_map by index operator

I have a boost::program_options::variables_map args. Now I want to insert into this map manually like a key-value pair. Example: boost::program_options::variables_map args args["document"] = "A"; args["flag"] = true; The problem is that I…
etotientz
  • 373
  • 5
  • 18
6
votes
0 answers

UBSan: boost::program_options with std::string

We are currently investigating a possible undefined behaviour in our program that is flagged by clang7 UBSan in combination with boost::program_option from boost 1.69.0. We have created the following working example that can we compiled and run…
nafur
  • 181
  • 4
6
votes
1 answer

Using Boost.Program_options in modular program

The code I use consists of set of modules, compiled to individual libraries. Libraries in turn, are linked in different combinations to build different binaries. So for, it's pretty ordinal. Different modules use different command line arguments…
dimba
  • 26,717
  • 34
  • 141
  • 196
6
votes
2 answers

boost program_options on/off flag

Using bool_switch, I can write a command line option to turn a flag on: bool flag; po::options_description options; options.add_options() ("on", po::bool_switch(&flag)->default_value(false)) ; Where now ./a.out will have flag==false and…
Barry
  • 286,269
  • 29
  • 621
  • 977
6
votes
1 answer

Using boost::program_options with enum in class in namespace

I'm using boost::program_options to parse the command line for my program and am having trouble trying to read a value into a public enum in a class which is also in a namespace. Specifics: Boost 1.44.0 g++ 4.4.7 I tried following the process…
Tim Anderson
  • 93
  • 1
  • 6
6
votes
1 answer

Parsing unregistered options for config files in Boost program_options?

With command line options, I can do the following: po::variables_map vm; auto parsedOptions = po::command_line_parser(argc, argv).options(optionsDescription1).allow_unregistered().run(); po::store(parsedOptions, vm); po::notify(vm); auto…
petersohn
  • 11,292
  • 13
  • 61
  • 98
6
votes
1 answer

how to automatically store the value of a simple flag into a variable?

The boost option parser allows one to assign a variable to store the option value, instead of using the so_long["typing"].as() way: bool flag_value; entries.add_options() ("flag", value(&flag_value), "a simple flag…
jiandingzhe
  • 1,881
  • 15
  • 35
6
votes
1 answer

Boost Program Options: Description too wide for terminal

I am using Boost Program Options to parse command line arguments (and I don't want to miss it since it works great). However, I have one problem: Boost program options offer the possibility to assign a description to each option. Boost then offers…
phimuemue
  • 34,669
  • 9
  • 84
  • 115
6
votes
2 answers

No arguments default behavior with Boost.Program_options?

I am using Boost::Program_options to parse my command line and adapted some code from the tutorial as following: try { po::options_description desc("Allowed options"); desc.add_options() ("help,h", "output help message") …
kittikun
  • 1,829
  • 1
  • 25
  • 33
6
votes
1 answer

Handling '-' with boost.program_options

Before you say OVERKILL, I don't care. How can I make Boost.program_options handle the required cat option -? I have // visible po::options_description options("Options"); options.add_options()("-u", po::value(), "Write bytes from the input…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
6
votes
2 answers

Linking to boost::program_options does not work properly

The following library files exist: cls /usr/local/Cellar/boost/1.51.0/lib $ ls libboost_program* libboost_program_options-mt.a libboost_program_options-mt.dylib I include the following header with #include : cls…
clstaudt
  • 21,436
  • 45
  • 156
  • 239