Boost.Program_Options:当命令行选项指定为 <bool> 时,有哪些有效的命令行参数?

11

考虑以下 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)")

    ;

那么在命令行中使用哪些参数会评估为false,哪些会评估为true

也就是说,假设程序名为 "foo",并且通过命令行执行为:foo -b ?其中问号是某些其他文本的占位符:所有可能的文本选项是什么,可以正确地评估为false,哪些可以评估为true

1个回答

25

查看 $(BOOST_ROOT)/libs/program_options/src/value_semantic.cpp ,你可以找到:

/* 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));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接