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