FastJet  3.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
CmdLine.hh
1 ///////////////////////////////////////////////////////////////////////////////
2 // File: CmdLine.hh //
3 // Part of the CmdLine library
4 // //
5 // Copyright (c) 2007 Gavin Salam //
6 // //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation; either version 2 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // This program is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15 // GNU General Public License for more details. //
16 // //
17 // You should have received a copy of the GNU General Public License //
18 // along with this program; if not, write to the Free Software //
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
20 // //
21 // $Revision:: 1761 $//
22 // $Date:: 2010-09-16 12:43:18 +0200 (Thu, 16 Sep 2010) $//
23 ///////////////////////////////////////////////////////////////////////////////
24 
25 
26 #ifndef __CMDLINE__
27 #define __CMDLINE__
28 
29 #include<string>
30 #include<sstream>
31 #include<map>
32 #include<vector>
33 using namespace std;
34 
35 /// \if internal_doc
36 /// @ingroup internal
37 /// \class CmdLine
38 /// Class designed to deal with command-line arguments in a fashion similar
39 /// to what was done in f90 iolib.
40 ///
41 /// Note that functionality might be slightly different?
42 /// Currently do not implement access to arguments by index
43 /// though data structure would in principle allow this quite easily.
44 ///
45 /// GPS 03/01/05
46 /// [NB: wonder if some of this might be more efficiently written
47 /// with templates for different type that can be read in...]
48 ///
49 /// Other question: dealing with list of options is rather common
50 /// occurrence -- command-line arguments, but also card files; maybe one
51 /// could somehow use base/derived classes to share common functionality?
52 /// \endif
53 class CmdLine {
54  mutable map<string,int> __options;
55  vector<string> __arguments;
56  mutable map<string,bool> __options_used;
57  //string __progname;
58  string __command_line;
59 
60  public :
61  CmdLine() {};
62  /// initialise a CmdLine from a C-style array of command-line arguments
63  CmdLine(const int argc, char** argv);
64  /// initialise a CmdLine from a C++ vector of arguments
65  CmdLine(const vector<string> & args);
66 
67  /// true if the option is present
68  bool present(const string & opt) const;
69  /// true if the option is present and corresponds to a value
70  bool present_and_set(const string & opt) const;
71 
72  /// return a reference to the vector of command-line arguments (0 is
73  /// command).
74  inline const vector<string> & arguments() const {return __arguments;}
75 
76  /// returns the value of the argument converted to type T
77  template<class T> T value(const string & opt) const;
78  template<class T> T value(const string & opt, const T & defval) const;
79 
80 
81  /// return the integer value corresponding to the given option
82  int int_val(const string & opt) const;
83  /// return the integer value corresponding to the given option or default if option is absent
84  int int_val(const string & opt, const int & defval) const;
85 
86  /// return the double value corresponding to the given option
87  double double_val(const string & opt) const;
88  /// return the double value corresponding to the given option or default if option is absent
89  double double_val(const string & opt, const double & defval) const;
90 
91  /// return the string value corresponding to the given option
92  string string_val(const string & opt) const;
93  /// return the string value corresponding to the given option or default if option is absent
94  string string_val(const string & opt, const string & defval) const;
95 
96  /// return the full command line
97  string command_line() const;
98 
99  /// return true if all options have been asked for at some point or other
100  bool all_options_used() const;
101 
102  private:
103  /// builds the internal structures needed to keep track of arguments and options
104  void init();
105 
106  /// report failure of conversion
107  void _report_conversion_failure(const string & opt,
108  const string & optstring) const;
109 
110 
111 };
112 
113 
114 
115 /// returns the value of the argument converted to type T
116 template<class T> T CmdLine::value(const string & opt) const {
117  T result;
118  string optstring = string_val(opt);
119  istringstream optstream(optstring);
120  optstream >> result;
121  if (optstream.fail()) _report_conversion_failure(opt, optstring);
122  return result;
123 }
124 
125 /// for the string case, just copy the string...
126 template<> inline string CmdLine::value<string>(const string & opt) const {
127  return string_val(opt);}
128 
129 
130 
131 template<class T> T CmdLine::value(const string & opt, const T & defval) const {
132  if (this->present_and_set(opt)) {return value<T>(opt);}
133  else {return defval;}
134 }
135 
136 #endif