Boost 枚举的自定义验证器
我正在尝试验证我定义的枚举的命令行输入,但出现编译器错误。我已使用使用Boost的program_options处理复杂选项一个可以借鉴的例子。
namespace po = boost::program_options;
namespace Length
{
enum UnitType
{
METER,
INCH
};
}
void validate(boost::any& v, const std::vector<std::string>& values, Length::UnitType*, int)
{
Length::UnitType unit;
if (values.size() < 1)
{
throw boost::program_options::validation_error("A unit must be specified");
}
// make sure no previous assignment was made
//po::validators::check_first_occurence(v); // tried this but compiler said it couldn't find it
std::string input = values.at(0);
//const std::string& input = po::validators::get_single_string(values); // tried this but compiler said it couldn't find it
// I'm just trying one for now
if (input.compare("inch") == 0)
{
unit = Length::INCH;
}
v = boost::any(unit);
}
// int main(int argc, char *argv[]) not included
为了避免包含比必要的更多的代码,我添加了如下选项:
po::options_description config("Configuration");
config.add_options()
("to-unit", po::value<std::vector<Length::UnitType> >(), "The unit(s) of length to convert to")
;
如果需要编译器错误,我可以发布它,但希望让问题看起来简单。我尝试寻找示例,但我真正能找到的唯一其他示例是 examples/regex.cpp 来自 Boost 网站。
- 我的场景和找到的示例之间有什么区别,除了我的是一个枚举,而其他的是结构? 编辑:我的场景不需要自定义验证器重载。
- 有没有办法重载枚举的validate方法? 编辑:不需要。
I am trying to validate command line input to an Enum that I've defined, but get compiler errors. I have used Handle complex options with Boost's program_options as an example to work from.
namespace po = boost::program_options;
namespace Length
{
enum UnitType
{
METER,
INCH
};
}
void validate(boost::any& v, const std::vector<std::string>& values, Length::UnitType*, int)
{
Length::UnitType unit;
if (values.size() < 1)
{
throw boost::program_options::validation_error("A unit must be specified");
}
// make sure no previous assignment was made
//po::validators::check_first_occurence(v); // tried this but compiler said it couldn't find it
std::string input = values.at(0);
//const std::string& input = po::validators::get_single_string(values); // tried this but compiler said it couldn't find it
// I'm just trying one for now
if (input.compare("inch") == 0)
{
unit = Length::INCH;
}
v = boost::any(unit);
}
// int main(int argc, char *argv[]) not included
And to spare including more code than what is necessary, I'm adding the option as follows:
po::options_description config("Configuration");
config.add_options()
("to-unit", po::value<std::vector<Length::UnitType> >(), "The unit(s) of length to convert to")
;
If the compiler error is needed I can post it, but was hoping to keep the question simple looking. I've tried looking for examples, but the only other example I could really find was the examples/regex.cpp from the Boost website.
- Is the difference between my scenario and the examples found, except that mine is an Enum where the others are Structs? EDIT: My scenario did not require a custom validator overload.
- Is there a way to overload the validate method for an Enum? EDIT: Not needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,您只需重载
operator>>
即可从istream
中提取Length::Unit
,如下所示:自定义验证器不是必需的。
In your case, you simply need to overload
operator>>
to extract aLength::Unit
from anistream
, as shown here:A custom validator is not necessary.