Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Program_Options: When <bool> is specified as a command-line option, what are valid command-line parameters?

Given the following simple use of Boost.Program_Options:

boost::program_options::options_description options("Options");

options.add_options()

    ("my_bool_flag,b", boost::program_options::value<bool>(), "Sample boolean switch)")

    ;

... what command-line arguments will evaluate to false, and what to true?

(I.e., assume the program is named "foo", and executed on the command line as: foo -b ? ... with the question mark a placeholder for some other text: What are all possible text options that will properly evaluate to false, and what to true?)

like image 508
Dan Nissenbaum Avatar asked Mar 26 '13 05:03

Dan Nissenbaum


People also ask

What is boost :: Program_options?

Boost C++ Libraries The program_options library allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.

Is Boost Program Options header only?

Some boost libraries are header-only, some are not, and for various reasons etc. Is there a specific reason/design decision why Boost.


1 Answers

Looking at $(BOOST_ROOT)/libs/program_options/src/value_semantic.cpp you can find:

/* Validates bool value.
    Any of "1", "true", "yes", "on" will be converted to "1".<br>
    Any of "0", "false", "no", "off" will be converted to "0".<br>
    Case is ignored. The 'xs' vector can either be empty, in which
    case the value is 'true', or can contain explicit value.
*/
BOOST_PROGRAM_OPTIONS_DECL void validate(any& v, const vector<string>& xs,
                   bool*, int)
{
    check_first_occurrence(v);
    string s(get_single_string(xs, true));

    for (size_t i = 0; i < s.size(); ++i)
        s[i] = char(tolower(s[i]));

    if (s.empty() || s == "on" || s == "yes" || s == "1" || s == "true")
        v = any(true);
    else if (s == "off" || s == "no" || s == "0" || s == "false")
        v = any(false);
    else
        boost::throw_exception(invalid_bool_value(s));
}
like image 87
user2210800 Avatar answered Sep 19 '22 15:09

user2210800