WTFIT
CommandLineParser.h
Go to the documentation of this file.
1 
8 #ifndef _COMMAND_LINE_PARSER_H
9 #define _COMMAND_LINE_PARSER_H
10 
11 #include <Debug.h>
12 
13 namespace wtfit{
14 
15  class CommandLineParser : public Debug{
16 
17  public:
18 
19  // 1) constructors, destructors, operators, etc.
20  class CommandLineArgument : public Debug{
21 
22 
23  public:
24 
26  boolValue_ = NULL;
27  intValue_ = NULL;
28  doubleValue_ = NULL;
29  stringValue_ = NULL;
30  };
31 
32  int print(stringstream &s) const{
33  s << "[CommandLine] ";
34  if((isAnOption_)||(isOptional_)){
35  s << "[";
36  }
37 
38  s << "-" << key_;
39 
40  if(isAnOption_)
41  s << ":";
42 
43  s << " ";
44  if(!isAnOption_)
45  s << "<";
46 
47  if(description_.length()){
48  s << description_;
49  }
50  else{
51  s << "no description";
52  }
53 
54  if(!isAnOption_)
55  s << ">";
56 
57  if((isAnOption_)||(isOptional_)){
58  s << "]";
59  }
60  s << endl;
61 
62  return 0;
63  };
64 
66  bool *boolValue_;
67  int *intValue_;
68  double *doubleValue_;
69  string *stringValue_;
70 
71  string key_;
72  string description_;
73  };
74 
77  "Global debug level", true);
78  };
79 
81 
82  // 2) functions
83  inline int parse(int argc, char **argv) const{
84 
85  for(int i = 0; i < argc; i++){
86 
87  if((string(argv[i]) == "-h")||(string(argv[i]) == "--help")){
88  printUsage(argv[0]);
89  }
90 
91  for(int j = 0; j < (int) arguments_.size(); j++){
92 
93  if(!arguments_[j].isAnOption_){
94  if((string(argv[i]) == "-" + arguments_[j].key_)
95  &&(i + 1 < argc)){
96 
97  if(arguments_[j].stringValue_){
98  // let's process a string
99  (*arguments_[j].stringValue_) = argv[i + 1];
100  }
101  else if(arguments_[j].intValue_){
102  stringstream s(argv[i + 1]);
103  s >> *(arguments_[j].intValue_);
104  }
105  else if(arguments_[j].doubleValue_){
106  stringstream s(argv[i + 1]);
107  s >> *(arguments_[j].doubleValue_);
108  }
109  }
110  }
111  else{
112  if(string(argv[i]) == "-" + arguments_[j].key_){
113  *(arguments_[j].boolValue_) = true;
114  }
115  }
116  }
117  }
118 
119  // check all the necessary arguments have been provided
120  for(int i = 0; i < (int) arguments_.size(); i++){
121  if(!arguments_[i].isOptional_){
122  if(arguments_[i].stringValue_){
123  if(!arguments_[i].stringValue_->length()){
124  stringstream msg;
125  msg << "[CommandLine] Missing mandatory argument:" << endl;
126  arguments_[i].print(msg);
127  dMsg(cerr, msg.str(), 1);
128  printUsage(argv[0]);
129  }
130  }
131  else if(arguments_[i].intValue_){
132  if(*(arguments_[i].intValue_) == -INT_MAX){
133  stringstream msg;
134  msg << "[CommandLine] Missing mandatory argument:" << endl;
135  arguments_[i].print(msg);
136  dMsg(cerr, msg.str(), 1);
137  printUsage(argv[0]);
138  }
139  }
140  else if(arguments_[i].doubleValue_){
141  if(*(arguments_[i].doubleValue_) == -DBL_MAX){
142  stringstream msg;
143  msg << "[CommandLine] Missing mandatory argument:" << endl;
144  arguments_[i].print(msg);
145  dMsg(cerr, msg.str(), 1);
146  printUsage(argv[0]);
147  }
148  }
149  }
150  }
151 
152  return 0;
153  };
154 
155  inline int printArgs(ostream &o) const{
156 
157  o << "[CommandLine] Options and arguments:" << endl;
158  for(int i = 0; i < (int) arguments_.size(); i++){
159  o << "[CommandLine] -" << arguments_[i].key_;
160  o << ": ";
161 
162  if(arguments_[i].isAnOption_){
163  if(arguments_[i].boolValue_){
164  if(*(arguments_[i].boolValue_))
165  o << "true";
166  else
167  o << "false";
168  }
169  else{
170  o << "(not set)";
171  }
172  }
173  else if(arguments_[i].stringValue_){
174  if(arguments_[i].stringValue_->length()){
175  o << *(arguments_[i].stringValue_);
176  }
177  else{
178  o << "(not set)";
179  }
180  }
181  else if(arguments_[i].intValue_){
182  if(*(arguments_[i].intValue_) == -INT_MAX){
183  o << "(not set)";
184  }
185  else{
186  o << *(arguments_[i].intValue_);
187  }
188  }
189  else if(arguments_[i].doubleValue_){
190  if(*(arguments_[i].doubleValue_) == -DBL_MAX){
191  o << "(not set)";
192  }
193  else{
194  o << *(arguments_[i].doubleValue_);
195  }
196  }
197 
198  o << endl;
199  }
200 
201  return 0;
202  }
203 
204  inline int printUsage(const string &binPath) const {
205 
206  stringstream msg;
207  msg << "[CommandLine]" << endl;
208  msg << "[CommandLine] Usage:" << endl;
209  msg << "[CommandLine] " << binPath << endl;
210  msg << "[CommandLine] Argument(s):" << endl;
211  for(int i = 0; i < (int) arguments_.size(); i++){
212  if(!arguments_[i].isAnOption_){
213  arguments_[i].print(msg);
214  }
215  }
216  msg << "[CommandLine] Option(s):" << endl;
217  for(int i = 0; i < (int) arguments_.size(); i++){
218  if(arguments_[i].isAnOption_){
219  arguments_[i].print(msg);
220  }
221  }
222 
223  dMsg(cerr, msg.str(), 1);
224 
225  exit(0);
226  return 0;
227  };
228 
229  inline int setOption(const string &key, bool *value,
230  const string &description = ""){
231 
232  arguments_.resize(arguments_.size() + 1);
233  arguments_.back().isOptional_ = true;
234  arguments_.back().key_ = key;
235  arguments_.back().description_ = description;
236  arguments_.back().boolValue_ = value;
237  *(arguments_.back().boolValue_) = false;
238  arguments_.back().isAnOption_ = true;
239 
240  return 0;
241  };
242 
243  inline int setDoubleArgument(const string &key, double *value,
244  const string &description = "",
245  const bool &optional = false){
246 
247  arguments_.resize(arguments_.size() + 1);
248  arguments_.back().isOptional_ = optional;
249  arguments_.back().key_ = key;
250  arguments_.back().description_ = description;
251  arguments_.back().doubleValue_ = value;
252  arguments_.back().isAnOption_ = false;
253 
254  *(arguments_.back().doubleValue_) = -DBL_MAX;
255 
256  return 0;
257  };
258 
259  inline int setIntArgument(const string &key, int *value,
260  const string &description = "",
261  const bool &optional = false){
262 
263  arguments_.resize(arguments_.size() + 1);
264  arguments_.back().isOptional_ = optional;
265  arguments_.back().key_ = key;
266  arguments_.back().description_ = description;
267  arguments_.back().intValue_ = value;
268  arguments_.back().isAnOption_ = false;
269 
270  *(arguments_.back().intValue_) = -INT_MAX;
271 
272  return 0;
273  };
274 
275  int setStringArgument(const string &key, string *value,
276  const string &description = "",
277  const bool &optional = false){
278 
279  arguments_.resize(arguments_.size() + 1);
280  arguments_.back().isOptional_ = optional;
281  arguments_.back().key_ = key;
282  arguments_.back().description_ = description;
283  arguments_.back().stringValue_ = value;
284  arguments_.back().isAnOption_ = false;
285 
286  *(arguments_.back().stringValue_) = "";
287 
288  return 0;
289  };
290 
291 
292  protected:
293 
294  vector<CommandLineArgument>
295  arguments_;
296  };
297 }
298 
299 #endif
string description_
Definition: CommandLineParser.h:72
int printUsage(const string &binPath) const
Definition: CommandLineParser.h:204
double * doubleValue_
Definition: CommandLineParser.h:68
vector< CommandLineArgument > arguments_
Definition: CommandLineParser.h:289
const int msg(const char *msg, const int &debugLevel=infoMsg) const
Definition: Debug.cpp:67
int parse(int argc, char **argv) const
Definition: CommandLineParser.h:83
~CommandLineParser()
Definition: CommandLineParser.h:80
int setIntArgument(const string &key, int *value, const string &description="", const bool &optional=false)
Definition: CommandLineParser.h:259
virtual const int dMsg(ostream &stream, string msg, const int &debugLevel=infoMsg) const
Definition: Debug.cpp:52
Definition: CommandLineParser.h:20
int printArgs(ostream &o) const
Definition: CommandLineParser.h:155
string key_
Definition: CommandLineParser.h:71
int print(stringstream &s) const
Definition: CommandLineParser.h:32
int setDoubleArgument(const string &key, double *value, const string &description="", const bool &optional=false)
Definition: CommandLineParser.h:243
int setOption(const string &key, bool *value, const string &description="")
Definition: CommandLineParser.h:229
CommandLineArgument()
Definition: CommandLineParser.h:25
Minimalist debugging class.
Definition: Debug.h:39
CommandLineParser()
Definition: CommandLineParser.h:75
bool isAnOption_
Definition: CommandLineParser.h:63
Basic command line parsing.
Definition: CommandLineParser.h:15
bool isOptional_
Definition: CommandLineParser.h:63
Definition: CommandLineParser.h:13
bool * boolValue_
Definition: CommandLineParser.h:66
int setStringArgument(const string &key, string *value, const string &description="", const bool &optional=false)
Definition: CommandLineParser.h:275
int globalDebugLevel_
Definition: Debug.cpp:5
string * stringValue_
Definition: CommandLineParser.h:69
int * intValue_
Definition: CommandLineParser.h:67