LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
BasicOptionsHandler.cc
Go to the documentation of this file.
2 
8 #include "cetlib/container_algorithms.h"
9 #include "cetlib/filepath_maker.h"
10 #include "fhiclcpp/coding.h"
13 #include "fhiclcpp/parse.h"
14 
15 #include <iostream>
16 #include <string>
17 
18 using namespace std::string_literals;
20 namespace {
21 
22  std::string
23  pretty_version(std::string s)
24  {
25  std::replace(s.begin(), s.end(), '_', '.');
26  return s.substr(1); // trim off 'v'
27  }
28 
29 } // namespace
30 
31 art::BasicOptionsHandler::BasicOptionsHandler(bpo::options_description& desc,
32  cet::filepath_maker& maker)
33  : help_desc_{desc}, maker_{maker}
34 {
35  auto options = desc.add_options();
36  add_opt(options, "help,h", "produce help message");
37  add_opt(
38  options,
39  "version",
40  ("Print art version (" + pretty_version(art::getReleaseVersion()) + ")")
41  .c_str());
42  add_opt(
43  options, "config,c", bpo::value<std::string>(), "Configuration file.");
44  add_opt(
45  options, "process-name", bpo::value<std::string>(), "art process name.");
46  add_opt(
47  options,
48  "print-available",
49  bpo::value<std::string>(),
50  ("List all available plugins with the provided suffix. Choose from:"s +
52  .c_str());
53  add_opt(options,
54  "print-available-modules",
55  "List all available modules that can be invoked in a FHiCL file.");
56  add_opt(options,
57  "print-available-services",
58  "List all available services that can be invoked in a FHiCL file.");
59  add_opt(options,
60  "print-description",
61  bpo::value<std::vector<std::string>>()->multitoken(),
62  "Print description of specified module, service, source, or other "
63  "plugin (multiple OK). Argument can be a regular expression used "
64  "to match the plugin specification. To narrow the search to "
65  "plugins with a particular suffix, preface the regular expression"
66  "with the suffix (e.g. service:TFileService).");
67  add_opt(options,
68  "status-bar",
69  "Provide status bar that reports the progress of retrieving "
70  "plugin information for a 'print-available' command.");
71 }
72 
73 int
74 art::BasicOptionsHandler::doCheckOptions(bpo::variables_map const& vm)
75 {
76  // Technically the "help" and "print*" options are processing steps,
77  // but we want to short-circuit.
78  if (vm.count("help")) {
79  // Could simply do cout << help_desc_, but the boost-provided
80  // printout does not add any left-hand padding. Will add a
81  // 2-space tab by hand.
82  std::stringstream ss;
83  ss << help_desc_; // Note NOT our own desc_.
84  for (std::string s; std::getline(ss, s);)
85  std::cout << std::string(2, ' ') << s << '\n';
86  std::cout << '\n';
87  return detail::info_success();
88  }
89  bool const status_bar = vm.count("status-bar") > 0;
90  if (vm.count("print-available")) {
92  Suffixes::get(vm["print-available"].as<std::string>()), status_bar);
93  return detail::info_success();
94  }
95  if (vm.count("print-available-modules")) {
97  return detail::info_success();
98  }
99  if (vm.count("print-available-services")) {
101  return detail::info_success();
102  }
103  if (status_bar) {
105  << "The '--status-bar' option can be used only with the "
106  "'--print-available*' program options.\n";
107  }
108 
109  if (vm.count("print-description")) {
111  vm["print-description"].as<std::vector<std::string>>());
112  return detail::info_success();
113  }
114  if (vm.count("version")) {
115  std::cout << "art " << pretty_version(getReleaseVersion()) << '\n';
116  return detail::info_success();
117  }
118 
119  if (!vm.count("config")) {
120  throw Exception(errors::Configuration) << "No configuration file given.\n";
121  }
122  return 0;
123 }
124 
125 int
127  bpo::variables_map const& vm,
128  fhicl::intermediate_table& raw_config)
129 {
130  try {
131  fhicl::parse_document(vm["config"].as<std::string>(), maker_, raw_config);
132  }
133  catch (cet::exception& e) {
134  std::cerr << "Failed to parse the configuration file '"
135  << vm["config"].as<std::string>() << "' with exception\n"
136  << e.what() << "\n";
137  return 90;
138  }
139  if (raw_config.empty()) {
140  std::cerr << "INFO: provided configuration file '"
141  << vm["config"].as<std::string>() << "' is empty: \n"
142  << "using minimal defaults and command-line options.\n";
143  }
144  if (vm.count("process-name")) {
145  raw_config.put("process_name", vm["process-name"].as<std::string>());
146  }
147  return 0;
148 }
Float_t s
Definition: plot.C:23
void add_opt(T &t, Args &&...args)
static std::string print()
Float_t ss
Definition: plot.C:23
int doCheckOptions(bpo::variables_map const &vm) override
shims::map< std::string, extended_value > table_t
std::string const & getReleaseVersion()
bool put(std::string const &name, std::string const &value, bool in_prolog=false)
void print_available_plugins(suffix_type st, bool const verbose, std::string const &spec)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bpo::options_description const & help_desc_
std::string value(boost::any const &)
void print_descriptions(std::vector< std::string > const &plugins)
static std::string const & get(suffix_type st)
BasicOptionsHandler(bpo::options_description &desc, cet::filepath_maker &maker)
constexpr int info_success()
Definition: info_success.h:9
cet::filepath_maker & maker_
Float_t e
Definition: plot.C:34
int doProcessOptions(bpo::variables_map const &vm, fhicl::intermediate_table &raw_config) override
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