CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Types | Private Member Functions | Private Attributes
CommandLine Class Reference

#include <Utilities.h>

Public Member Functions

bool check ()
 
 CommandLine ()
 
template<class T >
T getValue (const std::string &name)
 
template<class T >
T getValue (const std::string &name, T default_value)
 
template<>
bool getValue (const std::string &name)
 
template<>
bool getValue (const std::string &name, bool default_value)
 
template<class T >
std::vector< TgetVector (const std::string &name)
 
template<class T >
std::vector< TgetVector (const std::string &name, const std::string &default_as_string)
 
bool parse (int argc, char **argv)
 
void print ()
 
 ~CommandLine ()
 

Private Types

typedef std::map< std::string,
std::pair< std::string, bool > > 
OptionMap_t
 
typedef std::vector< std::string > StrVec_t
 

Private Member Functions

bool parse_file (const std::string &file_name)
 

Private Attributes

std::string _exe
 
OptionMap_t _options
 
StrVec_t _ordered_options
 
StrVec_t _unknowns
 

Detailed Description

Definition at line 24 of file Utilities.h.

Member Typedef Documentation

typedef std::map<std::string,std::pair<std::string,bool> > CommandLine::OptionMap_t
private

Definition at line 54 of file Utilities.h.

typedef std::vector<std::string> CommandLine::StrVec_t
private

Definition at line 55 of file Utilities.h.

Constructor & Destructor Documentation

CommandLine::CommandLine ( )

Definition at line 186 of file Utilities.h.

187 {
188 
189 }
CommandLine::~CommandLine ( )

Definition at line 191 of file Utilities.h.

192 {
193 
194 }

Member Function Documentation

bool CommandLine::check ( void  )

Definition at line 240 of file Utilities.h.

References _options, _unknowns, gather_cfg::cout, and query::result.

Referenced by main().

241 {
242  bool result = true;
243  OptionMap_t::const_iterator it;
244  for (it = _options.begin();it!=_options.end();++it) {
245  if (!it->second.second) {
246  std::cout<<"CommandLine WARNING: unused option '"<<it->first<<"'!"<<std::endl;
247  result = false;
248  }
249  }
250 
251  if (_unknowns.size()>0) {
252  result = false;
253  std::cout<<"\nCommandLine WARNING: "<<_unknowns.size()
254  <<" the followingparameters *must* be provided:"<<std::endl;
255  for (StrVec_t::const_iterator it=_unknowns.begin();it!=_unknowns.end();++it)
256  std::cout<<(*it)<<std::endl;
257  std::cout<<std::endl;
258  }
259  return result;
260 }
OptionMap_t _options
Definition: Utilities.h:62
tuple result
Definition: query.py:137
StrVec_t _unknowns
Definition: Utilities.h:64
tuple cout
Definition: gather_cfg.py:121
template<class T >
T CommandLine::getValue ( const std::string &  name)

Definition at line 74 of file Utilities.h.

References _options, _ordered_options, _unknowns, and query::result.

Referenced by main().

75 {
76  T result;
77  OptionMap_t::iterator it=_options.find(name);
78  if (it!=_options.end()) {
79  it->second.second = true;
80  _ordered_options.push_back(name);
81  std::stringstream ss;
82  ss<<it->second.first;
83  ss>>result;
84  return result;
85  }
86  _unknowns.push_back(name);
87  return result;
88 }
OptionMap_t _options
Definition: Utilities.h:62
tuple result
Definition: query.py:137
StrVec_t _unknowns
Definition: Utilities.h:64
long double T
StrVec_t _ordered_options
Definition: Utilities.h:63
template<class T >
T CommandLine::getValue ( const std::string &  name,
T  default_value 
)

Definition at line 93 of file Utilities.h.

References _options, _ordered_options, and mergeVDriftHistosByStation::name.

94 {
95  OptionMap_t::const_iterator it=_options.find(name);
96  if (it!=_options.end()) return getValue<T>(name);
97  std::string default_as_string;
98  std::stringstream ss;
99  ss<<default_value;
100  ss>>default_as_string;
101  _options[name] = std::make_pair(default_as_string,true);
102  _ordered_options.push_back(name);
103  return default_value;
104 }
OptionMap_t _options
Definition: Utilities.h:62
StrVec_t _ordered_options
Definition: Utilities.h:63
template<>
bool CommandLine::getValue ( const std::string &  name)

Definition at line 109 of file Utilities.h.

References mergeVDriftHistosByStation::name.

110 {
111  OptionMap_t::iterator it=_options.find(name);
112  if (it!=_options.end()) {
113  it->second.second = true;
114  _ordered_options.push_back(name);
115  std::string val_as_string = it->second.first;
116  if (val_as_string=="true") return true;
117  if (val_as_string=="false") return false;
118  int val_as_int;
119  std::stringstream ss;
120  ss<<val_as_string;
121  ss>>val_as_int;
122  return val_as_int;
123  }
124  _unknowns.push_back(name);
125  return false;
126 }
OptionMap_t _options
Definition: Utilities.h:62
StrVec_t _unknowns
Definition: Utilities.h:64
StrVec_t _ordered_options
Definition: Utilities.h:63
template<>
bool CommandLine::getValue ( const std::string &  name,
bool  default_value 
)

Definition at line 131 of file Utilities.h.

References _options, _ordered_options, and mergeVDriftHistosByStation::name.

132 {
133  OptionMap_t::const_iterator it=_options.find(name);
134  if (it!=_options.end()) return getValue<bool>(name);
135  _options[name] = (default_value) ?
136  std::make_pair("true",true) : std::make_pair("false",true);
137  _ordered_options.push_back(name);
138  return default_value;
139 }
OptionMap_t _options
Definition: Utilities.h:62
StrVec_t _ordered_options
Definition: Utilities.h:63
template<class T >
std::vector< T > CommandLine::getVector ( const std::string &  name)

Definition at line 144 of file Utilities.h.

References _options, _ordered_options, _unknowns, pos, query::result, and tmp.

Referenced by main().

145 {
146  std::vector<T> result;
147  OptionMap_t::iterator it=_options.find(name);
148  if (it!=_options.end()) {
149  it->second.second = true;
150  _ordered_options.push_back(name);
151  std::string tmp=it->second.first;
153  if (!tmp.empty()) {
154  do {
155  pos = tmp.find(",");
156  std::stringstream ss;
157  ss<<tmp.substr(0,pos);
158  tmp.erase(0,pos+1);
159  T element;
160  ss>>element;
161  result.push_back(element);
162  }
163  while (pos!=std::string::npos);
164  }
165  }
166  else {
167  _unknowns.push_back(name);
168  }
169  return result;
170 }
OptionMap_t _options
Definition: Utilities.h:62
uint16_t size_type
tuple result
Definition: query.py:137
StrVec_t _unknowns
Definition: Utilities.h:64
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
long double T
StrVec_t _ordered_options
Definition: Utilities.h:63
template<class T >
std::vector< T > CommandLine::getVector ( const std::string &  name,
const std::string &  default_as_string 
)

Definition at line 174 of file Utilities.h.

References _options, and mergeVDriftHistosByStation::name.

176 {
177  OptionMap_t::iterator it=_options.find(name);
178  if (it==_options.end()) _options[name] = std::make_pair(default_as_string,false);
179  return getVector<T>(name);
180 }
OptionMap_t _options
Definition: Utilities.h:62
bool CommandLine::parse ( int  argc,
char **  argv 
)

Definition at line 199 of file Utilities.h.

References _exe, _options, _ordered_options, _unknowns, dir2webdir::argc, gather_cfg::cout, i, GetRecoTauVFromDQM_MC_cff::next, parse_file(), and summarizeEdmComparisonLogfiles::success.

Referenced by main().

200 {
201  _exe = argv[0];
202  _options.clear();
203  _ordered_options.clear();
204  _unknowns.clear();
205 
206  for (int i=1;i<argc;i++) {
207  std::string opt=argv[i];
208  if(0!=opt.find("-")) {
209  if (i==1) {
210  bool success = parse_file(opt);
211  if (!success) return false;
212  continue;
213  }
214  else {
215  std::cout<<"CommandLine ERROR: options must start with '-'!"<<std::endl;
216  return false;
217  }
218  }
219  opt.erase(0,1);
220  std::string next=argv[i+1];
221  if (/*0==next.find("-")||*/i+1>=argc) {
222  std::cout<<"ERROR: option '"<<opt<<"' requires value!"<<std::endl;
223  return false;
224  }
225  _options[opt] = std::make_pair(next,false);
226  i++;
227  if (i<argc-1) {
228  next=argv[i+1];
229  while (next.find("-")!=0) {
230  _options[opt].first += ","+next;
231  i++;
232  next = (i<argc-1) ? argv[i+1] : "-";
233  }
234  }
235  }
236 
237  return true;
238 }
int i
Definition: DBlmapReader.cc:9
std::string _exe
Definition: Utilities.h:61
OptionMap_t _options
Definition: Utilities.h:62
StrVec_t _unknowns
Definition: Utilities.h:64
tuple argc
Definition: dir2webdir.py:41
tuple cout
Definition: gather_cfg.py:121
bool parse_file(const std::string &file_name)
Definition: Utilities.h:311
StrVec_t _ordered_options
Definition: Utilities.h:63
bool CommandLine::parse_file ( const std::string &  file_name)
private

Definition at line 311 of file Utilities.h.

References _options, gather_cfg::cout, alcazmumu_cfi::filter, groupFilesInBlocks::fin, combine::key, GetRecoTauVFromDQM_MC_cff::next, and relativeConstraints::value.

Referenced by parse().

312 {
313  ifstream fin(file_name.c_str());
314  if (!fin.is_open()) {
315  std::cout<<"Can't open configuration file "<<file_name<<std::endl;
316  return false;
317  }
318 
319  std::stringstream ss;
320  bool filter(false);
321  while(!fin.eof()){
322  char next;
323  fin.get(next);
324  if (!filter&&next=='$') filter=true;
325  if(!filter) {
326  if (next=='=') ss<<" "<<next<<" ";
327  else ss<<next;
328  }
329  if (filter&&next=='\n') filter=false;
330  }
331 
332  std::string token,last_token,key,value;
333  ss>>token;
334  while (!ss.eof()) {
335  if (token=="=") {
336  if (key!=""&&value!="") _options[key] = std::make_pair(value,false);
337  key=last_token;
338  last_token="";
339  value="";
340  }
341  else if (last_token!="") {
342  if (last_token.find("\"")==0) {
343  if (last_token.rfind("\"")==last_token.length()-1) {
344  last_token=last_token.substr(1,last_token.length()-2);
345  value+=(value!="")?","+last_token:last_token;
346  last_token=token;
347  }
348  else last_token+=" "+token;
349  }
350  else {
351  value+=(value!="")?","+last_token:last_token;
352  last_token=(token=="=")?"":token;
353  }
354  }
355  else last_token=(token=="=")?"":token;
356  ss>>token;
357  }
358  if (last_token!="") {
359  if (last_token.find("\"")==0&&last_token.rfind("\"")==last_token.length()-1)
360  last_token=last_token.substr(1,last_token.length()-2);
361  value+=(value!="")?","+last_token:last_token;
362  }
363  if (key!=""&&value!="") _options[key] = std::make_pair(value,false);
364 
365  return true;
366 }
OptionMap_t _options
Definition: Utilities.h:62
list key
Definition: combine.py:13
tuple cout
Definition: gather_cfg.py:121
void CommandLine::print ( void  )

Definition at line 262 of file Utilities.h.

References _exe, _options, _ordered_options, gather_cfg::cout, pos, and tmp.

Referenced by main().

263 {
264  std::cout<<"------------------------------------------------------------"<<std::endl;
265  std::cout<<_exe<<" options:"<<std::endl;
266  std::cout<<"------------------------------------------------------------"<<std::endl;
267  for (StrVec_t::const_iterator itvec=_ordered_options.begin();
268  itvec!=_ordered_options.end();++itvec) {
269  OptionMap_t::const_iterator it=_options.find(*itvec);
270  assert(it!=_options.end());
271  if (it->second.first.find(",")<std::string::npos) {
272  std::string tmp=it->second.first;
273  std::string::size_type length = tmp.length();
275  do {
276  pos = tmp.find(",");
277  if (tmp.length()==length) {
278  std::cout<<std::setiosflags(std::ios::left)<<std::setw(22)
279  <<it->first
280  <<std::resetiosflags(std::ios::left)
281  <<std::setw(3)<<"="
282  <<std::setiosflags(std::ios::right)<<std::setw(35)
283  <<tmp.substr(0,pos)
284  <<std::resetiosflags(std::ios::right)
285  <<std::endl;
286  }
287  else {
288  std::cout<<std::setiosflags(std::ios::right)<<std::setw(60)
289  <<tmp.substr(0,pos)
290  <<std::resetiosflags(std::ios::right)
291  <<std::endl;
292  }
293  tmp.erase(0,pos+1);
294  }
295  while (pos!=std::string::npos);
296  }
297  else {
298  std::cout<<std::setiosflags(std::ios::left)<<std::setw(22)
299  <<it->first
300  <<std::resetiosflags(std::ios::left)
301  <<std::setw(3)<<"="
302  <<std::setiosflags(std::ios::right)<<std::setw(35)
303  <<it->second.first
304  <<std::resetiosflags(std::ios::right)
305  <<std::endl;
306  }
307  }
308  std::cout<<"------------------------------------------------------------"<<std::endl;
309 }
std::string _exe
Definition: Utilities.h:61
OptionMap_t _options
Definition: Utilities.h:62
uint16_t size_type
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
tuple cout
Definition: gather_cfg.py:121
StrVec_t _ordered_options
Definition: Utilities.h:63

Member Data Documentation

std::string CommandLine::_exe
private

Definition at line 61 of file Utilities.h.

Referenced by parse(), and print().

OptionMap_t CommandLine::_options
private

Definition at line 62 of file Utilities.h.

Referenced by check(), getValue(), getVector(), parse(), parse_file(), and print().

StrVec_t CommandLine::_ordered_options
private

Definition at line 63 of file Utilities.h.

Referenced by getValue(), getVector(), parse(), and print().

StrVec_t CommandLine::_unknowns
private

Definition at line 64 of file Utilities.h.

Referenced by check(), getValue(), getVector(), and parse().