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)
 return the integer value corresponding to the given option
int int_val (const string &opt, const int &defval)
 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.

00058 {};

CmdLine::CmdLine ( const int  argc,
char **  argv 
)

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

Definition at line 40 of file CmdLine.cc.

References __arguments, and init().

00040                                              {
00041   __arguments.resize(argc);
00042   for(int iarg = 0; iarg < argc; iarg++){
00043     __arguments[iarg] = argv[iarg];
00044   }
00045   this->init();
00046 }

CmdLine::CmdLine ( const vector< string > &  args  ) 

initialise a CmdLine from a C++ vector of arguments

Definition at line 49 of file CmdLine.cc.

References __arguments, and init().

00049                                              {
00050   __arguments = args;
00051   this->init();
00052 }


Member Function Documentation

bool CmdLine::present ( const string &  opt  )  const

true if the option is present

Definition at line 88 of file CmdLine.cc.

References __options, and __options_used.

Referenced by main(), and present_and_set().

00088                                               {
00089   bool result = (__options.find(opt) != __options.end());
00090   if (result) __options_used[opt] = true;
00091   return result;
00092 }

bool CmdLine::present_and_set ( const string &  opt  )  const

true if the option is present and corresponds to a value

Definition at line 95 of file CmdLine.cc.

References __options, and present().

00095                                                       {
00096   bool result = present(opt) && __options[opt] > 0;
00097   return result;
00098 }

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.

References __arguments.

00071 {return __arguments;}

string CmdLine::value< string > ( const string &  opt  )  const [inline]

returns the value of the argument converted to type T

Definition at line 113 of file CmdLine.hh.

References _report_conversion_failure(), and string_val().

Referenced by main().

00113                                                            {
00114   T result;
00115   string optstring = string_val(opt);
00116   istringstream optstream(optstring);
00117   optstream >> result;
00118   if (optstream.fail()) _report_conversion_failure(opt, optstring);
00119   return result;
00120 }

template<class T>
T CmdLine::value ( const string &  opt,
const T &  defval 
) const [inline]

Definition at line 128 of file CmdLine.hh.

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

int CmdLine::int_val ( const string &  opt  ) 

return the integer value corresponding to the given option

Definition at line 124 of file CmdLine.cc.

References string_val().

Referenced by int_val(), and main().

00124                                        {
00125   int result;
00126   string optstring = string_val(opt);
00127   istringstream optstream(optstring);
00128   optstream >> result;
00129   if (optstream.fail()) {
00130     cerr << "Error: could not convert option ("<<opt<<") value ("
00131          <<optstring<<") to int"<<endl; 
00132     exit(-1);}
00133   return result;
00134 }

int CmdLine::int_val ( const string &  opt,
const int &  defval 
)

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

Definition at line 137 of file CmdLine.cc.

References int_val().

00137                                                            {
00138   if (this->present_and_set(opt)) {return int_val(opt);} 
00139   else {return defval;}
00140 }

double CmdLine::double_val ( const string &  opt  )  const

return the double value corresponding to the given option

Definition at line 146 of file CmdLine.cc.

References string_val().

Referenced by double_val(), and main().

00146                                                    {
00147   double result;
00148   string optstring = string_val(opt);
00149   istringstream optstream(optstring);
00150   optstream >> result;
00151   if (optstream.fail()) {
00152     cerr << "Error: could not convert option ("<<opt<<") value ("
00153          <<optstring<<") to double"<<endl; 
00154     exit(-1);}
00155   return result;
00156 }

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 159 of file CmdLine.cc.

References double_val().

00159                                                                           {
00160   if (this->present_and_set(opt)) {return double_val(opt);} 
00161   else {return defval;}
00162 }

string CmdLine::string_val ( const string &  opt  )  const

return the string value corresponding to the given option

Definition at line 102 of file CmdLine.cc.

References __arguments, __options, and __options_used.

Referenced by double_val(), int_val(), string_val(), and value().

00102                                                    {
00103   if (!this->present_and_set(opt)) {
00104     cerr << "Error: Option "<<opt
00105          <<" is needed but is not present_and_set"<<endl;
00106     exit(-1);
00107   }
00108   string arg = __arguments[__options[opt]];
00109   // this may itself look like an option -- if that is the case
00110   // declare the option to have been used
00111   if (arg.compare(0,1,"-") == 0) {__options_used[arg] = true;}
00112   return arg;
00113 }

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 116 of file CmdLine.cc.

References string_val().

00116                                                                           {
00117   if (this->present_and_set(opt)) {return string_val(opt);} 
00118   else {return defval;}
00119 }

string CmdLine::command_line (  )  const

return the full command line

Definition at line 166 of file CmdLine.cc.

References __command_line.

00166                                    {
00167   return __command_line;
00168 }

bool CmdLine::all_options_used (  )  const

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

Definition at line 172 of file CmdLine.cc.

References __options_used.

Referenced by main().

00172                                      {
00173   bool result = true;
00174   for(map<string,bool>::const_iterator opt = __options_used.begin();
00175       opt != __options_used.end(); opt++) {
00176     bool this_one = opt->second;
00177     if (! this_one) {cerr << "Option "<<opt->first<<" unused/unrecognized"<<endl;}
00178     result = result && this_one;
00179   }
00180   return result;
00181 }

void CmdLine::init (  )  [private]

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

Definition at line 55 of file CmdLine.cc.

References __arguments, __command_line, __options, and __options_used.

Referenced by CmdLine().

00055                    {
00056   __command_line = "";
00057   for(size_t iarg = 0; iarg < __arguments.size(); iarg++){
00058     __command_line += __arguments[iarg];
00059     __command_line += " ";
00060   }
00061   
00062   // group things into options
00063   bool next_may_be_val = false;
00064   string currentopt;
00065   for(size_t iarg = 1; iarg < __arguments.size(); iarg++){
00066     // if expecting an option value, then take it (even if
00067     // it is actually next option...)
00068     if (next_may_be_val) {__options[currentopt] = iarg;}
00069     // now see if it might be an option itself
00070     string arg = __arguments[iarg];
00071     bool thisisopt = (arg.compare(0,1,"-") == 0);
00072     if (thisisopt) {
00073       // set option to a standard undefined value and say that 
00074       // we expect (possibly) a value on next round
00075       currentopt = arg;
00076       __options[currentopt] = -1;
00077       __options_used[currentopt] = false;
00078       next_may_be_val = true;}
00079     else {
00080       // otherwise throw away the argument for now...
00081       next_may_be_val = false;
00082       currentopt = "";
00083     }
00084   }
00085 }

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

report failure of conversion

Definition at line 185 of file CmdLine.cc.

Referenced by value().

00186                                                                          {
00187   cerr << "Error: could not convert option ("<<opt<<") value ("
00188        <<optstring<<") to requested type"<<endl; 
00189   exit(-1);
00190 }


Member Data Documentation

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

Definition at line 51 of file CmdLine.hh.

Referenced by init(), present(), present_and_set(), and string_val().

vector<string> CmdLine::__arguments [private]

Definition at line 52 of file CmdLine.hh.

Referenced by arguments(), CmdLine(), init(), and string_val().

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

Definition at line 53 of file CmdLine.hh.

Referenced by all_options_used(), init(), present(), and string_val().

string CmdLine::__command_line [private]

Definition at line 55 of file CmdLine.hh.

Referenced by command_line(), and init().


The documentation for this class was generated from the following files:
Generated on Tue Dec 18 17:05:43 2007 for fastjet by  doxygen 1.5.2