c++ - Boost.Program_Options : When <bool> is specified as a command-line option, 什么是有效的命令行参数?

标签 c++ boost boolean command-line-arguments boost-program-options

鉴于 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?)

最佳答案

查看 $(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));
}

关于c++ - Boost.Program_Options : When <bool> is specified as a command-line option, 什么是有效的命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15629771/

相关文章:

c++ - 从 c++14 到 c++98 的端口字符串插值

c++ - 如何构建 boost::asio 示例?

java - 如果未执行 JAXB 模型中的 setter,则不显示 XML 元素

conditional - 我可以简化这个使用逻辑否定运算符的条件语句吗?

c++ - 我怎样才能将 NASM 用作图书馆?

c++ - 具有 32 位/64 位整数重载的模板函数

c++ - Qt QSqlQuery 返回 json

c++ - 使用 Boost Reflect 或其他 C++ 反射库包装 C++ 函数

c++ - Boost.program_options : implicit_value and Unicode results in compile-time error

python - 使用相同的 boolean 值初始化列表