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 23 of file config_export_schema.cxx.

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