Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatically store the value of a simple flag into a variable?

The boost option parser allows one to assign a variable to store the option value, instead of using the so_long["typing"].as<bool>() way:

bool flag_value;
entries.add_options()
("flag", value<bool>(&flag_value), "a simple flag value");
......
cout<<"flag value is: "<<flag_value<<endl;

However, the above option declaration does not create a simple flag option. It actually requires you to enter something as the value (--flag true|false|on|off|yes|no|1|0), which is not what I want.

So, is there any way store the result inside a boolean value, and still keep the option as a simple flag?

like image 687
jiandingzhe Avatar asked Jul 25 '13 09:07

jiandingzhe


1 Answers

To have an option with no value (passing means it is set true) you should create it like this:

options_description desc;
desc.add_options()
    ("help", "produce help message")

To use notifier for such an option you can use the following type as semantics:

boost::program_options::bool_switch()

it can have true or false values and no value can be explicitly taken for this option from the command line. If the option is passed then the value is true. If not passed - the value is false.

like image 68
Riga Avatar answered Sep 22 '22 16:09

Riga