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