16

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

Where --fly is the mode and --magic-wings-threshold is an associated param. What I've noticed is if --magic-wings-threshold 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 vm.count(). Does anyone know a workaround or alternative?

4

2 回答 2

22

利用boost::program_options::variable_value::defaulted()

if (vm["magic-wings-threshold"].defaulted())  {
    // assume defaulted value
} else {
    // one was provided
}
于 2012-02-08T20:12:53.653 回答
0

如果你想区分

-k option not provided
-k provided

您应该使用 po::value()-> implicit_value (),您可以通过以下方式区分不同的情况:

-k option not provided  ->  vm["k"]==0  
-k option provided      ->  vm["k"]==1
于 2016-09-02T16:42:21.853 回答