CmdLine.hh

Go to the documentation of this file.
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:: 470                                                          $//
00022 // $Date:: 2007-02-20 20:20:19 +0100 (Tue, 20 Feb 2007)                     $//
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 
00050 class CmdLine {
00051   mutable map<string,int> __options;
00052   vector<string> __arguments;
00053   mutable map<string,bool> __options_used;
00054   //string __progname;
00055   string __command_line;
00056 
00057  public :
00058   CmdLine() {};
00060   CmdLine(const int argc, char** argv);
00062   CmdLine(const vector<string> & args);
00063 
00065   bool    present(const string & opt) const;
00067   bool    present_and_set(const string & opt) const;
00068 
00071   inline const vector<string> & arguments() const {return __arguments;}
00072 
00074   template<class T> T value(const string & opt) const;
00075   template<class T> T value(const string & opt, const T & defval) const;
00076 
00077 
00079   int     int_val(const string & opt);
00081   int     int_val(const string & opt, const int & defval);
00082 
00084   double  double_val(const string & opt) const;
00086   double  double_val(const string & opt, const double & defval) const;
00087 
00089   string  string_val(const string & opt) const;
00091   string  string_val(const string & opt, const string & defval) const;
00092 
00094   string  command_line() const;
00095 
00097   bool all_options_used() const;
00098 
00099  private:
00101   void init();
00102 
00104   void _report_conversion_failure(const string & opt, 
00105                                   const string & optstring) const;
00106 
00107 
00108 };
00109 
00110 
00111 
00113 template<class T> T CmdLine::value(const string & opt) const {
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 }
00121 
00123 template<> inline string CmdLine::value<string>(const string & opt) const {
00124   return string_val(opt);}
00125 
00126 
00127 
00128 template<class T> T CmdLine::value(const string & opt, const T & defval) const {
00129   if (this->present_and_set(opt)) {return value<T>(opt);} 
00130   else {return defval;}
00131 }
00132 
00133 #endif

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