23main(
int argc,
char* argv[])
25 std::string output_file, db_name, classes, format(
"json");
26 bool direct_only(
false);
28 boost::program_options::options_description desc(
29 "Export conffwk schema using boost property tree.\n\nOptions/Arguments");
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")(
40 boost::program_options::value<std::string>(&format)->default_value(format),
41 "output format (\"json\", \"xml\" or \"info\")")(
"help,h",
"Print help message");
43 boost::program_options::variables_map vm;
44 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
46 if (vm.count(
"help")) {
47 std::cout << desc << std::endl;
51 if (vm.count(
"direct-only"))
54 boost::program_options::notify(vm);
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;
67 boost::property_tree::ptree pt;
71 std::ostringstream buf;
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));
78 boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(
' ', 4));
80 if (!output_file.empty()) {
81 std::ofstream f(output_file);
82 f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
86 std::cout << buf.str();
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;