argoneus argoneus - 1 year ago 85
C++ Question

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, i.e.

unicorn --fly --magic-wings-threshold

is the mode and
is an associated param. What I've noticed is if
has a default value, e.g.

("magic-wings-threshold,w", po::value<double>(&wings_thresh)->default_value(0.8, "0.8"),
"Magic wings maximum power"

then I can't use

if (vm.count("magic-wings-threshold")( {
// do stuff

to detect if the user passed that param.

It appears that default value params are always passed and detected in
. Does anyone know a workaround or alternative?

Answer Source

use boost::program_options::variable_value::defaulted()

if (vm["magic-wings-threshold"].defaulted())  {
    // assume defaulted value
} else {
    // one was provided
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download