LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
fhicl-dump.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // Executable for dumping processed configuration files
4 //
5 // ======================================================================
6 
7 #include "boost/program_options.hpp"
8 #include "cetlib_except/demangle.h"
13 #include "fhiclcpp/parse.h"
14 
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 
19 using namespace fhicl;
20 using namespace fhicl::detail;
21 
22 namespace {
23 
24  std::string const fhicl_env_var{"FHICL_FILE_PATH"};
25 
26  // Error categories
27  std::string const help{"Help"};
28  std::string const processing{"Processing"};
29  std::string const config{"Configuration"};
30 
31  using std::string;
32 
33  struct Options {
35  bool quiet{false};
36  string output_filename;
37  string input_filename;
38  int lookup_policy{};
39  string lookup_path;
40  };
41 
42  Options process_arguments(int argc, char* argv[]);
43 
44  fhicl::ParameterSet form_pset(string const& filename,
45  cet::filepath_maker& lookup_policy);
46 
47  std::unique_ptr<cet::filepath_maker> get_policy(int const lookup_policy,
48  string const& lookup_path);
49 }
50 
51 //======================================================================
52 
53 int
54 main(int argc, char* argv[])
55 {
56 
57  Options opts;
58  try {
59  opts = process_arguments(argc, argv);
60  }
61  catch (cet::exception const& e) {
62  if (e.category() == help)
63  return 1;
64  if (e.category() == processing) {
65  std::cerr << e.what() << '\n';
66  return 2;
67  }
68  if (e.category() == config) {
69  std::cerr << e.what() << '\n';
70  return 3;
71  }
72  }
73 
74  auto const policy = get_policy(opts.lookup_policy, opts.lookup_path);
75 
76  ParameterSet pset;
77  try {
78  pset = form_pset(opts.input_filename, *policy);
79  }
80  catch (cet::exception const& e) {
81  std::cerr << e.what() << '\n';
82  return 4;
83  }
84  catch (...) {
85  std::cerr << "Unknown exception\n";
86  return 5;
87  }
88 
89  if (opts.quiet)
90  return 0;
91 
92  std::ofstream ofs{opts.output_filename};
93  std::ostream& os = opts.output_filename.empty() ? std::cout : ofs;
94 
95  os << "# Produced from '" << argv[0] << "' using:\n"
96  << "# Input : " << opts.input_filename << '\n'
97  << "# Policy : "
98  << cet::demangle_symbol(typeid(decltype(*policy)).name()) << '\n'
99  << "# Path : \"" << opts.lookup_path << "\"\n\n"
100  << pset.to_indented_string(0, opts.mode);
101 }
102 
103 //======================================================================
104 
105 namespace {
106 
107  Options
108  process_arguments(int argc, char* argv[])
109  {
110  namespace bpo = boost::program_options;
111 
112  Options opts;
113 
114  bool annotated{false};
115  bool prefix_annotated{false};
116 
117  bpo::options_description desc("fhicl-dump [-c] <file>\nOptions");
118  desc.add_options()("help,h", "produce this help message")(
119  "config,c", bpo::value<std::string>(&opts.input_filename), "input file")(
120  "output,o",
121  bpo::value<std::string>(&opts.output_filename),
122  "output file (default is STDOUT)")(
123  "annotated,a",
124  bpo::bool_switch(&annotated)->default_value(false, "false"),
125  "include source location annotations")(
126  "prefix-annotated",
127  bpo::bool_switch(&prefix_annotated)->default_value(false, "false"),
128  "include source location annotations on line preceding parameter "
129  "assignment (mutually exclusive with 'annotated' option)")(
130  "quiet,q", "suppress output to STDOUT")(
131  "lookup-policy,l",
132  bpo::value<int>(&opts.lookup_policy)->default_value(1),
133  "lookup policy code:"
134  "\n 0 => cet::filepath_maker"
135  "\n 1 => cet::filepath_lookup"
136  "\n 2 => cet::filepath_lookup_nonabsolute"
137  "\n 3 => cet::filepath_lookup_after1")(
138  "path,p",
139  bpo::value<std::string>(&opts.lookup_path)->default_value(fhicl_env_var),
140  "path or environment variable to be used by lookup-policy");
141 
142  bpo::positional_options_description p;
143  p.add("config", -1);
144 
145  bpo::variables_map vm;
146  try {
147  bpo::store(
148  bpo::command_line_parser(argc, argv).options(desc).positional(p).run(),
149  vm);
150  bpo::notify(vm);
151  }
152  catch (bpo::error& err) {
153  std::ostringstream err_stream;
154  err_stream << "Error processing command line in " << argv[0] << ": "
155  << err.what() << '\n';
156  throw cet::exception(processing) << err_stream.str();
157  };
158 
159  if (vm.count("help")) {
160  std::cout << desc << '\n';
161  throw cet::exception(help);
162  }
163 
164  if (vm.count("quiet")) {
165  if (annotated || prefix_annotated) {
166  throw cet::exception(config) << "Cannot specify both '--quiet' and "
167  "'--(prefix-)annotated' options.\n";
168  }
169  opts.quiet = true;
170  }
171 
172  if (annotated && prefix_annotated) {
173  throw cet::exception(config) << "Cannot specify both '--annotated' and "
174  "'--prefix-annotated' options.\n";
175  }
176 
177  if (annotated)
178  opts.mode = print_mode::annotated;
179  if (prefix_annotated)
180  opts.mode = print_mode::prefix_annotated;
181 
182  if (!vm.count("config")) {
183  std::ostringstream err_stream;
184  err_stream << "\nMissing input configuration file.\n\n" << desc << '\n';
185  throw cet::exception(config) << err_stream.str();
186  }
187  return opts;
188  }
189 
190  std::unique_ptr<cet::filepath_maker>
191  get_policy(int const lookup_policy, std::string const& lookup_path)
192  {
193  switch (lookup_policy) {
194  case 0:
195  return std::make_unique<cet::filepath_maker>();
196  case 1:
197  return std::make_unique<cet::filepath_lookup>(lookup_path);
198  case 2:
199  return std::make_unique<cet::filepath_lookup_nonabsolute>(lookup_path);
200  case 3:
201  return std::make_unique<cet::filepath_lookup_after1>(lookup_path);
202  default:
203  std::ostringstream err_stream;
204  err_stream << "Error: command line lookup-policy " << lookup_policy
205  << " is unknown; choose 0, 1, 2, or 3\n";
206  throw std::runtime_error(err_stream.str());
207  }
208  }
209 
211  form_pset(std::string const& filename, cet::filepath_maker& lookup_policy)
212  {
214  fhicl::parse_document(filename, lookup_policy, tbl);
215  fhicl::ParameterSet pset;
216  fhicl::make_ParameterSet(tbl, pset);
217  return pset;
218  }
219 }
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
parameter set interface
std::string to_indented_string() const
int main(int argc, char *argv[])
Definition: fhicl-dump.cc:54
Float_t e
Definition: plot.C:34
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void parse_document(std::string const &filename, cet::filepath_maker &maker, intermediate_table &result)
Definition: parse.cc:856