FastJet 3.0.2
CmdLine.hh
00001 ///////////////////////////////////////////////////////////////////////////////
00002 // File: CmdLine.hh                                                          //
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:: 1761                                                         $//
00022 // $Date:: 2010-09-16 12:43:18 +0200 (Thu, 16 Sep 2010)                     $//
00023 ///////////////////////////////////////////////////////////////////////////////
00024 
00025 
00026 #ifndef __CMDLINE__
00027 #define __CMDLINE__
00028 
00029 #include<string>
00030 #include<sstream>
00031 #include<map>
00032 #include<vector>
00033 using namespace std;
00034 
00035 /// \if internal_doc
00036 /// @ingroup internal
00037 /// \class CmdLine
00038 /// Class designed to deal with command-line arguments in a fashion similar
00039 /// to what was done in f90 iolib.
00040 ///
00041 /// Note that functionality might be slightly different? 
00042 /// Currently do not implement access to arguments by index
00043 /// though data structure would in principle allow this quite easily.
00044 ///
00045 /// GPS 03/01/05
00046 /// [NB: wonder if some of this might be more efficiently written 
00047 /// with templates for different type that can be read in...]
00048 ///
00049 /// Other question: dealing with list of options is rather common
00050 /// occurrence -- command-line arguments, but also card files; maybe one
00051 /// could somehow use base/derived classes to share common functionality? 
00052 /// \endif
00053 class CmdLine {
00054   mutable map<string,int> __options;
00055   vector<string> __arguments;
00056   mutable map<string,bool> __options_used;
00057   //string __progname;
00058   string __command_line;
00059 
00060  public :
00061   CmdLine() {};
00062   /// initialise a CmdLine from a C-style array of command-line arguments
00063   CmdLine(const int argc, char** argv);
00064   /// initialise a CmdLine from a C++ vector of arguments 
00065   CmdLine(const vector<string> & args);
00066 
00067   /// true if the option is present
00068   bool    present(const string & opt) const;
00069   /// true if the option is present and corresponds to a value
00070   bool    present_and_set(const string & opt) const;
00071 
00072   /// return a reference to the vector of command-line arguments (0 is
00073   /// command).
00074   inline const vector<string> & arguments() const {return __arguments;}
00075 
00076   /// returns the value of the argument converted to type T
00077   template<class T> T value(const string & opt) const;
00078   template<class T> T value(const string & opt, const T & defval) const;
00079 
00080 
00081   /// return the integer value corresponding to the given option
00082   int     int_val(const string & opt) const;
00083   /// return the integer value corresponding to the given option or default if option is absent
00084   int     int_val(const string & opt, const int & defval) const;
00085 
00086   /// return the double value corresponding to the given option
00087   double  double_val(const string & opt) const;
00088   /// return the double value corresponding to the given option or default if option is absent
00089   double  double_val(const string & opt, const double & defval) const;
00090 
00091   /// return the string value corresponding to the given option
00092   string  string_val(const string & opt) const;
00093   /// return the string value corresponding to the given option or default if option is absent
00094   string  string_val(const string & opt, const string & defval) const;
00095 
00096   /// return the full command line
00097   string  command_line() const;
00098 
00099   /// return true if all options have been asked for at some point or other
00100   bool all_options_used() const;
00101 
00102  private:
00103   /// builds the internal structures needed to keep track of arguments and options
00104   void init();
00105 
00106   /// report failure of conversion
00107   void _report_conversion_failure(const string & opt, 
00108                                   const string & optstring) const;
00109 
00110 
00111 };
00112 
00113 
00114 
00115 /// returns the value of the argument converted to type T
00116 template<class T> T CmdLine::value(const string & opt) const {
00117   T result;
00118   string optstring = string_val(opt);
00119   istringstream optstream(optstring);
00120   optstream >> result;
00121   if (optstream.fail()) _report_conversion_failure(opt, optstring);
00122   return result;
00123 }
00124 
00125 /// for the string case, just copy the string...
00126 template<> inline string CmdLine::value<string>(const string & opt) const {
00127   return string_val(opt);}
00128 
00129 
00130 
00131 template<class T> T CmdLine::value(const string & opt, const T & defval) const {
00132   if (this->present_and_set(opt)) {return value<T>(opt);} 
00133   else {return defval;}
00134 }
00135 
00136 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends