CMS 3D CMS Logo

Public Member Functions | Private Types | Private Member Functions | Private Attributes

CommandLine Class Reference

#include <Utilities.h>

List of all members.

Public Member Functions

bool check ()
 CommandLine ()
template<>
bool getValue (const std::string &name, bool default_value)
template<class T >
T getValue (const std::string &name)
template<class T >
T getValue (const std::string &name, T default_value)
template<class T >
std::vector< TgetVector (const std::string &name, const std::string &default_as_string)
template<class T >
std::vector< TgetVector (const std::string &name)
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.

{
  
}
CommandLine::~CommandLine ( )

Definition at line 191 of file Utilities.h.

{
  
}

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().

{
  bool result = true;
  OptionMap_t::const_iterator it;
  for (it = _options.begin();it!=_options.end();++it) {
    if (!it->second.second) {
      std::cout<<"CommandLine WARNING: unused option '"<<it->first<<"'!"<<std::endl;
      result = false;
    }
  }
  
  if (_unknowns.size()>0) {
    result = false;
    std::cout<<"\nCommandLine WARNING: "<<_unknowns.size()
        <<" the followingparameters *must* be provided:"<<std::endl;
    for (StrVec_t::const_iterator it=_unknowns.begin();it!=_unknowns.end();++it)
      std::cout<<(*it)<<std::endl;
    std::cout<<std::endl;
  }
  return result;
}
bool CommandLine::getValue< bool > ( const std::string &  name)

Definition at line 74 of file Utilities.h.

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

Referenced by main().

{
  T result;
  OptionMap_t::iterator it=_options.find(name);
  if (it!=_options.end()) {
    it->second.second = true;
    _ordered_options.push_back(name);
    std::stringstream ss;
    ss<<it->second.first;
    ss>>result;
    return result;
  }
  _unknowns.push_back(name);
  return result;
}
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.

{
  OptionMap_t::const_iterator it=_options.find(name);
  if (it!=_options.end()) return getValue<bool>(name);
  _options[name] = (default_value) ?
    std::make_pair("true",true) : std::make_pair("false",true);
  _ordered_options.push_back(name);
  return default_value;
}
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.

{
  OptionMap_t::const_iterator it=_options.find(name);
  if (it!=_options.end()) return getValue<T>(name);
  std::string default_as_string;
  std::stringstream ss;
  ss<<default_value;
  ss>>default_as_string;
  _options[name] = std::make_pair(default_as_string,true);
  _ordered_options.push_back(name);
  return default_value;
}
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.

{
  OptionMap_t::iterator it=_options.find(name);
  if (it==_options.end()) _options[name] = std::make_pair(default_as_string,false);
  return getVector<T>(name);
}
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().

{
  std::vector<T> result;
  OptionMap_t::iterator it=_options.find(name);
  if (it!=_options.end()) {
    it->second.second = true;
    _ordered_options.push_back(name);
    std::string tmp=it->second.first;
    std::string::size_type pos;
    if (!tmp.empty()) {
      do {
        pos = tmp.find(",");
        std::stringstream ss;
        ss<<tmp.substr(0,pos);
        tmp.erase(0,pos+1);
        T element;
        ss>>element;
        result.push_back(element);
      }
      while (pos!=std::string::npos);
    }
  }
  else {
    _unknowns.push_back(name);
  }
  return result;
}
bool CommandLine::parse ( int  argc,
char **  argv 
)

Definition at line 199 of file Utilities.h.

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

Referenced by main().

{
  _exe = argv[0];
  _options.clear();
  _ordered_options.clear();
  _unknowns.clear();
  
  for (int i=1;i<argc;i++) {
    std::string opt=argv[i];
    if(0!=opt.find("-")) {
      if (i==1) {
        bool success = parse_file(opt);
        if (!success) return false;
        continue;
      }
      else {
        std::cout<<"CommandLine ERROR: options must start with '-'!"<<std::endl;
        return false;
      }
    }
    opt.erase(0,1);
    std::string next=argv[i+1];
    if (/*0==next.find("-")||*/i+1>=argc) {
      std::cout<<"ERROR: option '"<<opt<<"' requires value!"<<std::endl;
      return false;
    }
    _options[opt] = std::make_pair(next,false);
    i++;
    if (i<argc-1) {
      next=argv[i+1];
      while (next.find("-")!=0) {
        _options[opt].first += ","+next;
        i++;
        next = (i<argc-1) ? argv[i+1] : "-";
      }
    }
  }
  
  return true;
}
bool CommandLine::parse_file ( const std::string &  file_name) [private]

Definition at line 311 of file Utilities.h.

References _options, gather_cfg::cout, align_tpl::filter, combine::key, and relativeConstraints::value.

Referenced by parse().

{
  ifstream fin(file_name.c_str());
  if (!fin.is_open()) {
    std::cout<<"Can't open configuration file "<<file_name<<std::endl;
    return false;
  }

  std::stringstream ss;
  bool filter(false);
  while(!fin.eof()){
    char next;
    fin.get(next);
    if (!filter&&next=='$') filter=true;
    if(!filter) {
      if (next=='=') ss<<" "<<next<<" ";
      else ss<<next;
    }
    if (filter&&next=='\n') filter=false;
  }
  
  std::string token,last_token,key,value;
  ss>>token;
  while (!ss.eof()) {
    if (token=="=") {
      if (key!=""&&value!="") _options[key] = std::make_pair(value,false);
      key=last_token;
      last_token="";
      value="";
    }
    else if (last_token!="") {
      if (last_token.find("\"")==0) {
        if (last_token.rfind("\"")==last_token.length()-1) {
          last_token=last_token.substr(1,last_token.length()-2);
          value+=(value!="")?","+last_token:last_token;
          last_token=token;
        }
        else last_token+=" "+token;
      }
      else {
        value+=(value!="")?","+last_token:last_token;
        last_token=(token=="=")?"":token;
      }
    }
    else last_token=(token=="=")?"":token;
    ss>>token;
  }
  if (last_token!="") {
    if (last_token.find("\"")==0&&last_token.rfind("\"")==last_token.length()-1)
      last_token=last_token.substr(1,last_token.length()-2);
    value+=(value!="")?","+last_token:last_token;
  }
  if (key!=""&&value!="") _options[key] = std::make_pair(value,false);

  return true;
}
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().

{
  std::cout<<"------------------------------------------------------------"<<std::endl;
  std::cout<<_exe<<" options:"<<std::endl;
  std::cout<<"------------------------------------------------------------"<<std::endl;
  for (StrVec_t::const_iterator itvec=_ordered_options.begin();
       itvec!=_ordered_options.end();++itvec) {
    OptionMap_t::const_iterator it=_options.find(*itvec);
    assert(it!=_options.end());
    if (it->second.first.find(",")<std::string::npos) {
      std::string tmp=it->second.first;
      std::string::size_type length = tmp.length();
      std::string::size_type pos;
      do {
        pos = tmp.find(",");
        if (tmp.length()==length) {
          std::cout<<std::setiosflags(std::ios::left)<<std::setw(22)
              <<it->first
              <<std::resetiosflags(std::ios::left)
              <<std::setw(3)<<"="
              <<std::setiosflags(std::ios::right)<<std::setw(35)
              <<tmp.substr(0,pos)
              <<std::resetiosflags(std::ios::right)
              <<std::endl;
        }
        else {
          std::cout<<std::setiosflags(std::ios::right)<<std::setw(60)
              <<tmp.substr(0,pos)
              <<std::resetiosflags(std::ios::right)
              <<std::endl;
        }
        tmp.erase(0,pos+1);
      }
      while (pos!=std::string::npos);
    }
    else {
      std::cout<<std::setiosflags(std::ios::left)<<std::setw(22)
          <<it->first
          <<std::resetiosflags(std::ios::left)
          <<std::setw(3)<<"="
          <<std::setiosflags(std::ios::right)<<std::setw(35)
          <<it->second.first
          <<std::resetiosflags(std::ios::right)
          <<std::endl;
    }
  }
  std::cout<<"------------------------------------------------------------"<<std::endl;
}

Member Data Documentation

std::string CommandLine::_exe [private]

Definition at line 61 of file Utilities.h.

Referenced by parse(), and print().

Definition at line 62 of file Utilities.h.

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

Definition at line 63 of file Utilities.h.

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

Definition at line 64 of file Utilities.h.

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