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