Boost.Program_Options:当 <bool> 被指定为命令行选项时,啥是有效的命令行参数?
Posted
技术标签:
【中文标题】Boost.Program_Options:当 <bool> 被指定为命令行选项时,啥是有效的命令行参数?【英文标题】:Boost.Program_Options: When <bool> is specified as a command-line option, what are valid command-line parameters?Boost.Program_Options:当 <bool> 被指定为命令行选项时,什么是有效的命令行参数? 【发布时间】:2013-03-15 19:37:20 【问题描述】:鉴于以下对 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】:查看 $(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));
【讨论】:
以上是关于Boost.Program_Options:当 <bool> 被指定为命令行选项时,啥是有效的命令行参数?的主要内容,如果未能解决你的问题,请参考以下文章
boost::program_options - 解析多个命令行参数,其中一些是字符串,包括空格和字符
c++/boost program_options 一个选项禁用其他
未定义的引用`boost :: program_options :: options_description :: m_default_line_length'