Boost Program Options bool always True
Asked Answered
B

3

6

With program options, I am checking valid combinations of arguments. But for some reason, gpu argument is a bool and it is always true regardless if I set it to false on the command line. Is there a way that gpu option can be false if I specified it on the command line? I want to be able to make a bool variable that represents if the option on the command line was used.

Also I couldn't find any documentation on count() for variables_map. Is it a std::map function?

Partial Code:

namespace po = boost::program_options;
po::options_description desc("Allowed Options");
desc.add_options()
  ("help,h", "Produce help message")
  ("remove_database,r",po::value<std::vector<std::string>>
    (&remove_database),
    "Remove a pre-built database, provide a name(s) of the database")
  ("gpu,u", po::bool_switch()->default_value(false),
    "Use GPU? Only for specific algorithms");

po::variables_map vm;
po::store(po::parse_command_line(argc,argv,desc),vm);
po::notify(vm);

//Processing Cmd Args
bool help           = vm.count("help");
bool remove         = vm.count("remove_database");
bool gpu            = vm.count("gpu");

test(help,"help");
test(remove, "remove");
test(gpu, "gpu");

.....
void test(bool var1, std::string var2){
  if(var1)
    std::cout << var2 << " is active " << std::endl;
 else
    std::cout << var2 << " is not active " << std::endl;

Output:

$./a.out -r xx -u off
remove is active 
gpu is active
$./a.out -r xx -u false
remove is active 
gpu is active
Beale answered 21/8, 2015 at 22:36 Comment(1)
bool_switch docs: "Works the same way as the 'value<bool>' function, but the created value_semantic won't accept any explicit value. So, if the option is present on the command line, the value will be 'true'."Physical
P
7

You're using a bool_switch. By default, the option will be false like you specified with ->default_value(false). Since it's a switch, the mere presence of -u or --gpu when you run the executable will turn the switch to true. It doesn't matter what you put after it.

See this answer for more usage details.

Portaltoportal answered 21/8, 2015 at 22:42 Comment(0)
E
4

It seems(*) count() is always 1 for a bool_switch. Hence one should not use:

bool help           = vm.count("help");

But instead use:

bool help           = vm["help"].as<bool>();

Or for "safety"(*):

bool help           = vm.count("help") ? vm["help"].as<bool>() : false;

(*) Delving into the documentation should tell exactly what is the exact and certain way to do things.

Elbow answered 23/1, 2017 at 13:27 Comment(0)
M
4

Although not directly answer what OP has asked, I think this is an important note. According to the boost program_options spec spec, no matter what your default value is, when you specify the option from commandline it always turns the switch to true

So if you use default_value(true) for bool_switch(), you can't really turn it off...

Modify answered 1/11, 2017 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.