fastjet 2.4.5
Public Member Functions | Private Member Functions | Private Attributes
CmdLine Class Reference

Class designed to deal with command-line arguments in a fashion similar to what was done in f90 iolib. More...

#include <CmdLine.hh>

List of all members.

Public Member Functions

 CmdLine ()
 CmdLine (const int argc, char **argv)
 initialise a CmdLine from a C-style array of command-line arguments
 CmdLine (const vector< string > &args)
 initialise a CmdLine from a C++ vector of arguments
bool present (const string &opt) const
 true if the option is present
bool present_and_set (const string &opt) const
 true if the option is present and corresponds to a value
const vector< string > & arguments () const
 return a reference to the vector of command-line arguments (0 is command).
template<class T >
value (const string &opt) const
 returns the value of the argument converted to type T
template<class T >
value (const string &opt, const T &defval) const
int int_val (const string &opt) const
 return the integer value corresponding to the given option
int int_val (const string &opt, const int &defval) const
 return the integer value corresponding to the given option or default if option is absent
double double_val (const string &opt) const
 return the double value corresponding to the given option
double double_val (const string &opt, const double &defval) const
 return the double value corresponding to the given option or default if option is absent
string string_val (const string &opt) const
 return the string value corresponding to the given option
string string_val (const string &opt, const string &defval) const
 return the string value corresponding to the given option or default if option is absent
string command_line () const
 return the full command line
bool all_options_used () const
 return true if all options have been asked for at some point or other

Private Member Functions

void init ()
 builds the internal structures needed to keep track of arguments and options
void _report_conversion_failure (const string &opt, const string &optstring) const
 report failure of conversion

Private Attributes

map< string, int > __options
vector< string > __arguments
map< string, bool > __options_used
string __command_line

Detailed Description

Class designed to deal with command-line arguments in a fashion similar to what was done in f90 iolib.

Note that functionality might be slightly different? Currently do not implement access to arguments by index though data structure would in principle allow this quite easily.

GPS 03/01/05 [NB: wonder if some of this might be more efficiently written with templates for different type that can be read in...]

Other question: dealing with list of options is rather common occurrence -- command-line arguments, but also card files; maybe one could somehow use base/derived classes to share common functionality?

Definition at line 50 of file CmdLine.hh.


Constructor & Destructor Documentation

CmdLine::CmdLine ( ) [inline]

Definition at line 58 of file CmdLine.hh.

{};
CmdLine::CmdLine ( const int  argc,
char **  argv 
)

initialise a CmdLine from a C-style array of command-line arguments

Definition at line 41 of file CmdLine.cc.

                                             {
  __arguments.resize(argc);
  for(int iarg = 0; iarg < argc; iarg++){
    __arguments[iarg] = argv[iarg];
  }
  this->init();
}
CmdLine::CmdLine ( const vector< string > &  args)

initialise a CmdLine from a C++ vector of arguments

constructor from a vector of strings, one argument per string

Definition at line 50 of file CmdLine.cc.

                                             {
  __arguments = args;
  this->init();
}

Member Function Documentation

void CmdLine::_report_conversion_failure ( const string &  opt,
const string &  optstring 
) const [private]

report failure of conversion

Definition at line 186 of file CmdLine.cc.

                                                                         {
  cerr << "Error: could not convert option ("<<opt<<") value ("
       <<optstring<<") to requested type"<<endl; 
  exit(-1);
}
bool CmdLine::all_options_used ( ) const

return true if all options have been asked for at some point or other

Definition at line 173 of file CmdLine.cc.

Referenced by main().

                                     {
  bool result = true;
  for(map<string,bool>::const_iterator opt = __options_used.begin();
      opt != __options_used.end(); opt++) {
    bool this_one = opt->second;
    if (! this_one) {cerr << "Option "<<opt->first<<" unused/unrecognized"<<endl;}
    result = result && this_one;
  }
  return result;
}
const vector<string>& CmdLine::arguments ( ) const [inline]

return a reference to the vector of command-line arguments (0 is command).

Definition at line 71 of file CmdLine.hh.

{return __arguments;}
string CmdLine::command_line ( ) const

return the full command line

Definition at line 167 of file CmdLine.cc.

Referenced by print_jets().

                                   {
  return __command_line;
}
double CmdLine::double_val ( const string &  opt) const

return the double value corresponding to the given option

Definition at line 147 of file CmdLine.cc.

Referenced by main().

                                                   {
  double result;
  string optstring = string_val(opt);
  istringstream optstream(optstring);
  optstream >> result;
  if (optstream.fail()) {
    cerr << "Error: could not convert option ("<<opt<<") value ("
         <<optstring<<") to double"<<endl; 
    exit(-1);}
  return result;
}
double CmdLine::double_val ( const string &  opt,
const double &  defval 
) const

return the double value corresponding to the given option or default if option is absent

Definition at line 160 of file CmdLine.cc.

                                                                          {
  if (this->present_and_set(opt)) {return double_val(opt);} 
  else {return defval;}
}
void CmdLine::init ( ) [private]

builds the internal structures needed to keep track of arguments and options

Definition at line 56 of file CmdLine.cc.

                   {
  __command_line = "";
  for(size_t iarg = 0; iarg < __arguments.size(); iarg++){
    __command_line += __arguments[iarg];
    __command_line += " ";
  }
  
  // group things into options
  bool next_may_be_val = false;
  string currentopt;
  for(size_t iarg = 1; iarg < __arguments.size(); iarg++){
    // if expecting an option value, then take it (even if
    // it is actually next option...)
    if (next_may_be_val) {__options[currentopt] = iarg;}
    // now see if it might be an option itself
    string arg = __arguments[iarg];
    bool thisisopt = (arg.compare(0,1,"-") == 0);
    if (thisisopt) {
      // set option to a standard undefined value and say that 
      // we expect (possibly) a value on next round
      currentopt = arg;
      __options[currentopt] = -1;
      __options_used[currentopt] = false;
      next_may_be_val = true;}
    else {
      // otherwise throw away the argument for now...
      next_may_be_val = false;
      currentopt = "";
    }
  }
}
int CmdLine::int_val ( const string &  opt,
const int &  defval 
) const

return the integer value corresponding to the given option or default if option is absent

Definition at line 138 of file CmdLine.cc.

                                                                 {
  if (this->present_and_set(opt)) {return int_val(opt);} 
  else {return defval;}
}
int CmdLine::int_val ( const string &  opt) const

return the integer value corresponding to the given option

Definition at line 125 of file CmdLine.cc.

Referenced by main().

                                             {
  int result;
  string optstring = string_val(opt);
  istringstream optstream(optstring);
  optstream >> result;
  if (optstream.fail()) {
    cerr << "Error: could not convert option ("<<opt<<") value ("
         <<optstring<<") to int"<<endl; 
    exit(-1);}
  return result;
}
bool CmdLine::present ( const string &  opt) const

true if the option is present

Definition at line 89 of file CmdLine.cc.

Referenced by main().

                                              {
  bool result = (__options.find(opt) != __options.end());
  if (result) __options_used[opt] = true;
  return result;
}
bool CmdLine::present_and_set ( const string &  opt) const

true if the option is present and corresponds to a value

Definition at line 96 of file CmdLine.cc.

                                                      {
  bool result = present(opt) && __options[opt] > 0;
  return result;
}
string CmdLine::string_val ( const string &  opt) const

return the string value corresponding to the given option

Definition at line 103 of file CmdLine.cc.

                                                   {
  if (!this->present_and_set(opt)) {
    cerr << "Error: Option "<<opt
         <<" is needed but is not present_and_set"<<endl;
    exit(-1);
  }
  string arg = __arguments[__options[opt]];
  // this may itself look like an option -- if that is the case
  // declare the option to have been used
  if (arg.compare(0,1,"-") == 0) {__options_used[arg] = true;}
  return arg;
}
string CmdLine::string_val ( const string &  opt,
const string &  defval 
) const

return the string value corresponding to the given option or default if option is absent

Definition at line 117 of file CmdLine.cc.

                                                                          {
  if (this->present_and_set(opt)) {return string_val(opt);} 
  else {return defval;}
}
string CmdLine::value< string > ( const string &  opt) const [inline]

returns the value of the argument converted to type T

for the string case, just copy the string...

Definition at line 113 of file CmdLine.hh.

Referenced by main().

                                                           {
  T result;
  string optstring = string_val(opt);
  istringstream optstream(optstring);
  optstream >> result;
  if (optstream.fail()) _report_conversion_failure(opt, optstring);
  return result;
}
template<class T >
T CmdLine::value ( const string &  opt,
const T &  defval 
) const

Definition at line 128 of file CmdLine.hh.

                                                                             {
  if (this->present_and_set(opt)) {return value<T>(opt);} 
  else {return defval;}
}

Member Data Documentation

vector<string> CmdLine::__arguments [private]

Definition at line 52 of file CmdLine.hh.

string CmdLine::__command_line [private]

Definition at line 55 of file CmdLine.hh.

map<string,int> CmdLine::__options [mutable, private]

Definition at line 51 of file CmdLine.hh.

map<string,bool> CmdLine::__options_used [mutable, private]

Definition at line 53 of file CmdLine.hh.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines