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