plll  1.0
arguments.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2011-2014 University of Zurich
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21 */
22 
23 #ifndef PLLL_INCLUDE_GUARD__ARGUMENTS_HPP
24 #define PLLL_INCLUDE_GUARD__ARGUMENTS_HPP
25 
26 #include <map>
27 #include <set>
28 #include <list>
29 #include <string>
30 
38 namespace plll
39 {
40  namespace helper
41  {
64  {
65  public:
66  class Value
70  {
71  friend class ArgumentParser;
72 
73  private:
74  enum Type { VT_Empty, VT_Text, VT_Integer, VT_Float };
75 
76  // VT_Empty is used for arguments of type "--blah". The argument "--blah=" is
77  // treated as VT_Text with getText().size() == 0.
78 
79  std::string d_value;
80  Type d_type;
81  long d_int;
82  double d_float;
83  bool d_id_overflow; // d_type is VT_Integer or VT_Float, but value is too large,
84  // whence d_int/d_float is not meaningful and d_value should be
85  // used
86 
87  Value(const std::string & value, std::string & id);
88 
89  public:
97  bool isEmpty() const
98  {
99  return d_type == VT_Empty;
100  }
101 
108  bool isText() const
109  {
110  return d_type == VT_Text;
111  }
112 
120  const std::string & getText() const
121  {
122  return d_value;
123  }
124 
133  bool hasOverflow() const
134  {
135  return d_id_overflow;
136  }
137 
141  bool isInteger() const
142  {
143  return d_type == VT_Integer;
144  }
145 
152  long getInteger() const
153  {
154  return d_int;
155  }
156 
162  bool isFloat() const
163  {
164  return d_type == VT_Float;
165  }
166 
173  double getFloat() const
174  {
175  return d_float;
176  }
177  };
178 
179  private:
180  class MapIteratorComparator
181  {
182  public:
183  bool operator() (const std::map<std::string, Value>::const_iterator & A,
184  const std::map<std::string, Value>::const_iterator & B) const
185  {
186  std::less<const Value*> less;
187  return less(&(A->second), &(B->second));
188  }
189  };
190 
191  std::map<std::string, Value> d_args;
192  std::list<std::string> d_names;
193  mutable std::set<std::map<std::string, Value>::const_iterator, MapIteratorComparator> d_unprocessed_args;
194 
195  public:
199  ArgumentParser(int argc, char **argv);
200 
204  bool hasNoArguments() const
205  {
206  return d_args.empty() && d_names.empty();
207  }
208 
212  const std::list<std::string> & getNames() const
213  {
214  return d_names;
215  }
216 
223  const Value * getValue(const std::string & arg) const
224  {
225  std::map<std::string, Value>::const_iterator i = d_args.find(arg);
226  if (i == d_args.end())
227  return NULL;
228  else
229  {
230  std::set<std::map<std::string, Value>::const_iterator, MapIteratorComparator>::iterator it = d_unprocessed_args.find(i);
231  if (it != d_unprocessed_args.end())
232  d_unprocessed_args.erase(it);
233  return &(i->second);
234  }
235  }
236 
244  {
245  return d_unprocessed_args.size();
246  }
247 
255  template<class Fun>
257  {
258  for (std::set<std::map<std::string, Value>::const_iterator, MapIteratorComparator>::iterator
259  i = d_unprocessed_args.begin(); i != d_unprocessed_args.end(); ++i)
260  f((*i)->first, (*i)->second);
261  }
262  };
263  }
264 }
265 
266 #endif
bool hasUnprocessedArguments() const
Tests for unchecked label/values pairs.
Definition: arguments.hpp:243
const std::string & getText() const
Returns the raw text value.
Definition: arguments.hpp:120
long getInteger() const
Return integer value.
Definition: arguments.hpp:152
void enumerateUnprocessedArguments(Fun f) const
Calls a functor for every unchecked label/values pair.
Definition: arguments.hpp:256
const std::list< std::string > & getNames() const
Retrieves a list of names, i.e. arguments not starting with - or preceeded with --.
Definition: arguments.hpp:212
ArgumentParser(int argc, char **argv)
Parses the command line arguments. Ignores the name of the current program.
Parses command line arguments and makes them accessible.
Definition: arguments.hpp:42
const Value * getValue(const std::string &arg) const
Looks up the value of a label.
Definition: arguments.hpp:223
double getFloat() const
Return floating point value.
Definition: arguments.hpp:173
bool isEmpty() const
Tests whether value is empty.
Definition: arguments.hpp:97
bool hasOverflow() const
Checks for overflows.
Definition: arguments.hpp:133
bool isFloat() const
Informs whether the result is a floating point number.
Definition: arguments.hpp:162
bool hasNoArguments() const
Quick tests whether arguments were given.
Definition: arguments.hpp:204
bool isInteger() const
Informs whether the result is an integer.
Definition: arguments.hpp:141
bool isText() const
Returns whether the value is text.
Definition: arguments.hpp:108
Stores the value of one command line argument.
Definition: arguments.hpp:66