artdaq_demo  v3_09_00
readfhicl.cc
1 #include <iostream>
2 #include "fhiclcpp/ParameterSet.h"
3 #include "fhiclcpp/make_ParameterSet.h"
4 
5 #include <boost/program_options.hpp>
6 
7 using namespace fhicl;
8 namespace bpo = boost::program_options;
9 
10 int main(int argc, char* argv[])
11 try
12 {
13  // Get the input parameters via the boost::program_options library,
14  // designed to make it relatively simple to define arguments and
15  // issue errors if argument list is supplied incorrectly
16 
17  std::ostringstream descstr;
18  descstr << *argv << " <-c <config-file>> <other-options>";
19 
20  bpo::options_description desc = descstr.str();
21 
22  desc.add_options()("config,c", bpo::value<std::string>(), "Configuration file.")("help,h",
23  "produce help message");
24 
25  bpo::variables_map vm;
26 
27  try
28  {
29  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
30  bpo::notify(vm);
31  }
32  catch (bpo::error const& e)
33  {
34  std::cerr << "Exception from command line processing in " << *argv << ": " << e.what() << "\n";
35  return -1;
36  }
37 
38  if (vm.count("help") != 0u)
39  {
40  std::cout << desc << std::endl;
41  return 1;
42  }
43  if (vm.count("config") == 0u)
44  {
45  std::cerr << "Exception from command line processing in " << *argv << ": no configuration file given.\n"
46  << "For usage and an options list, please do '" << *argv << " --help"
47  << "'.\n";
48  return 2;
49  }
50 
51  // Check the directories defined by the FHICL_FILE_PATH
52  // environmental variable for the *.fcl file whose name was passed to
53  // the command line. If not defined, look in the current directory.
54 
55  ParameterSet complete_pset;
56 
57  if (getenv("FHICL_FILE_PATH") == nullptr)
58  {
59  std::cerr << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
60  setenv("FHICL_FILE_PATH", ".", 0);
61  }
62 
63  auto file_name = vm["config"].as<std::string>();
64  auto filepath_maker = cet::filepath_lookup("FHICL_FILE_PATH");
65 
66  make_ParameterSet(file_name, filepath_maker, complete_pset);
67 
68  std::cout << complete_pset.to_indented_string(0, false) << "\n";
69 
70  return 0;
71 }
72 
73 catch (std::exception const& x)
74 {
75  std::cerr << "Exception (type std::exception) caught in driver: " << x.what() << "\n";
76  return 1;
77 }
78 catch (...)
79 {
80  return -1;
81 }