c – 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,以及什么是真的?

(即,假定程序命名为“foo”,并在命令行上执行:
foo -b?
…问号是一些其他文字的占位符:什么是所有可能的文本选项,将正确评估为false,以及真正的?)

解决方法

看看$(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++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...