Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

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)
 true if the option is present
bool present_and_set (const string &opt)
 true if the option is present and corresponds to a value
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)
 return the double value corresponding to the given option
double double_val (const string &opt, const double &defval)
 return the double value corresponding to the given option or default if option is absent
string string_val (const string &opt)
 return the string value corresponding to the given option
string string_val (const string &opt, const string &defval)
 return the string value corresponding to the given option or default if option is absent
string command_line ()
 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

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 55 of file CmdLine.hh.


Constructor & Destructor Documentation

CmdLine::CmdLine  )  [inline]
 

Definition at line 63 of file CmdLine.hh.

00063 {};

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

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

Definition at line 45 of file CmdLine.cc.

References __arguments, and init().

00045                                              {
00046   __arguments.resize(argc);
00047   for(int iarg = 0; iarg < argc; iarg++){
00048     __arguments[iarg] = argv[iarg];
00049   }
00050   this->init();
00051 }

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

initialise a CmdLine from a C++ vector of arguments

Definition at line 54 of file CmdLine.cc.

References __arguments, and init().

00054                                              {
00055   __arguments = args;
00056   this->init();
00057 }


Member Function Documentation

bool CmdLine::all_options_used  )  const
 

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

Definition at line 177 of file CmdLine.cc.

References __options_used.

Referenced by main().

00177                                      {
00178   bool result = true;
00179   for(map<string,bool>::const_iterator opt = __options_used.begin();
00180       opt != __options_used.end(); opt++) {
00181     bool this_one = opt->second;
00182     if (! this_one) {cerr << "Option "<<opt->first<<" unused"<<endl;}
00183     result = result && this_one;
00184   }
00185   return result;
00186 }

string CmdLine::command_line  ) 
 

return the full command line

Definition at line 171 of file CmdLine.cc.

References __command_line.

00171                              {
00172   return __command_line;
00173 }

double CmdLine::double_val const string &  opt,
const double &  defval
 

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

Definition at line 164 of file CmdLine.cc.

References double_val().

00164                                                                     {
00165   if (this->present_and_set(opt)) {return double_val(opt);} 
00166   else {return defval;}
00167 }

double CmdLine::double_val const string &  opt  ) 
 

return the double value corresponding to the given option

Definition at line 151 of file CmdLine.cc.

References string_val().

Referenced by double_val(), and main().

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

void CmdLine::init  )  [private]
 

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

Definition at line 60 of file CmdLine.cc.

References __arguments, __command_line, __options, and __options_used.

Referenced by CmdLine().

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

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

References int_val().

00142                                                            {
00143   if (this->present_and_set(opt)) {return int_val(opt);} 
00144   else {return defval;}
00145 }

int CmdLine::int_val const string &  opt  ) 
 

return the integer value corresponding to the given option

Definition at line 129 of file CmdLine.cc.

References string_val().

Referenced by int_val(), and main().

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

bool CmdLine::present const string &  opt  ) 
 

true if the option is present

Definition at line 93 of file CmdLine.cc.

References __options, and __options_used.

Referenced by main(), and present_and_set().

00093                                         {
00094   bool result = (__options.find(opt) != __options.end());
00095   if (result) __options_used[opt] = true;
00096   return result;
00097 }

bool CmdLine::present_and_set const string &  opt  ) 
 

true if the option is present and corresponds to a value

Definition at line 100 of file CmdLine.cc.

References __options, and present().

00100                                                 {
00101   bool result = present(opt) && __options[opt] > 0;
00102   return result;
00103 }

string CmdLine::string_val const string &  opt,
const string &  defval
 

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

Definition at line 121 of file CmdLine.cc.

References string_val().

00121                                                                     {
00122   if (this->present_and_set(opt)) {return string_val(opt);} 
00123   else {return defval;}
00124 }

string CmdLine::string_val const string &  opt  ) 
 

return the string value corresponding to the given option

Definition at line 107 of file CmdLine.cc.

References __arguments, __options, and __options_used.

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

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


Member Data Documentation

vector<string> CmdLine::__arguments [private]
 

Definition at line 57 of file CmdLine.hh.

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

string CmdLine::__command_line [private]
 

Definition at line 60 of file CmdLine.hh.

Referenced by command_line(), and init().

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

Definition at line 56 of file CmdLine.hh.

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

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

Definition at line 58 of file CmdLine.hh.

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


The documentation for this class was generated from the following files:
Generated on Thu Oct 12 17:36:35 2006 for fastjet by  doxygen 1.4.2