artdaq_demo  v3_04_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 
11 int main(int argc, char* argv[]) 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[0] << " <-c <config-file>> <other-options>";
19 
20  bpo::options_description desc = descstr.str();
21 
22  desc.add_options()
23  ("config,c", bpo::value<std::string>(), "Configuration file.")
24  ("help,h", "produce help message");
25 
26  bpo::variables_map vm;
27 
28  try
29  {
30  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
31  bpo::notify(vm);
32  }
33  catch (bpo::error const& e)
34  {
35  std::cerr << "Exception from command line processing in " << argv[0]
36  << ": " << e.what() << "\n";
37  return -1;
38  }
39 
40  if (vm.count("help"))
41  {
42  std::cout << desc << std::endl;
43  return 1;
44  }
45  if (!vm.count("config"))
46  {
47  std::cerr << "Exception from command line processing in " << argv[0]
48  << ": no configuration file given.\n"
49  << "For usage and an options list, please do '"
50  << argv[0] << " --help"
51  << "'.\n";
52  return 2;
53  }
54 
55 
56  // Check the directories defined by the FHICL_FILE_PATH
57  // environmental variable for the *.fcl file whose name was passed to
58  // the command line. If not defined, look in the current directory.
59 
60  ParameterSet complete_pset;
61 
62 
63  if (getenv("FHICL_FILE_PATH") == nullptr)
64  {
65  std::cerr
66  << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
67  setenv("FHICL_FILE_PATH", ".", 0);
68  }
69 
70  auto file_name = vm["config"].as<std::string>();
71  auto filepath_maker = cet::filepath_lookup("FHICL_FILE_PATH");
72 
73  make_ParameterSet(file_name, filepath_maker, complete_pset);
74 
75  std::cout << complete_pset.to_indented_string(0, false) << "\n";
76 
77  return 0;
78 }
79 
80 catch (std::string& x)
81 {
82  std::cerr << "Exception (type string) caught in driver: " << x << "\n";
83  return 1;
84 }
85 
86 catch (char const* m)
87 {
88  std::cerr << "Exception (type char const*) caught in driver: " << std::endl;
89  if (m)
90  {
91  std::cerr << m;
92  }
93  else
94  {
95  std::cerr << "[the value was a null pointer, so no message is available]";
96  }
97  std::cerr << '\n';
98 }