24{
25 std::string output_file, db_name, classes,
format(
"json");
26 bool direct_only(false);
27
28 boost::program_options::options_description desc(
29 "Export conffwk schema using boost property tree.\n\nOptions/Arguments");
30
31 try {
32 desc.add_options()("database,d",
33 boost::program_options::value<std::string>(&db_name)->required(),
34 "database specification in format plugin-name:parameters")(
35 "classes,c", boost::program_options::value<std::string>(&classes), "regex defining class names; ignore if empty")(
36 "direct-only,r", "print direct properties")("output,o",
37 boost::program_options::value<std::string>(&output_file),
38 "output file name; print to standard out, if not defined")(
39 "format,t",
40 boost::program_options::value<std::string>(&format)->default_value(format),
41 "output format (\"json\", \"xml\" or \"info\")")("help,h", "Print help message");
42
43 boost::program_options::variables_map vm;
44 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
45
46 if (vm.count("help")) {
47 std::cout << desc << std::endl;
48 return EXIT_SUCCESS;
49 }
50
51 if (vm.count("direct-only"))
52 direct_only = true;
53
54 boost::program_options::notify(vm);
55
56 auto valid_formats = { "json", "xml", "info" };
57 if (std::none_of(valid_formats.begin(), valid_formats.end(), [&format](auto p) { return p == format; }))
58 throw std::runtime_error("unsupported format \"" + format + '\"');
59 } catch (std::exception& ex) {
60 std::cerr << "command line error: " << ex.what() << std::endl;
61 return EXIT_FAILURE;
62 }
63
64 try {
66
67 boost::property_tree::ptree pt;
68
69 db.export_schema(pt, classes, direct_only);
70
71 std::ostringstream buf;
72 if (format == "json")
73 boost::property_tree::json_parser::write_json(buf, pt);
74 else if (format == "xml")
75 boost::property_tree::xml_parser::write_xml(
76 buf, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4));
77 else
78 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(' ', 4));
79
80 if (!output_file.empty()) {
81 std::ofstream
f(output_file);
82 f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
85 } else
86 std::cout << buf.str();
87
88 return EXIT_SUCCESS;
89 } catch (const dunedaq::conffwk::Exception& ex) {
90 std::cout << "conffwk error: " << ex << std::endl;
91 } catch (const boost::property_tree::json_parser_error& ex) {
92 std::cout << "ptree json error: " << ex.what() << std::endl;
93 } catch (const std::exception& ex) {
94 std::cout << "error: " << ex.what() << std::endl;
95 }
96
97 return EXIT_FAILURE;
98}
Defines base class for cache of template objects.