CMS 3D CMS Logo

Options.cc
Go to the documentation of this file.
1 #include <cstdlib>
2 
3 #include <iostream>
4 #include <iomanip>
5 #include "boost/filesystem.hpp"
6 #include "boost/version.hpp"
7 #include "boost/program_options.hpp"
8 
9 #include "Options.h"
10 
11 using namespace std;
12 namespace fs = boost::filesystem;
13 namespace po = boost::program_options; // used to read the options from command line
14 
15 namespace AllInOneConfig {
16 
17  void check_file(string file) {
18  // check that the config file exists
19  if (!fs::exists(file)) {
20  cerr << "Didn't find configuration file " << file << '\n';
21  exit(EXIT_FAILURE);
22  }
23  }
24 
25  void set_verbose(bool flag) {
26  if (!flag)
27  cout.setstate(ios_base::failbit);
28  }
29 
30  void set_silent(bool flag) {
31  if (flag)
32  cerr.setstate(ios_base::failbit);
33  }
34 
40  Options::Options(bool getter) : help{"Helper"}, desc{"Options"}, hide{"Hidden"} {
41  // define all options
42  help.add_options()("help,h", "Help screen")("example,e", "Print example of config in JSON format")(
43  "tutorial,t", "Explain how to use the command");
44 
45  // then (except for getJSON) the running options
46  if (!getter)
47  desc.add_options()(
48  "dry,d", po::bool_switch(&dry)->default_value(false), "Set up everything, but don't run anything")(
49  "verbose,v", po::bool_switch()->default_value(false)->notifier(set_verbose), "Enable standard output stream")(
50  "silent,s", po::bool_switch()->default_value(false)->notifier(set_silent), "Disable standard error stream");
51 
52  // and finally / most importantly, the config file as apositional argument
53  hide.add_options()("config,c", po::value<string>(&config)->required()->notifier(check_file), "JSON config file");
54  pos_hide.add("config", 1); // 1 means one (positional) argument
55  if (getter) {
56  // in case of getIMFO, adding a second positional argument for the key
57  hide.add_options()("key,k", po::value<string>(&key)->required(), "key");
58  pos_hide.add("key", 1);
59  }
60  }
61 
62  void Options::helper(int argc, char* argv[]) {
63  po::options_description options;
64  options.add(help).add(desc);
65 
66  po::command_line_parser parser_helper{argc, argv};
67  parser_helper
68  .options(help) // don't parse required options before parsing help
69  .allow_unregistered(); // ignore unregistered options,
70 
71  // defines actions
72  po::variables_map vm;
73  po::store(parser_helper.run(), vm);
74  po::notify(vm); // necessary for config to be given the value from the cmd line
75 
76  if (vm.count("help")) {
77  fs::path executable = argv[0];
78  cout << "Basic syntax:\n " << executable.filename().string() << " config.JSON\n"
79  << options << '\n'
80  << "Boost " << BOOST_LIB_VERSION << endl;
81  exit(EXIT_SUCCESS);
82  }
83 
84  if (vm.count("example")) {
85  static const char *bold = "\e[1m", *normal = "\e[0m";
86  cout << bold << "Example of HTC condor config:" << normal << '\n' << endl;
87  system("cat $CMSSW_BASE/src/Alignment/OfflineValidation/bin/.default.sub");
88  cout << '\n' << bold << "Example of JSON config (for `validateAlignment` only):" << normal << '\n' << endl;
89  system("cat $CMSSW_BASE/src/Alignment/OfflineValidation/bin/.example.JSON");
90  cout << '\n'
91  << bold << "NB: " << normal
92  << " for examples of inputs to GCPs, DMRs, etc., just make a dry run of the example" << endl;
93  exit(EXIT_SUCCESS);
94  }
95  // TODO: make examples for each executables? (could be examples used in release validation...)
96 
97  if (vm.count("tutorial")) {
98  cout << " ______________________\n"
99  << " < Oops! not ready yet! >\n"
100  << " ----------------------\n"
101  << " \\ ^__^\n"
102  << " \\ (oo)\\_______\n"
103  << " (__)\\ )\\/\\\n"
104  << " ||----w |\n"
105  << " || ||\n"
106  << flush;
107  exit(EXIT_SUCCESS);
108  }
109  }
110 
111  void Options::parser(int argc, char* argv[]) {
112  po::options_description cmd_line;
113  cmd_line.add(desc).add(hide);
114 
115  po::command_line_parser parser{argc, argv};
116  parser.options(cmd_line).positional(pos_hide);
117 
118  po::variables_map vm;
119  po::store(parser.run(), vm);
120  po::notify(vm); // necessary for config to be given the value from the cmd line
121  }
122 
123 } // end of namespace AllInOneConfig
static const char * normal
Definition: DMRtrends.cc:35
void check_file(string file)
Definition: Options.cc:17
void set_verbose(bool flag)
Definition: Options.cc:25
boost::program_options::options_description hide
Definition: Options.h:9
boost::program_options::options_description help
Definition: Options.h:9
std::vector< std::shared_ptr< fireworks::OptionNode > > Options
key
prepare the HTCondor submission files and eventually submit them
boost::program_options::options_description desc
Definition: Options.h:9
void set_silent(bool flag)
Definition: Options.cc:30
bool check_file(std::string const &file)
Definition: hltDiff.cc:1224
static const char * bold
Definition: DMRtrends.cc:35
dictionary config
Read in AllInOne config in JSON format.
Definition: DiMuonV_cfg.py:29
void helper(int argc, char *argv[])
Definition: Options.cc:62
void parser(int argc, char *argv[])
Definition: Options.cc:111
boost::program_options::positional_options_description pos_hide
Definition: Options.h:10
def exit(msg="")