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

CmdLine.cc

Go to the documentation of this file.
00001 //STARTHEADER
00002 // $Id: CmdLine.cc 293 2006-08-17 19:38:38Z salam $
00003 //
00004 // Copyright (c) 2005 Matteo Cacciari and Gavin Salam
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of a simple command-line handling environment
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet; if not, write to the Free Software
00026 //  Foundation, Inc.:
00027 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //----------------------------------------------------------------------
00029 //ENDHEADER
00030 
00031 
00032 #include "CmdLine.hh"
00033 #include<string>
00034 #include<sstream>
00035 #include<iostream> // testing
00036 #include<vector>
00037 #include<cstddef> // for size_t
00038 using namespace std;
00039 
00040 // initialise the various structures that we shall
00041 // use to access the command-line options;
00042 //
00043 // If an option appears several times, it is its LAST value
00044 // that will be used in searching for option values (opposite of f90)
00045 CmdLine::CmdLine (const int argc, char** argv) {
00046   __arguments.resize(argc);
00047   for(int iarg = 0; iarg < argc; iarg++){
00048     __arguments[iarg] = argv[iarg];
00049   }
00050   this->init();
00051 }
00052 
00054 CmdLine::CmdLine (const vector<string> & args) {
00055   __arguments = args;
00056   this->init();
00057 }
00058 
00059 //----------------------------------------------------------------------
00060 void CmdLine::init (){
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 }
00091 
00092 // indicates whether an option is present
00093 bool CmdLine::present(const string & opt) {
00094   bool result = (__options.find(opt) != __options.end());
00095   if (result) __options_used[opt] = true;
00096   return result;
00097 }
00098 
00099 // indicates whether an option is present and has a value associated
00100 bool CmdLine::present_and_set(const string & opt) {
00101   bool result = present(opt) && __options[opt] > 0;
00102   return result;
00103 }
00104 
00105 
00106 // return the string value corresponding to the specified option
00107 string CmdLine::string_val(const string & opt) {
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 }
00119 
00120 // as above, but if opt is not present_and_set, return default
00121 string CmdLine::string_val(const string & opt, const string & defval) {
00122   if (this->present_and_set(opt)) {return string_val(opt);} 
00123   else {return defval;}
00124 }
00125 
00126 // Return the integer value corresponding to the specified option;
00127 // Not too sure what happens if option is present_and_set but does not
00128 // have string value...
00129 int CmdLine::int_val(const string & opt) {
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 }
00140 
00141 // as above, but if opt is not present_and_set, return default
00142 int CmdLine::int_val(const string & opt, const int & defval) {
00143   if (this->present_and_set(opt)) {return int_val(opt);} 
00144   else {return defval;}
00145 }
00146 
00147 
00148 // Return the integer value corresponding to the specified option;
00149 // Not too sure what happens if option is present_and_set but does not
00150 // have string value...
00151 double CmdLine::double_val(const string & opt) {
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 }
00162 
00163 // as above, but if opt is not present_and_set, return default
00164 double CmdLine::double_val(const string & opt, const double & defval) {
00165   if (this->present_and_set(opt)) {return double_val(opt);} 
00166   else {return defval;}
00167 }
00168 
00169 
00170 // return the full command line including the command itself
00171 string CmdLine::command_line() {
00172   return __command_line;
00173 }
00174 
00175 
00176 // return true if all options have been asked for at some point or other
00177 bool CmdLine::all_options_used() const {
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 }

Generated on Thu Oct 12 17:36:33 2006 for fastjet by  doxygen 1.4.2