我想了解program_options自定义验证,以便将python代码转换为C++之一。
任何方式
我,我必须重载验证功能
即时试图找到升压program_options文件中的原始功能的例子,但白白
任何机构可以告诉我看哪里是原来的验证功能,我LL过载它
这是愚蠢的问题,但我想知道它是如何验证在默认情况下,以获取有关验证的概念,想法,它是如何做
由于事先boost program_options自定义验证
0
A
回答
0
扩大我的一个网上评论,我总是发现boost :: program_options在参数valida中有点不足灰。
因此,我发现为每个选项类型编写自定义类通常更容易。通常,program_options使用operator>>
来解码选项值,因此如果覆盖此选项,您将有机会抛出program_options可识别的异常。如果使用嵌套异常,则可以打印相当详细的错误诊断信息。
例子:
#include <boost/program_options.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>
namespace po = boost::program_options;
// a custom option type
struct foo_or_bar {
std::string value;
// self-describing
static constexpr const char *option_name() { return "foo"; }
static constexpr const char *description() { return "single option only. value must be either foo or bar"; }
// check the value and throw a nested exception chain if it's wrong
void check_value() const
try {
if (value != "foo" and value != "bar") {
std::ostringstream ss;
ss << "value must be foo or bar, you supplied " << std::quoted(value);
throw std::invalid_argument(ss.str());
}
}
catch (...) {
std::throw_with_nested(po::validation_error(po::validation_error::invalid_option_value, option_name()));
}
// overload operators
friend std::istream &operator>>(std::istream &is, foo_or_bar &arg) {
is >> arg.value;
arg.check_value();
return is;
}
friend std::ostream &operator<<(std::ostream &os, foo_or_bar const &arg) {
return os << arg.value;
}
};
// test
void test(int argc, const char **argv) {
foo_or_bar my_foo;
po::options_description desc("test options");
desc.add_options()
(foo_or_bar::option_name(), po::value(&my_foo), foo_or_bar::description());
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
std::cout << "foo is " << my_foo << std::endl;
}
void print_exception(const std::exception& e, int level = 0)
{
std::cerr << std::string(level, ' ') << "exception: " << e.what() << '\n';
try {
std::rethrow_if_nested(e);
} catch(const std::exception& e) {
print_exception(e, level+1);
} catch(...) {}
}
int main() {
{
std::vector<const char *> test_args = {
"executable_name",
"--foo=bar"
};
test(test_args.size(), test_args.data());
}
try {
std::vector<const char *> test_args = {
"executable_name",
"--foo=bob"
};
test(test_args.size(), test_args.data());
}
catch (std::exception const &e) {
print_exception(e);
}
}
预期输出:
foo is bar
exception: the argument for option '--foo' is invalid
exception: value must be foo or bar, you supplied "bob"
+0
感谢您的回复 –
+0
感谢你的回复..im仍然初学者,所以我发现表达式“静态constexpr常量字符*”一点点复杂..我认为这将是类型指针的方法指向静态“在运行期间保持活跃”constexpr “我不明白这一点,因为它真的紧跟着是const char”....但是你的概念很棒......我一直在寻找validate函数的位置,我想我在一些论坛上为它找到了一个模板....再次感谢你的回复 –
相关问题
- 1. 升压program_options自定义分析
- 2. boost :: program_options undefined reference
- 3. setprecision for boost :: program_options
- 4. extractin/building boost program_options
- 5. 自定义验证
- 6. 自定义验证
- 7. ASP.NET - 验证自定义验证器
- 8. jquery验证:自定义验证规则
- 9. 自定义验证器不允许default_value
- 10. 自定义验证问题
- 11. 自定义指令验证
- 12. 邮差自定义验证
- 13. RSpec的自定义验证
- 14. 自定义验证消息
- 15. 自定义bean验证
- 16. ASP.NET MVC自定义验证
- 17. 关于自定义验证
- 18. WPF自定义验证MVVM
- 19. 自定义Grails验证
- 20. jquery自定义验证器
- 21. Appengine Django自定义验证
- 22. Spring自定义验证
- 23. 自定义验证摘要
- 24. 自定义验证jquery
- 25. 自定义验证检查
- 26. Asp.net自定义验证
- 27. WPF Richttextbox自定义验证
- 28. Confide自定义验证器
- 29. 表单验证“自定义”
- 30. Rails 5自定义验证
的方式我这样做,是通过为每个选项一个新类型。然后超载'运营商<<' and '>>'。我在'operator <<'中执行验证。 –