CmdLine.cc

Go to the documentation of this file.
00001 
00002 // File: CmdLine.cc                                                          //
00003 // Part of the CmdLine library                                               //
00004 //                                                                           //
00005 // Copyright (c) 2007 Gavin Salam                                            //
00006 //                                                                           //
00007 // This program is free software; you can redistribute it and/or modify      //
00008 // it under the terms of the GNU General Public License as published by      //
00009 // the Free Software Foundation; either version 2 of the License, or         //
00010 // (at your option) any later version.                                       //
00011 //                                                                           //
00012 // This program is distributed in the hope that it will be useful,           //
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of            //
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             //
00015 // GNU General Public License for more details.                              //
00016 //                                                                           //
00017 // You should have received a copy of the GNU General Public License         //
00018 // along with this program; if not, write to the Free Software               //
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
00020 //                                                                           //
00021 // $Revision:: 139                                                          $//
00022 // $Date:: 2007-01-23 16:09:23 +0100 (Tue, 23 Jan 2007)                     $//
00024 
00025 
00026 
00027 #include "CmdLine.hh"
00028 #include<string>
00029 #include<sstream>
00030 #include<iostream> // testing
00031 #include<vector>
00032 #include<cstddef> // for size_t
00033 using namespace std;
00034 
00035 // initialise the various structures that we shall
00036 // use to access the command-line options;
00037 //
00038 // If an option appears several times, it is its LAST value
00039 // that will be used in searching for option values (opposite of f90)
00040 CmdLine::CmdLine (const int argc, char** argv) {
00041   __arguments.resize(argc);
00042   for(int iarg = 0; iarg < argc; iarg++){
00043     __arguments[iarg] = argv[iarg];
00044   }
00045   this->init();
00046 }
00047 
00049 CmdLine::CmdLine (const vector<string> & args) {
00050   __arguments = args;
00051   this->init();
00052 }
00053 
00054 //----------------------------------------------------------------------
00055 void CmdLine::init (){
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 }
00086 
00087 // indicates whether an option is present
00088 bool CmdLine::present(const string & opt) const {
00089   bool result = (__options.find(opt) != __options.end());
00090   if (result) __options_used[opt] = true;
00091   return result;
00092 }
00093 
00094 // indicates whether an option is present and has a value associated
00095 bool CmdLine::present_and_set(const string & opt) const {
00096   bool result = present(opt) && __options[opt] > 0;
00097   return result;
00098 }
00099 
00100 
00101 // return the string value corresponding to the specified option
00102 string CmdLine::string_val(const string & opt) const {
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 }
00114 
00115 // as above, but if opt is not present_and_set, return default
00116 string CmdLine::string_val(const string & opt, const string & defval) const {
00117   if (this->present_and_set(opt)) {return string_val(opt);} 
00118   else {return defval;}
00119 }
00120 
00121 // Return the integer value corresponding to the specified option;
00122 // Not too sure what happens if option is present_and_set but does not
00123 // have string value...
00124 int CmdLine::int_val(const string & opt) {
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 }
00135 
00136 // as above, but if opt is not present_and_set, return default
00137 int CmdLine::int_val(const string & opt, const int & defval) {
00138   if (this->present_and_set(opt)) {return int_val(opt);} 
00139   else {return defval;}
00140 }
00141 
00142 
00143 // Return the integer value corresponding to the specified option;
00144 // Not too sure what happens if option is present_and_set but does not
00145 // have string value...
00146 double CmdLine::double_val(const string & opt) const {
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 }
00157 
00158 // as above, but if opt is not present_and_set, return default
00159 double CmdLine::double_val(const string & opt, const double & defval) const {
00160   if (this->present_and_set(opt)) {return double_val(opt);} 
00161   else {return defval;}
00162 }
00163 
00164 
00165 // return the full command line including the command itself
00166 string CmdLine::command_line() const {
00167   return __command_line;
00168 }
00169 
00170 
00171 // return true if all options have been asked for at some point or other
00172 bool CmdLine::all_options_used() const {
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 }
00182 
00183 
00185 void CmdLine::_report_conversion_failure(const string & opt, 
00186                                          const string & optstring) const {
00187   cerr << "Error: could not convert option ("<<opt<<") value ("
00188        <<optstring<<") to requested type"<<endl; 
00189   exit(-1);
00190 }
00191 

Generated on Tue Dec 18 17:05:02 2007 for fastjet by  doxygen 1.5.2