Question

I am trying to get input options using boost::program_options.

I would like to have two source of options one from command line and another from file but I also want to have different option names for the same value.

commandLine.add_options()
 ("dim,d", po::value<int>(&dimension), "Problem dimension")
 ("adv", po::value<bool>(&adv_enabled), "Enable/Disable advection term {1|0}")
 ("div", po::value<bool>(&div_enabled), "Enable/Disable divergance term {1|0}")

file_options.add_options()
 ("dimension",po::value<int>(&dimension), "Set Problem dimension")
 ("enable.advection", po::value<bool>(&adv_enabled), "Enable/Disable advection")
 ("enable.divergance", po::value<bool>(&div_enabled), "Enable/Disable divergance")

Here I want to use shorter versions in command line and grouped versions in file.

Is there a way to just pass the variable to both or should I parse them in code?

Était-ce utile?

La solution

The answer is quite straightforward in the documentation:

Of course, there will be a need to combine the values from command line and config file. For example, the optimization level specified on the command line should override the value from the config file. On the other hand, include paths should be combined.

... what happens if the same value is specified both on the command line and in config file? Usually, the value stored first is preferred. This is what happens for the "--optimization" option. For "composing" options, like "include-file", the values are merged.
http://www.boost.org/doc/libs/1_55_0/doc/html/program_options/tutorial.html#idp163316264

You can add options which repeat on the command line and in the config, and if it is a non-merging option, the preference will be given to the one first called with po::store()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top