CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends
optutl::CommandLineParser Class Reference

#include <CommandLineParser.h>

Inheritance diagram for optutl::CommandLineParser:
optutl::VariableMapCont

Public Types

enum  { kEventContOpt = 1 << 0 }
 
- Public Types inherited from optutl::VariableMapCont
typedef std::vector< double > DVec
 
typedef DVec::const_iterator DVecConstIter
 
typedef DVec::iterator DVecIter
 
typedef std::vector< int > IVec
 
typedef IVec::const_iterator IVecConstIter
 
typedef IVec::iterator IVecIter
 
enum  OptionType {
  kNone = 0, kInteger, kDouble, kString,
  kBool, kIntegerVector, kDoubleVector, kStringVector,
  kNumOptionTypes
}
 
typedef std::map< std::string, bool > SBMap
 
typedef SBMap::const_iterator SBMapConstIter
 
typedef SBMap::iterator SBMapIter
 
typedef std::map< std::string, double > SDMap
 
typedef SDMap::const_iterator SDMapConstIter
 
typedef SDMap::iterator SDMapIter
 
typedef std::map< std::string, DVecSDVecMap
 
typedef SDVecMap::const_iterator SDVecMapConstIter
 
typedef SDVecMap::iterator SDVecMapIter
 
typedef std::map< std::string, int > SIMap
 
typedef SIMap::const_iterator SIMapConstIter
 
typedef SIMap::iterator SIMapIter
 
typedef std::map< std::string, IVecSIVecMap
 
typedef SIVecMap::const_iterator SIVecMapConstIter
 
typedef SIVecMap::iterator SIVecMapIter
 
typedef std::map< std::string, std::string > SSMap
 
typedef SSMap::const_iterator SSMapConstIter
 
typedef SSMap::iterator SSMapIter
 
typedef std::map< std::string, SVecSSVecMap
 
typedef SSVecMap::const_iterator SSVecMapConstIter
 
typedef SSVecMap::iterator SSVecMapIter
 
typedef std::vector< std::string > SVec
 
typedef SVec::const_iterator SVecConstIter
 
typedef SVec::iterator SVecIter
 

Public Member Functions

void _finishDefaultOptions (std::string tag="")
 
const SVec argVec () const
 
 CommandLineParser (const std::string &usage, unsigned int optionsType=kEventContOpt)
 
void help ()
 
void parseArguments (int argc, char **argv, bool allowArgs=false)
 
void printOptionValues ()
 
void setPrintOptoins (bool print)
 
- Public Member Functions inherited from optutl::VariableMapCont
void _checkKey (std::string &key, const std::string &description="")
 
void addOption (std::string key, OptionType type, const std::string &description="")
 
void addOption (std::string key, OptionType type, const std::string &description, int defaultValue)
 
void addOption (std::string key, OptionType type, const std::string &description, double defaultValue)
 
void addOption (std::string key, OptionType type, const std::string &description, const std::string &defaultValue)
 
void addOption (std::string key, OptionType type, const std::string &description, const char *defaultValue)
 
void addOption (std::string key, OptionType type, const std::string &description, bool defaultValue)
 
void addVariable (std::string key, OptionType type)
 
void addVariable (std::string key, OptionType type, int defaultValue)
 
void addVariable (std::string key, OptionType type, double defaultValue)
 
void addVariable (std::string key, OptionType type, const std::string &defaultValue)
 
void addVariable (std::string key, OptionType type, const char *defaultValue)
 
void addVariable (std::string key, OptionType type, bool defaultValue)
 
bool & boolValue (std::string key)
 
double & doubleValue (std::string key)
 
DVecdoubleVector (std::string key)
 
OptionType hasOption (std::string key)
 
OptionType hasVariable (std::string key)
 
void help ()
 
int & integerValue (std::string key)
 
IVecintegerVector (std::string key)
 
std::string & stringValue (std::string key)
 
SVecstringVector (std::string key)
 
 VariableMapCont ()
 

Static Public Member Functions

static void findCommand (const std::string &line, std::string &command, std::string &rest)
 
static void removeComment (std::string &line)
 
static std::string removeEnding (const std::string &input, const std::string &ending)
 
static void removeLeadingAndTrailingSpaces (std::string &line)
 
static void split (SVec &retval, std::string line, std::string match=" \, bool ignoreComments=true)
 
- Static Public Member Functions inherited from optutl::VariableMapCont
static void lowercaseString (std::string &arg)
 
static char toLower (char &ch)
 

Static Public Attributes

static const std::string kSpaces = " \t"
 
- Static Public Attributes inherited from optutl::VariableMapCont
static const bool kDefaultBool = false
 
static const double kDefaultDouble = 0.
 
static const int kDefaultInteger = 0
 
static const std::string kDefaultString = ""
 
static const DVec kEmptyDVec
 
static const IVec kEmptyIVec
 
static const SVec kEmptySVec
 

Private Member Functions

void _getSectionFiles (const SVec &inputList, SVec &outputList, int section, int totalSection)
 
bool _runVariableCommandFromString (const std::string &arg)
 
bool _setVariableFromString (const std::string &arg, bool dontOverrideChange=false, int offset=0)
 
bool _setVariablesFromFile (const std::string &filename)
 

Private Attributes

std::string m_argv0
 
SVec m_fullArgVec
 
unsigned int m_optionsType
 
bool m_printOptions
 
std::string m_usageString
 

Friends

std::ostream & operator<< (std::ostream &o_stream, const CommandLineParser &rhs)
 

Additional Inherited Members

- Protected Member Functions inherited from optutl::VariableMapCont
bool _valueHasBeenModified (const std::string &key)
 
- Protected Attributes inherited from optutl::VariableMapCont
SBMap m_boolMap
 
SDMap m_doubleMap
 
SDVecMap m_doubleVecMap
 
SIMap m_integerMap
 
SIVecMap m_integerVecMap
 
SSMap m_stringMap
 
SSVecMap m_stringVecMap
 
SSMap m_variableDescriptionMap
 
SBMap m_variableModifiedMap
 

Detailed Description

Definition at line 10 of file CommandLineParser.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
kEventContOpt 

Definition at line 18 of file CommandLineParser.h.

Constructor & Destructor Documentation

◆ CommandLineParser()

CommandLineParser::CommandLineParser ( const std::string &  usage,
unsigned int  optionsType = kEventContOpt 
)

Definition at line 19 of file CommandLineParser.cc.

References optutl::VariableMapCont::addOption(), cms::cuda::assert(), DMR_cfg::cerr, optutl::VariableMapCont::kBool, kEventContOpt, optutl::VariableMapCont::kInteger, optutl::VariableMapCont::kString, optutl::VariableMapCont::kStringVector, and m_optionsType.

20  : m_argv0(""), m_usageString(usage), m_printOptions(true), m_optionsType(optionsType) {
22  // Integer options
23  addOption("totalSections", kInteger, "Total number of sections", 0);
24  addOption("section", kInteger, "This section (from 1..totalSections inclusive)", 0);
25  addOption("maxEvents", kInteger, "Maximum number of events to run over (0 for whole file)", 0);
26  addOption("jobID", kInteger, "jobID given by CRAB,etc. (-1 means append nothing)", -1);
27  addOption("outputEvery", kInteger, "Output something once every N events (0 for never)", 0);
28  // String options
29  addOption("outputFile", kString, "Output filename", "output.root");
30  addOption("storePrepend", kString, "Prepend location on files starting with '/store/'");
31  addOption("tag", kString, "A 'tag' to append to output file (e.g., 'v2', etc.)");
32  // Bool options
33  addOption("logName", kBool, "Print log name and exit");
34  // String vector options
35  addOption("inputFiles", kStringVector, "List of input files");
36  addOption("secondaryInputFiles", kStringVector, "List of secondary input files (a.k.a. two-file-solution");
37  addOption("orderedSecondaryFiles", kBool, "Are the secondary files ordered?", false);
38  return;
39  }
40  // If we're still here, we have a type I don't understand.
41  cerr << "CommandLineParser() Error: type '" << optionsType << "' is not understood. Aborting." << endl;
42  assert(0);
43 }
assert(be >=bs)
void addOption(std::string key, OptionType type, const std::string &description="")
void usage()
Definition: array2xmlEB.cc:14

Member Function Documentation

◆ _finishDefaultOptions()

void CommandLineParser::_finishDefaultOptions ( std::string  tag = "")

Definition at line 583 of file CommandLineParser.cc.

References _getSectionFiles(), optutl::VariableMapCont::boolValue(), gather_cfg::cout, beamvalidation::exit(), corrVsCorr::filename, optutl::VariableMapCont::integerValue(), kEventContOpt, m_optionsType, m_printOptions, download_sqlite_cfg::outputFile, printOptionValues(), removeEnding(), optutl::VariableMapCont::stringValue(), optutl::VariableMapCont::stringVector(), and makeGlobalPositionRcd_cfg::tag.

Referenced by fwlite::EventContainer::EventContainer().

583  {
584  if (!(m_optionsType & kEventContOpt)) {
585  // nothing to see here, folks
586  return;
587  }
589  // Deal with sections //
591  if (integerValue("totalSections")) {
592  // we have a request to break this into sections. First, note
593  // this in the output file
594  tag += Form("_sec%03d", integerValue("section"));
595  SVec tempVec;
596  _getSectionFiles(stringVector("inputFiles"), tempVec, integerValue("section"), integerValue("totalSections"));
597  stringVector("inputFiles") = tempVec;
598  } // if section requested
599 
601  // Store lfn to pfn //
603  const string &kStorePrepend = stringValue("storePrepend");
604  if (kStorePrepend.length()) {
605  string match = "/store/";
606  int matchLen = match.length();
607  SVec tempVec;
608  SVec &currentFiles = stringVector("inputFiles");
609  for (SVecConstIter iter = currentFiles.begin(); currentFiles.end() != iter; ++iter) {
610  const string &filename = *iter;
611  if ((int)filename.length() > matchLen && filename.substr(0, matchLen) == match) {
612  tempVec.push_back(kStorePrepend + filename);
613  } else {
614  tempVec.push_back(filename);
615  }
616  }
617  currentFiles = tempVec;
618  } // if storePrepend.
619 
621  // //////////////////////////// //
622  // // Modify output filename // //
623  // //////////////////////////// //
625  string outputFile = stringValue("outputFile");
626  bool modifyOutputFile = (outputFile.length());
627  outputFile = removeEnding(outputFile, ".root");
628  outputFile += tag;
629  if (integerValue("maxEvents")) {
630  outputFile += Form("_maxevt%03d", integerValue("maxEvents"));
631  }
632  if (integerValue("jobID") >= 0) {
633  outputFile += Form("_jobID%03d", integerValue("jobID"));
634  }
635  if (stringValue("tag").length()) {
636  outputFile += "_" + stringValue("tag");
637  }
638 
640  // Log File Name, if requested //
642  if (boolValue("logName")) {
643  cout << outputFile << ".log" << endl;
644  exit(0);
645  }
646  outputFile += ".root";
647  if (modifyOutputFile) {
648  stringValue("outputFile") = outputFile;
649  }
650 
651  // finally, if they asked us to print all options, let's do so
652  // after we've modified all variables appropriately
653  if (m_printOptions) {
655  } // if printOptions
656 }
void _getSectionFiles(const SVec &inputList, SVec &outputList, int section, int totalSection)
static std::string removeEnding(const std::string &input, const std::string &ending)
std::string & stringValue(std::string key)
std::vector< std::string > SVec
int & integerValue(std::string key)
SVec & stringVector(std::string key)
bool & boolValue(std::string key)
SVec::const_iterator SVecConstIter
def exit(msg="")

◆ _getSectionFiles()

void CommandLineParser::_getSectionFiles ( const SVec inputList,
SVec outputList,
int  section,
int  totalSection 
)
private

Definition at line 545 of file CommandLineParser.cc.

References cms::cuda::assert(), crabTemplate::inputList, createfilelist::int, heppy_loop::loop, EgammaValidation_cff::num, submitPVResolutionJobs::outputList, and hgcalPlots::section.

Referenced by _finishDefaultOptions().

545  {
546  // Make sure the segment numbers make sense
547  assert(section > 0 && section <= totalSections);
548 
549  // The Perl code:
550  // my $entries = @list;
551  // my $perSection = int ($entries / $totalSections);
552  // my $extra = $entries % $totalSections;
553  // --$section; # we want 0..n-1 not 1..n
554  // my $start = $perSection * $section;
555  // my $num = $perSection - 1;
556  // if ($section < $extra) {
557  // $start += $section;
558  // ++$num;
559  // } else {
560  // $start += $extra;
561  // };
562  // my $end = $start + $num;
563  int entries = (int)inputList.size();
564  int perSection = entries / totalSections;
565  int extra = entries % totalSections;
566  --section; // we want 0..n-1, not 1..n.
567  int current = perSection * section;
568  int num = perSection - 1;
569  if (section < extra) {
570  current += section;
571  ++num;
572  } else {
573  current += extra;
574  }
575  outputList.clear();
576  // we want to go from 0 to num inclusive, so make sure we have '<='
577  // and not '='
578  for (int loop = current; loop <= current + num; ++loop) {
579  outputList.push_back(inputList.at(loop));
580  } // for loop
581 }
assert(be >=bs)
string inputList
Definition: crabTemplate.py:6

◆ _runVariableCommandFromString()

bool CommandLineParser::_runVariableCommandFromString ( const std::string &  arg)
private

Definition at line 461 of file CommandLineParser.cc.

References cms::cuda::assert(), DMR_cfg::cerr, mps_check::command, optutl::VariableMapCont::doubleVector(), corrVsCorr::filename, optutl::VariableMapCont::hasOption(), recoMuon::in, optutl::VariableMapCont::integerVector(), optutl::VariableMapCont::kDoubleVector, submitPVResolutionJobs::key, optutl::VariableMapCont::kIntegerVector, kSpaces, optutl::VariableMapCont::kStringVector, mps_splice::line, optutl::VariableMapCont::lowercaseString(), source, split(), and optutl::VariableMapCont::stringVector().

Referenced by parseArguments().

461  {
462  SVec equalWords;
463  split(equalWords, arg, "=");
464  if (2 != equalWords.size()) {
465  return false;
466  }
467  SVec commandWords;
468  split(commandWords, equalWords.at(0), "_");
469  if (2 != commandWords.size()) {
470  return false;
471  }
472  string &command = commandWords.at(1);
474  if (command != "load" && command != "clear") {
475  return false;
476  }
477  const string &key = commandWords.at(0);
480  cerr << "Command '" << command << "' only works on vectors." << endl;
481  return false;
482  }
483 
485  // Clear //
487  if ("clear" == command) {
488  if (kIntegerVector == type) {
489  integerVector(key).clear();
490  } else if (kDoubleVector == type) {
491  doubleVector(key).clear();
492  } else if (kStringVector == type) {
493  stringVector(key).clear();
494  } else {
495  // If we're here, then I made a coding mistake and want to
496  // know about it.
497  assert(0);
498  }
499  return true;
500  }
501 
503  // Load //
505  const string &filename = equalWords.at(1);
506  ifstream source(filename.c_str(), ios::in);
507  if (!source) {
508  cerr << "file " << filename << "could not be opened" << endl;
509  return false;
510  }
511  string line;
512  while (getline(source, line)) {
513  // find the first nonspace
514  string::size_type where = line.find_first_not_of(kSpaces);
515  if (string::npos == where) {
516  // no non-spaces
517  continue;
518  }
519  // get rid of leading spaces
520  line = line.substr(where);
521  // get rid of trailing spaces
522  where = line.find_last_not_of(kSpaces);
523  if (line.length() - 1 != where) {
524  line = line.substr(0, where + 1);
525  }
526  if ('#' == line.at(0)) {
527  // this is a comment line, ignore it
528  continue;
529  }
530  if (kIntegerVector == type) {
531  integerVector(key).push_back((int)atof(line.c_str()));
532  } else if (kDoubleVector == type) {
533  doubleVector(key).push_back(atof(line.c_str()));
534  } else if (kStringVector == type) {
535  stringVector(key).push_back(line);
536  } else {
537  // If we're here, then I made a coding mistake and want to
538  // know about it.
539  assert(0);
540  }
541  } // while getline
542  return true;
543 }
static void split(SVec &retval, std::string line, std::string match=" \, bool ignoreComments=true)
std::vector< std::string > SVec
assert(be >=bs)
uint16_t size_type
A arg
Definition: Factorize.h:31
static const std::string kSpaces
DVec & doubleVector(std::string key)
key
prepare the HTCondor submission files and eventually submit them
static void lowercaseString(std::string &arg)
IVec & integerVector(std::string key)
SVec & stringVector(std::string key)
list command
Definition: mps_check.py:25
static std::string const source
Definition: EdmProvDump.cc:49
OptionType hasOption(std::string key)

◆ _setVariableFromString()

bool CommandLineParser::_setVariableFromString ( const std::string &  arg,
bool  dontOverrideChange = false,
int  offset = 0 
)
private

Definition at line 313 of file CommandLineParser.cc.

References optutl::VariableMapCont::_valueHasBeenModified(), cms::cuda::assert(), DMR_cfg::cerr, createfilelist::int, optutl::VariableMapCont::lowercaseString(), optutl::VariableMapCont::m_boolMap, optutl::VariableMapCont::m_doubleMap, optutl::VariableMapCont::m_doubleVecMap, optutl::VariableMapCont::m_integerMap, optutl::VariableMapCont::m_integerVecMap, optutl::VariableMapCont::m_stringMap, optutl::VariableMapCont::m_stringVecMap, optutl::VariableMapCont::m_variableModifiedMap, hltrates_dqm_sourceclient-live_cfg::offset, split(), and relativeConstraints::value.

Referenced by _setVariablesFromFile(), and parseArguments().

313  {
314  string::size_type where = arg.find_first_of('=', offset + 1);
315  string varname = arg.substr(offset, where - offset);
316  string value = arg.substr(where + 1);
317  lowercaseString(varname);
318  // check to make sure this is a valid option
319  SBMapConstIter sbiter = m_variableModifiedMap.find(varname);
320  if (m_variableModifiedMap.end() == sbiter) {
321  // Not found. Not a valid option
322  return false;
323  }
324  // if 'dontOverrideChange' is set, then we are being asked to NOT
325  // change any variables that have already been changed.
326  if (dontOverrideChange && _valueHasBeenModified(varname)) {
327  // don't go any further
328  return true;
329  }
330  // integers
331  SIMapIter integerIter = m_integerMap.find(varname);
332  if (m_integerMap.end() != integerIter) {
333  // we found it
334  // use 'atof' instead of 'atoi' to get scientific notation
335  integerIter->second = (int)atof(value.c_str());
336  m_variableModifiedMap[varname] = true;
337  return true;
338  }
339  // double
340  SDMapIter doubleIter = m_doubleMap.find(varname);
341  if (m_doubleMap.end() != doubleIter) {
342  // we found it
343  doubleIter->second = atof(value.c_str());
344  m_variableModifiedMap[varname] = true;
345  return true;
346  }
347  // string
348  SSMapIter stringIter = m_stringMap.find(varname);
349  if (m_stringMap.end() != stringIter) {
350  // we found it
351  stringIter->second = value;
352  m_variableModifiedMap[varname] = true;
353  return true;
354  }
355  // bool
356  SBMapIter boolIter = m_boolMap.find(varname);
357  if (m_boolMap.end() != boolIter) {
358  // we found it
359  boolIter->second = 0 != atoi(value.c_str());
360  m_variableModifiedMap[varname] = true;
361  return true;
362  }
363  // IntegerVec
364  SIVecMapIter integerVecIter = m_integerVecMap.find(varname);
365  if (m_integerVecMap.end() != integerVecIter) {
366  // we found it
367  SVec words;
368  split(words, value, ",");
369  for (SVecConstIter wordIter = words.begin(); words.end() != wordIter; ++wordIter) {
370  integerVecIter->second.push_back((int)atof(wordIter->c_str()));
371  }
372  // we don't want to mark this as modified because we can add
373  // many values to this
374  // m_variableModifiedMap[varname] = true;
375  return true;
376  }
377  // DoubleVec
378  SDVecMapIter doubleVecIter = m_doubleVecMap.find(varname);
379  if (m_doubleVecMap.end() != doubleVecIter) {
380  // we found it
381  SVec words;
382  split(words, value, ",");
383  for (SVecConstIter wordIter = words.begin(); words.end() != wordIter; ++wordIter) {
384  doubleVecIter->second.push_back(atof(wordIter->c_str()));
385  }
386  // we don't want to mark this as modified because we can add
387  // many values to this
388  // m_variableModifiedMap[varname] = true;
389  return true;
390  }
391  // StringVec
392  SSVecMapIter stringVecIter = m_stringVecMap.find(varname);
393  if (m_stringVecMap.end() != stringVecIter) {
394  // we found it
395  SVec words;
396  split(words, value, ",");
397  for (SVecConstIter wordIter = words.begin(); words.end() != wordIter; ++wordIter) {
398  stringVecIter->second.push_back(*wordIter);
399  }
400  // we don't want to mark this as modified because we can add
401  // many values to this
402  // m_variableModifiedMap[varname] = true;
403  return true;
404  }
405  // We didn't find your variable. And we really shouldn't be here
406  // because we should have know that we didn't find your variable.
407  cerr << "CommandLineParser::SetVeriableFromString() Error: "
408  << "Unknown variable and internal fault. Aborting." << endl;
409  assert(0);
410  return false;
411 }
SSMap::iterator SSMapIter
static void split(SVec &retval, std::string line, std::string match=" \, bool ignoreComments=true)
SBMap::iterator SBMapIter
SIMap::iterator SIMapIter
SBMap::const_iterator SBMapConstIter
SIVecMap::iterator SIVecMapIter
std::vector< std::string > SVec
assert(be >=bs)
uint16_t size_type
A arg
Definition: Factorize.h:31
static void lowercaseString(std::string &arg)
Definition: value.py:1
SSVecMap::iterator SSVecMapIter
SDVecMap::iterator SDVecMapIter
bool _valueHasBeenModified(const std::string &key)
SDMap::iterator SDMapIter
SVec::const_iterator SVecConstIter

◆ _setVariablesFromFile()

bool CommandLineParser::_setVariablesFromFile ( const std::string &  filename)
private

Definition at line 413 of file CommandLineParser.cc.

References _setVariableFromString(), DMR_cfg::cerr, beamvalidation::exit(), corrVsCorr::filename, dqmdumpme::first, recoMuon::in, createfilelist::int, kSpaces, mps_splice::line, position, and source.

413  {
414  ifstream source(filename.c_str(), ios::in);
415  if (!source) {
416  cerr << "file " << filename << "could not be opened" << endl;
417  return false;
418  }
419  string line;
420  while (getline(source, line)) {
421  // find the first nonspace
422  string::size_type where = line.find_first_not_of(kSpaces);
423  if (string::npos == where) {
424  // no non-spaces
425  continue;
426  }
427  char first = line.at(where);
428  if ('-' != first) {
429  continue;
430  }
431  where = line.find_first_not_of(kSpaces, where + 1);
432  if (string::npos == where) {
433  // no non-spaces
434  continue;
435  }
436  // Get string starting at first nonspace after '-'. Copy it to
437  // another string without copying any spaces and stopping at the
438  // first '#'.
439  string withspaces = line.substr(where);
440  string nospaces;
441  for (int position = 0; position < (int)withspaces.length(); ++position) {
442  char ch = withspaces[position];
443  if ('#' == ch) {
444  // start of a comment
445  break;
446  } else if (' ' == ch || '\t' == ch) {
447  continue;
448  }
449  nospaces += ch;
450  } // for position
451  if (!_setVariableFromString(nospaces, true)) {
452  cerr << "Don't understand line" << endl
453  << line << endl
454  << "in options file '" << filename << "'. Aborting." << endl;
455  exit(0);
456  } // if setting variable failed
457  } // while getline
458  return true;
459 }
uint16_t size_type
static const std::string kSpaces
bool _setVariableFromString(const std::string &arg, bool dontOverrideChange=false, int offset=0)
static int position[264][3]
Definition: ReadPGInfo.cc:289
static std::string const source
Definition: EdmProvDump.cc:49
def exit(msg="")

◆ argVec()

const SVec optutl::CommandLineParser::argVec ( ) const
inline

Definition at line 46 of file CommandLineParser.h.

References m_fullArgVec.

Referenced by fwlite::EventContainer::~EventContainer().

◆ findCommand()

void CommandLineParser::findCommand ( const std::string &  line,
std::string &  command,
std::string &  rest 
)
static

Definition at line 184 of file CommandLineParser.cc.

References mps_check::command, kSpaces, mps_splice::line, removeLeadingAndTrailingSpaces(), and mergeAndRegister::rest.

184  {
185  command = rest = "";
186  string::size_type nonspace = line.find_first_not_of(kSpaces);
187  if (string::npos == nonspace) {
188  // we don't have anything here at all. Just quit now
189  return;
190  }
191  string::size_type space = line.find_first_of(kSpaces, nonspace);
192  if (string::npos == space) {
193  // we only have a command and nothing else
194  command = line.substr(nonspace);
195  return;
196  }
197  command = line.substr(nonspace, space - nonspace);
198  rest = line.substr(space + 1);
200 }
static void removeLeadingAndTrailingSpaces(std::string &line)
uint16_t size_type
static const std::string kSpaces
list command
Definition: mps_check.py:25

◆ help()

void CommandLineParser::help ( )

Definition at line 104 of file CommandLineParser.cc.

References gather_cfg::cout, beamvalidation::exit(), m_argv0, m_usageString, and printOptionValues().

Referenced by parseArguments().

104  {
105  if (m_usageString.length()) {
106  cout << m_argv0 << " - " << m_usageString << endl;
107  }
108  cout << "--help - This screen" << endl
109  << "--noPrint - Do not print out all settings" << endl
110  << "--print - Print out all settings" << endl;
112  exit(0);
113 }
def exit(msg="")

◆ parseArguments()

void CommandLineParser::parseArguments ( int  argc,
char **  argv,
bool  allowArgs = false 
)

Definition at line 45 of file CommandLineParser.cc.

References _runVariableCommandFromString(), _setVariableFromString(), dir2webdir::argc, GCPpyPlots::argv, DMR_cfg::cerr, beamvalidation::exit(), dqmdumpme::first, help(), heppy_loop::loop, optutl::VariableMapCont::lowercaseString(), m_argv0, m_fullArgVec, and m_printOptions.

45  {
46  bool callHelp = false;
47  SVec argsVec;
48  m_argv0 = argv[0];
49  m_fullArgVec.push_back(argv[0]);
50  for (int loop = 1; loop < argc; ++loop) {
51  string arg = argv[loop];
52  m_fullArgVec.push_back(arg);
53  string::size_type where = arg.find_first_of('=');
54  if (string::npos != where) {
56  continue;
57  }
59  continue;
60  }
61  cerr << "Don't understand: " << arg << endl;
62  exit(0);
63  } // tag=value strings
64  else if (arg.at(0) == '-') {
65  string::size_type where = arg.find_first_not_of('-');
66  if (string::npos == where) {
67  // a poorly formed option
68  cerr << "Don't understand: " << arg << endl;
69  exit(0);
70  continue;
71  }
73  char first = arg.at(where);
74  // Print the values
75  if ('p' == first) {
76  m_printOptions = true;
77  continue;
78  }
79  if ('n' == first) {
80  m_printOptions = false;
81  continue;
82  }
83  // Exit after printing values
84  if ('h' == first) {
85  callHelp = true;
86  continue;
87  }
88  // if we're still here, then we've got a problem.
89  cerr << "Don't understand: " << arg << endl;
90  exit(0);
91  } // -arg strings
92  if (returnArgs) {
93  argsVec.push_back(arg);
94  } else {
95  cerr << "Don't understand: " << arg << endl;
96  exit(0);
97  }
98  } // for loop
99  if (callHelp) {
100  help();
101  }
102 }
bool _runVariableCommandFromString(const std::string &arg)
std::vector< std::string > SVec
uint16_t size_type
A arg
Definition: Factorize.h:31
static void lowercaseString(std::string &arg)
bool _setVariableFromString(const std::string &arg, bool dontOverrideChange=false, int offset=0)
def exit(msg="")

◆ printOptionValues()

void CommandLineParser::printOptionValues ( )

Definition at line 202 of file CommandLineParser.cc.

References gather_cfg::cout, makeListRunsInFiles::description, optutl::VariableMapCont::m_boolMap, optutl::VariableMapCont::m_doubleMap, optutl::VariableMapCont::m_doubleVecMap, optutl::VariableMapCont::m_integerMap, optutl::VariableMapCont::m_integerVecMap, optutl::VariableMapCont::m_stringMap, optutl::VariableMapCont::m_stringVecMap, and optutl::VariableMapCont::m_variableDescriptionMap.

Referenced by _finishDefaultOptions(), and help().

202  {
203  cout << "------------------------------------------------------------------" << left << endl;
204  // Print the integers next
205  if (!m_integerMap.empty()) {
206  cout << endl << "Integer options:" << endl;
207  }
208  for (SIMapConstIter iter = m_integerMap.begin(); m_integerMap.end() != iter; ++iter) {
209  const string &description = m_variableDescriptionMap[iter->first];
210  cout << " " << setw(14) << iter->first << " = " << setw(14) << iter->second;
211  if (description.length()) {
212  cout << " - " << description;
213  }
214  cout << endl;
215  } // for iter
216 
217  // Print the doubles next
218  if (!m_doubleMap.empty()) {
219  cout << endl << "Double options:" << endl;
220  }
221  for (SDMapConstIter iter = m_doubleMap.begin(); m_doubleMap.end() != iter; ++iter) {
222  const string &description = m_variableDescriptionMap[iter->first];
223  cout << " " << setw(14) << iter->first << " = " << setw(14) << iter->second;
224  if (description.length()) {
225  cout << " - " << description;
226  }
227  cout << endl;
228  } // for iter
229 
230  // Print the bools first
231  if (!m_boolMap.empty()) {
232  cout << endl << "Bool options:" << endl;
233  }
234  for (SBMapConstIter iter = m_boolMap.begin(); m_boolMap.end() != iter; ++iter) {
235  const string &description = m_variableDescriptionMap[iter->first];
236  cout << " " << setw(14) << iter->first << " = " << setw(14);
237  if (iter->second) {
238  cout << "true";
239  } else {
240  cout << "false";
241  }
242  if (description.length()) {
243  cout << " - " << description;
244  }
245  cout << endl;
246  } // for iter
247 
248  // Print the strings next
249  if (!m_stringMap.empty()) {
250  cout << endl << "String options:" << endl;
251  }
252  for (SSMapConstIter iter = m_stringMap.begin(); m_stringMap.end() != iter; ++iter) {
253  const string &description = m_variableDescriptionMap[iter->first];
254  cout << " " << setw(14) << iter->first << " = ";
255  const string value = "'" + iter->second + "'";
256  cout << setw(14) << "";
257  if (description.length()) {
258  cout << " - " << description;
259  }
260  cout << endl << " " << value << endl;
261  } // for iter
262 
263  // Integer Vec
264  if (!m_integerVecMap.empty()) {
265  cout << endl << "Integer Vector options:" << endl;
266  }
267  for (SIVecMapConstIter iter = m_integerVecMap.begin(); m_integerVecMap.end() != iter; ++iter) {
268  const string &description = m_variableDescriptionMap[iter->first];
269  cout << " " << setw(14) << iter->first << " = ";
270  dumpSTL(iter->second);
271  cout << endl;
272  if (description.length()) {
273  cout << " - " << description;
274  }
275  cout << endl;
276  } // for iter
277 
278  // Double Vec
279  if (!m_doubleVecMap.empty()) {
280  cout << endl << "Double Vector options:" << endl;
281  }
282  for (SDVecMapConstIter iter = m_doubleVecMap.begin(); m_doubleVecMap.end() != iter; ++iter) {
283  const string &description = m_variableDescriptionMap[iter->first];
284  cout << " " << setw(14) << iter->first << " = ";
285  dumpSTL(iter->second);
286  cout << endl;
287  if (description.length()) {
288  cout << " - " << description;
289  }
290  cout << endl;
291  } // for iter
292 
293  // String Vec
294  if (!m_stringVecMap.empty()) {
295  cout << endl << "String Vector options:" << endl;
296  } else {
297  cout << endl;
298  }
299  for (SSVecMapConstIter iter = m_stringVecMap.begin(); m_stringVecMap.end() != iter; ++iter) {
300  const string &description = m_variableDescriptionMap[iter->first];
301  cout << " " << setw(14) << iter->first << " = ";
302  if (description.length()) {
303  cout << setw(14) << ""
304  << " - " << description;
305  }
306  cout << endl;
307  dumpSTLeachEndl(iter->second, 8);
308  } // for iter
309 
310  cout << "------------------------------------------------------------------" << right << endl;
311 }
SIMap::const_iterator SIMapConstIter
SDMap::const_iterator SDMapConstIter
SBMap::const_iterator SBMapConstIter
SSVecMap::const_iterator SSVecMapConstIter
SSMap::const_iterator SSMapConstIter
SIVecMap::const_iterator SIVecMapConstIter
Definition: value.py:1
SDVecMap::const_iterator SDVecMapConstIter

◆ removeComment()

void CommandLineParser::removeComment ( std::string &  line)
static

Definition at line 149 of file CommandLineParser.cc.

References mps_splice::line, and EcalCondDBWriter_cfi::location.

Referenced by split().

149  {
150  string::size_type location = line.find('#');
151  if (string::npos != location) {
152  // we've got a comment. Strip it out
153  line = line.substr(0, location - 1);
154  } // if found
155 }
uint16_t size_type

◆ removeEnding()

string CommandLineParser::removeEnding ( const std::string &  input,
const std::string &  ending 
)
static

Definition at line 174 of file CommandLineParser.cc.

References input, and position.

Referenced by _finishDefaultOptions().

174  {
175  string::size_type position = input.rfind(ending);
176  if (input.length() - ending.length() == position) {
177  // we've got it
178  return input.substr(0, position);
179  }
180  // If we're still here, it wasn't there
181  return input;
182 }
uint16_t size_type
static std::string const input
Definition: EdmProvDump.cc:50
static int position[264][3]
Definition: ReadPGInfo.cc:289

◆ removeLeadingAndTrailingSpaces()

void CommandLineParser::removeLeadingAndTrailingSpaces ( std::string &  line)
static

Definition at line 157 of file CommandLineParser.cc.

References kSpaces, and mps_splice::line.

Referenced by findCommand().

157  {
158  string::size_type pos = line.find_first_not_of(kSpaces);
159  if (string::npos == pos) {
160  // we don't have anything here at all. Just quit now
161  return;
162  }
163  if (pos) {
164  // We have spaces at the beginning.
165  line = line.substr(pos);
166  }
167  pos = line.find_last_not_of(kSpaces);
168  if (pos + 1 != line.length()) {
169  // we've got spaces at the end
170  line = line.substr(0, pos + 1);
171  }
172 }
uint16_t size_type
static const std::string kSpaces

◆ setPrintOptoins()

void optutl::CommandLineParser::setPrintOptoins ( bool  print)
inline

Definition at line 43 of file CommandLineParser.h.

References m_printOptions, and print().

43 { m_printOptions = print; }
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ split()

void CommandLineParser::split ( SVec retval,
std::string  line,
std::string  match = " \t",
bool  ignoreComments = true 
)
static

Definition at line 115 of file CommandLineParser.cc.

References kSpaces, dqmdumpme::last, mps_splice::line, and removeComment().

Referenced by _runVariableCommandFromString(), and _setVariableFromString().

115  {
116  if (ignoreComments) {
118  } // if ignoreComments
119  retval.clear();
120  // find the first non-space
121  string::size_type start1 = line.find_first_not_of(kSpaces);
122  // Is the first non-space character a '#'
123  char firstCh = line[start1];
124  if ('#' == firstCh) {
125  // this line is a comment
126  return;
127  }
128 
129  line += match; // get last word of line
130  string::size_type last = string::npos;
131  string::size_type current = line.find_first_of(match);
132  while (string::npos != current) {
134  if (string::npos != last) {
135  pos = last + 1;
136  } else {
137  pos = 0;
138  }
139  string part = line.substr(pos, current - last - 1);
140  // don't bother adding 0 length strings
141  if (part.length()) {
142  retval.push_back(part);
143  }
144  last = current;
145  current = line.find_first_of(match, current + 1);
146  } // while we're finding spaces
147 }
uint16_t size_type
static const std::string kSpaces
static void removeComment(std::string &line)
part
Definition: HCALResponse.h:20

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  o_stream,
const CommandLineParser rhs 
)
friend

Member Data Documentation

◆ kSpaces

const std::string CommandLineParser::kSpaces = " \t"
static

◆ m_argv0

std::string optutl::CommandLineParser::m_argv0
private

Definition at line 115 of file CommandLineParser.h.

Referenced by help(), and parseArguments().

◆ m_fullArgVec

SVec optutl::CommandLineParser::m_fullArgVec
private

Definition at line 114 of file CommandLineParser.h.

Referenced by argVec(), and parseArguments().

◆ m_optionsType

unsigned int optutl::CommandLineParser::m_optionsType
private

Definition at line 118 of file CommandLineParser.h.

Referenced by _finishDefaultOptions(), and CommandLineParser().

◆ m_printOptions

bool optutl::CommandLineParser::m_printOptions
private

Definition at line 117 of file CommandLineParser.h.

Referenced by _finishDefaultOptions(), parseArguments(), and setPrintOptoins().

◆ m_usageString

std::string optutl::CommandLineParser::m_usageString
private

Definition at line 116 of file CommandLineParser.h.

Referenced by help().