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

1

6

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?

Latishalatitude answered 25/7, 2013 at 9:22 Comment(0)
L
10

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.

Limited answered 26/7, 2013 at 9:36 Comment(1)
Maybe the downvoter thought you duplicated another answer of yours ? Here is a +1 to even it out, your answer helped me.Burly

© 2022 - 2024 — McMap. All rights reserved.