CMS 3D CMS Logo

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<>
bool getValue (const std::string &name)
 
template<>
bool getValue (const std::string &name, bool default_value)
 
template<class T >
T getValue (const std::string &name, T 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

◆ OptionMap_t

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

Definition at line 56 of file Utilities.h.

◆ StrVec_t

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

Definition at line 57 of file Utilities.h.

Constructor & Destructor Documentation

◆ CommandLine()

CommandLine::CommandLine ( )

Definition at line 177 of file Utilities.h.

177 {}

◆ ~CommandLine()

CommandLine::~CommandLine ( )

Definition at line 179 of file Utilities.h.

179 {}

Member Function Documentation

◆ check()

bool CommandLine::check ( void  )

Definition at line 224 of file Utilities.h.

224  {
225  bool result = true;
226  OptionMap_t::const_iterator it;
227  for (it = _options.begin(); it != _options.end(); ++it) {
228  if (!it->second.second) {
229  std::cout << "CommandLine WARNING: unused option '" << it->first << "'!" << std::endl;
230  result = false;
231  }
232  }
233 
234  if (!_unknowns.empty()) {
235  result = false;
236  std::cout << "\nCommandLine WARNING: " << _unknowns.size()
237  << " the followingparameters *must* be provided:" << std::endl;
238  for (StrVec_t::const_iterator it = _unknowns.begin(); it != _unknowns.end(); ++it)
239  std::cout << (*it) << std::endl;
240  std::cout << std::endl;
241  }
242  return result;
243 }

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

◆ getValue() [1/4]

template<class T >
T CommandLine::getValue ( const std::string &  name)

Definition at line 74 of file Utilities.h.

74  {
75  T result = T();
76  OptionMap_t::iterator it = _options.find(name);
77  if (it != _options.end()) {
78  it->second.second = true;
79  _ordered_options.push_back(name);
80  std::stringstream ss;
81  ss << it->second.first;
82  ss >> result;
83  return result;
84  }
85  _unknowns.push_back(name);
86  return result;
87 }

References _options, _ordered_options, _unknowns, Skims_PA_cff::name, mps_fire::result, and contentValuesCheck::ss.

◆ getValue() [2/4]

template<>
bool CommandLine::getValue ( const std::string &  name)

Definition at line 106 of file Utilities.h.

106  {
107  OptionMap_t::iterator it = _options.find(name);
108  if (it != _options.end()) {
109  it->second.second = true;
110  _ordered_options.push_back(name);
111  std::string val_as_string = it->second.first;
112  if (val_as_string == "true")
113  return true;
114  if (val_as_string == "false")
115  return false;
116  int val_as_int;
117  std::stringstream ss;
118  ss << val_as_string;
119  ss >> val_as_int;
120  return val_as_int;
121  }
122  _unknowns.push_back(name);
123  return false;
124 }

References _options, _ordered_options, _unknowns, Skims_PA_cff::name, contentValuesCheck::ss, and AlCaHLTBitMon_QueryRunRegistry::string.

◆ getValue() [3/4]

template<>
bool CommandLine::getValue ( const std::string &  name,
bool  default_value 
)

Definition at line 128 of file Utilities.h.

128  {
129  OptionMap_t::const_iterator it = _options.find(name);
130  if (it != _options.end())
131  return getValue<bool>(name);
132  _options[name] = (default_value) ? std::make_pair("true", true) : std::make_pair("false", true);
133  _ordered_options.push_back(name);
134  return default_value;
135 }

References _options, _ordered_options, and Skims_PA_cff::name.

◆ getValue() [4/4]

template<class T >
T CommandLine::getValue ( const std::string &  name,
T  default_value 
)

Definition at line 91 of file Utilities.h.

91  {
92  OptionMap_t::const_iterator it = _options.find(name);
93  if (it != _options.end())
94  return getValue<T>(name);
95  std::string default_as_string;
96  std::stringstream ss;
97  ss << default_value;
98  ss >> default_as_string;
99  _options[name] = std::make_pair(default_as_string, true);
100  _ordered_options.push_back(name);
101  return default_value;
102 }

References _options, _ordered_options, Skims_PA_cff::name, contentValuesCheck::ss, and AlCaHLTBitMon_QueryRunRegistry::string.

◆ getVector() [1/2]

template<class T >
std::vector< T > CommandLine::getVector ( const std::string &  name)

Definition at line 139 of file Utilities.h.

139  {
140  std::vector<T> result;
141  OptionMap_t::iterator it = _options.find(name);
142  if (it != _options.end()) {
143  it->second.second = true;
144  _ordered_options.push_back(name);
145  std::string tmp = it->second.first;
147  if (!tmp.empty()) {
148  do {
149  pos = tmp.find(",");
150  std::stringstream ss;
151  ss << tmp.substr(0, pos);
152  tmp.erase(0, pos + 1);
153  T element;
154  ss >> element;
155  result.push_back(element);
156  } while (pos != std::string::npos);
157  }
158  } else {
159  _unknowns.push_back(name);
160  }
161  return result;
162 }

References _options, _ordered_options, _unknowns, Skims_PA_cff::name, mps_fire::result, contentValuesCheck::ss, AlCaHLTBitMon_QueryRunRegistry::string, and createJobs::tmp.

◆ getVector() [2/2]

template<class T >
std::vector< T > CommandLine::getVector ( const std::string &  name,
const std::string &  default_as_string 
)

Definition at line 166 of file Utilities.h.

166  {
167  OptionMap_t::iterator it = _options.find(name);
168  if (it == _options.end())
169  _options[name] = std::make_pair(default_as_string, false);
170  return getVector<T>(name);
171 }

References _options, and Skims_PA_cff::name.

◆ parse()

bool CommandLine::parse ( int  argc,
char **  argv 
)

Definition at line 184 of file Utilities.h.

184  {
185  _exe = argv[0];
186  _options.clear();
187  _ordered_options.clear();
188  _unknowns.clear();
189 
190  for (int i = 1; i < argc; i++) {
191  std::string opt = argv[i];
192  if (0 != opt.find("-")) {
193  if (i == 1) {
194  bool success = parse_file(opt);
195  if (!success)
196  return false;
197  continue;
198  } else {
199  std::cout << "CommandLine ERROR: options must start with '-'!" << std::endl;
200  return false;
201  }
202  }
203  opt.erase(0, 1);
204  std::string next = argv[i + 1];
205  if (/*0==next.find("-")||*/ i + 1 >= argc) {
206  std::cout << "ERROR: option '" << opt << "' requires value!" << std::endl;
207  return false;
208  }
209  _options[opt] = std::make_pair(next, false);
210  i++;
211  if (i < argc - 1) {
212  next = argv[i + 1];
213  while (next.find("-") != 0) {
214  _options[opt].first += "," + next;
215  i++;
216  next = (i < argc - 1) ? argv[i + 1] : "-";
217  }
218  }
219  }
220 
221  return true;
222 }

References _exe, _options, _ordered_options, _unknowns, dir2webdir::argc, cmsBatch::argv, gather_cfg::cout, mps_fire::i, GetRecoTauVFromDQM_MC_cff::next, runTheMatrix::opt, parse_file(), AlCaHLTBitMon_QueryRunRegistry::string, and summarizeEdmComparisonLogfiles::success.

◆ parse_file()

bool CommandLine::parse_file ( const std::string &  file_name)
private

Definition at line 277 of file Utilities.h.

277  {
278  std::ifstream fin(file_name.c_str());
279  if (!fin.is_open()) {
280  std::cout << "Can't open configuration file " << file_name << std::endl;
281  return false;
282  }
283 
284  std::stringstream ss;
285  bool filter(false);
286  while (!fin.eof()) {
287  char next;
288  fin.get(next);
289  if (!filter && next == '$')
290  filter = true;
291  if (!filter) {
292  if (next == '=')
293  ss << " " << next << " ";
294  else
295  ss << next;
296  }
297  if (filter && next == '\n')
298  filter = false;
299  }
300 
301  std::string token, last_token, key, value;
302  ss >> token;
303  while (!ss.eof()) {
304  if (token == "=") {
305  if (!key.empty() && !value.empty())
306  _options[key] = std::make_pair(value, false);
307  key = last_token;
308  last_token = "";
309  value = "";
310  } else if (!last_token.empty()) {
311  if (last_token.find("\"") == 0) {
312  if (last_token.rfind("\"") == last_token.length() - 1) {
313  last_token = last_token.substr(1, last_token.length() - 2);
314  value += (!value.empty()) ? "," + last_token : last_token;
315  last_token = token;
316  } else
317  last_token += " " + token;
318  } else {
319  value += (!value.empty()) ? "," + last_token : last_token;
320  last_token = (token == "=") ? "" : token;
321  }
322  } else
323  last_token = (token == "=") ? "" : token;
324  ss >> token;
325  }
326  if (!last_token.empty()) {
327  if (last_token.find("\"") == 0 && last_token.rfind("\"") == last_token.length() - 1)
328  last_token = last_token.substr(1, last_token.length() - 2);
329  value += (!value.empty()) ? "," + last_token : last_token;
330  }
331  if (!key.empty() && !value.empty())
332  _options[key] = std::make_pair(value, false);
333 
334  return true;
335 }

References _options, gather_cfg::cout, contentValuesCheck::file_name, ALCARECOTkAlBeamHalo_cff::filter, groupFilesInBlocks::fin, crabWrapper::key, GetRecoTauVFromDQM_MC_cff::next, contentValuesCheck::ss, AlCaHLTBitMon_QueryRunRegistry::string, unpackBuffers-CaloStage2::token, and relativeConstraints::value.

Referenced by parse().

◆ print()

void CommandLine::print ( void  )

Definition at line 245 of file Utilities.h.

245  {
246  std::cout << "------------------------------------------------------------" << std::endl;
247  std::cout << _exe << " options:" << std::endl;
248  std::cout << "------------------------------------------------------------" << std::endl;
249  for (StrVec_t::const_iterator itvec = _ordered_options.begin(); itvec != _ordered_options.end(); ++itvec) {
250  OptionMap_t::const_iterator it = _options.find(*itvec);
251  assert(it != _options.end());
252  if (it->second.first.find(",") < std::string::npos) {
253  std::string tmp = it->second.first;
254  std::string::size_type length = tmp.length();
256  do {
257  pos = tmp.find(",");
258  if (tmp.length() == length) {
259  std::cout << std::setiosflags(std::ios::left) << std::setw(22) << it->first
260  << std::resetiosflags(std::ios::left) << std::setw(3) << "=" << std::setiosflags(std::ios::right)
261  << std::setw(35) << tmp.substr(0, pos) << std::resetiosflags(std::ios::right) << std::endl;
262  } else {
263  std::cout << std::setiosflags(std::ios::right) << std::setw(60) << tmp.substr(0, pos)
264  << std::resetiosflags(std::ios::right) << std::endl;
265  }
266  tmp.erase(0, pos + 1);
267  } while (pos != std::string::npos);
268  } else {
269  std::cout << std::setiosflags(std::ios::left) << std::setw(22) << it->first << std::resetiosflags(std::ios::left)
270  << std::setw(3) << "=" << std::setiosflags(std::ios::right) << std::setw(35) << it->second.first
271  << std::resetiosflags(std::ios::right) << std::endl;
272  }
273  }
274  std::cout << "------------------------------------------------------------" << std::endl;
275 }

References _exe, _options, _ordered_options, cms::cuda::assert(), gather_cfg::cout, AlCaHLTBitMon_QueryRunRegistry::string, and createJobs::tmp.

Member Data Documentation

◆ _exe

std::string CommandLine::_exe
private

Definition at line 62 of file Utilities.h.

Referenced by parse(), and print().

◆ _options

OptionMap_t CommandLine::_options
private

Definition at line 63 of file Utilities.h.

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

◆ _ordered_options

StrVec_t CommandLine::_ordered_options
private

Definition at line 64 of file Utilities.h.

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

◆ _unknowns

StrVec_t CommandLine::_unknowns
private

Definition at line 65 of file Utilities.h.

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

CommandLine::_exe
std::string _exe
Definition: Utilities.h:62
CommandLine::_options
OptionMap_t _options
Definition: Utilities.h:63
cmsBatch.argv
argv
Definition: cmsBatch.py:279
mps_fire.i
i
Definition: mps_fire.py:355
CommandLine::_ordered_options
StrVec_t _ordered_options
Definition: Utilities.h:64
dir2webdir.argc
argc
Definition: dir2webdir.py:39
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pos
Definition: PixelAliasList.h:18
CommandLine::parse_file
bool parse_file(const std::string &file_name)
Definition: Utilities.h:277
cms::cuda::assert
assert(be >=bs)
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
runTheMatrix.opt
opt
Definition: runTheMatrix.py:280
contentValuesCheck.file_name
file_name
Definition: contentValuesCheck.py:38
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
groupFilesInBlocks.fin
fin
Definition: groupFilesInBlocks.py:94
summarizeEdmComparisonLogfiles.success
success
Definition: summarizeEdmComparisonLogfiles.py:115
ALCARECOTkAlBeamHalo_cff.filter
filter
Definition: ALCARECOTkAlBeamHalo_cff.py:27
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
CommandLine::_unknowns
StrVec_t _unknowns
Definition: Utilities.h:65
value
Definition: value.py:1
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
mps_fire.result
result
Definition: mps_fire.py:303
crabWrapper.key
key
Definition: crabWrapper.py:19
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:316