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

boost::program_options - Is is possible to enforce mandatory flag?

I'm using boost::program_options in my program. I want to make a certain flag mandatory. Is is possible to do this with program_options in a way that it'll enforce this itself? i.e., throw an error message?
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
20
votes
1 answer

Cannot find C++ library when linking, error compliling the `boost::program_options` example

I am trying to compile the multiple_sources.cpp to compile on my computer. I am running Xubuntu Lucid Lynx fully updated. It will compile without issue with g++ -c multiple_sources.cpp but when I try to link and make an exectuable with g++…
Sled
  • 18,541
  • 27
  • 119
  • 168
19
votes
1 answer

Sets of mutually exclusive options in boost program options

My program (prog.exe) supports the following four flags: -P, -p , -b and -s. However: -b and -p must be specified together, constitute a set, and have numeric values e.g. -b 42 -s cannot be specified with the above set, and vice versa -P is…
Olumide
  • 5,397
  • 10
  • 55
  • 104
18
votes
1 answer

Boost Program Options Syntax

I'm using boost::program_options to read the users' input from the command line argument. It works very nicely and allows me to output helpful usage messages and validate input properly. However, by default long option names must come after a…
Dan
  • 12,857
  • 7
  • 40
  • 57
18
votes
1 answer

boost program options multiple values for an option

When I type a.out -i file0 file1 at the command line, I want the option -i to receive both file0 and file1 But, -i only receive file0 but not file1 But, I found that I had to type a.out -i file0 -i file1 to make -i option to receive both file0 and…
18
votes
5 answers

How to solve "boost::bad_any_cast: failed conversion using boost::any_cast" when using boost program options?

//Using boost program options to read command line and config file data #include using namespace std; using namespace boost; namespace po = boost::program_options; int main (int argc, char *argv[]) { …
bentaisan
  • 1,056
  • 3
  • 12
  • 29
17
votes
3 answers

clang-format for boost program options

clang-format seems to make a big mess out of blocks like this: desc.add_options()("help", "output usage") ("inputDirectory", po::value()->required(), "The input path") ("outputDirectory",…
David Doria
  • 9,873
  • 17
  • 85
  • 147
16
votes
2 answers

Boost program options with default values always present when using vm.count()

I've been trying to validate my passed options with boost::program_options. My command has several modes, each of which have associated params that can be specified. What I'm trying to do is ensure these associated params are passed with the mode,…
argoneus
  • 701
  • 1
  • 7
  • 14
16
votes
3 answers

Using '--' as end-of-options marker with boost::program_options

The traditional way of indicating the end of options for command line programs is with the option --. How can I get boost::program_options to recognize this as an option and accept the rest of the command line as positional arguments? The following…
uckelman
  • 25,298
  • 8
  • 64
  • 82
16
votes
1 answer

Handle complex options with Boost's program_options

I have a program that generates graphs using different multi-level models. Each multi-level model consists of a generation of a smaller seed graph (say, 50 nodes) which can be created from several models (for example - for each possible edge, choose…
R S
  • 11,359
  • 10
  • 43
  • 50
16
votes
1 answer

boost::program_options: How to specify a command without "--" or "-"

I'm using boost::program_options to implement a command-line utility with this syntax: myutil command [--in file_name] [---out file_name] where 'command' is mandatory, and is one of the following: read | write | find | version | help the thing is…
Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32
15
votes
3 answers

boost::program_options config file option with multiple tokens

I can not seem to be able to read from config file multitoken options like I can from command line. What is the syntax for the config file? This is how the option description is added: //parser.cpp - - - po::options_description* generic; generic=new…
15
votes
2 answers

is there a way to print config file for boost program options

I'm using boost::program_options to get parameters from a config file. i understand that i can create a file by hand and program options will parse it. but i'm looking for a way for the program to generate the file automatically. meaning printing…
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
15
votes
1 answer

Short options only in boost::program_options

How would one go about specifying short options without their long counterparts in boost? (",w", po::value(), "Perfrom write with N frames") generates this -w [ -- ] arg : Perfrom write with N frames Any way to specify short options only?
Novikov
  • 4,399
  • 3
  • 28
  • 36
14
votes
2 answers

Why is Boost.ProgramOptions not header-only?

Some boost libraries are header-only, some are not, and for various reasons etc. Is there a specific reason/design decision why Boost.ProgramOptions is not header-only? I'm wondering because it claims to be a "small" library in its the documentation…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
1
2
3
25 26