DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
config_export_schema.cxx File Reference
#include <stdlib.h>
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include "conffwk/Configuration.hpp"
Include dependency graph for config_export_schema.cxx:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 16 of file config_export_schema.cxx.

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