【发布时间】:2023-03-14 06:29:01
【问题描述】:
有没有办法为参数设置一组允许的输入变量?例如参数“arg”只能有“cat”和“dog”之类的字符串值。
【问题讨论】:
有没有办法为参数设置一组允许的输入变量?例如参数“arg”只能有“cat”和“dog”之类的字符串值。
【问题讨论】:
您可以使用custom validator 功能。为您的选项定义一个不同的类型,然后在该类型上重载 validate 函数。
struct catdog {
catdog(std::string const& val):
value(val)
{ }
std::string value;
};
void validate(boost::any& v,
std::vector<std::string> const& values,
catdog* /* target_type */,
int)
{
using namespace boost::program_options;
// Make sure no previous assignment to 'v' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
std::string const& s = validators::get_single_string(values);
if (s == "cat" || s == "dog") {
v = boost::any(catdog(s));
} else {
throw validation_error(validation_error::invalid_option_value);
}
}
该代码引发的异常与任何其他无效选项值引发的异常没有什么不同,因此您应该已经准备好处理它们。
在定义选项时,请使用特殊选项类型,而不仅仅是 string:
desc.add_options()
("help", "produce help message")
("arg", po::value<catdog>(), "set animal type")
;
【讨论】:
std::istream &operator>>(std::istream &in, catdog &cd){return in >> in.value;}`
lexical_cast 将输入字符串转换为所需的数据类型,如果您想使用相同的技术,那么您确实需要实现operator>>。不过,我的示例使用直接构造。 如何从字符串创建自定义类型超出了这个问题的范围。
一个非常简单的方法是将“动物”作为普通字符串,然后通知您测试并在需要时抛出。
if (vm.count("animal") && (!(animal == "cat" || animal == "dog")))
throw po::validation_error(po::validation_error::invalid_option_value, "animal");
【讨论】:
我浏览了 Boost.Program_options 文档,但你是否可以做到这一点对我来说并不明显。我的印象是该库主要关注解析命令行,而不是验证它。您可能可以使用custom validator 解决问题,但这涉及在输入错误时引发异常(这可能是比您想要的更严重的错误)。我认为该功能更适合确保您确实得到了一个字符串,而不是它是“猫”或“狗”。
我能想到的最简单的解决方案是让库正常解析命令行,然后稍后添加您自己的代码来验证--arg 是否设置为cat 或dog。然后你可以打印一个错误并退出,恢复到一些合适的默认值,或者你喜欢的任何东西。
【讨论】: