How to automatically save the value of a simple flag into a variable?

The syntax effect parser allows you to assign a variable to store the parameter value instead of using the so_long["typing"].as<bool>() method so_long["typing"].as<bool>() :

 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. In fact, you need to enter something as a value (--flag true | false | on | off | yes | no | 1 | 0), which I don't want.

So, is there a way to save the result inside a boolean and save the parameter as a simple flag?

+6
source share
1 answer

To have a parameter without a value (passing means that it is set to true ), you must create it like this :

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

To use a notifier for such a parameter, you can use the following type as semantics:

 boost::program_options::bool_switch() 

it can be true or false , and no value can be explicitly used for this option from the command line. If the option is passed, then true . If it fails, false .

+10
source

Source: https://habr.com/ru/post/950244/


All Articles