GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
CommandLineParser.h
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #ifndef GRT_COMMAND_LINE_PARSER_HEADER
22 #define GRT_COMMAND_LINE_PARSER_HEADER
23 
24 #include <iostream>
25 #include <vector>
26 #include <algorithm>
27 #include <fstream>
28 #include <sstream>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <limits>
32 #include <math.h>
33 #include <fstream>
34 #include <typeinfo>
35 #include "InfoLog.h"
36 #include "WarningLog.h"
37 #include "ErrorLog.h"
38 
39 GRT_BEGIN_NAMESPACE
40 
42 public:
46  CommandLineParser():infoLog("[CommandLineParser]"),errorLog("[ERROR CommandLineParser]"),warningLog("[WARNING CommandLineParser]"){}
47 
51  virtual ~CommandLineParser(){}
52 
60  bool addOption(const std::string &option,const std::string &id){
61 
62  //Make sure the option has not already been added
63  std::map<std::string,std::string>::iterator it = options.find( option );
64  if( it != options.end() ){
65  return false;
66  }
67 
68  //Add the option and id
69  options[option] = id;
70  return true;
71  }
72 
80  bool parse( const int argc, char * argv[] ){
81 
82  results.clear();
83 
84  std::map<std::string,std::string>::iterator it;
85  for(int i=1; i<argc; i++){
86 
87  //Search for a match in the options map
88  it = options.find( std::string(argv[i]) );
89  if( it != options.end() ){
90  results[ it->second ] = std::string(argv[i+1]);
91  }
92  }
93 
94  return true;
95  }
96 
106  template<class T> bool get(const std::string &id,T &value,T defaultValue = T()){
107 
108  //Find the option in the results buffer
109  std::map<std::string,std::string>::iterator it;
110  it = results.find( id );
111 
112  if( it == results.end() ){
113  value = defaultValue;
114  warningLog << "get(const std::string &id,T &value) - Failed to find id: " << id << std::endl;
115  return false;
116  }
117 
118  //Convert the data associated with the id to the relevant type
119  try{
120  std::stringstream s( it->second );
121  s >> value;
122  return true;
123  }catch( ... ){
124  value = defaultValue;
125  warningLog << "get(const std::string &id,T &value) - Can not parse type: " << typeid( value ).name() << std::endl;
126  }
127 
128  return false;
129  }
130 
138  bool validate(const std::string &id){
139 
140  //Find the option in the results buffer
141  std::map<std::string,std::string>::iterator it;
142  it = results.find( id );
143 
144  if( it == results.end() ){
145  warningLog << "validate(const std::string ) - Failed to find id: " << id << std::endl;
146  return false;
147  }
148 
149  //If we get this far then we've found a match
150  return true;
151  }
152 
159  bool setInfoLoggingEnabled(const bool loggingEnabled){
160  infoLog.setEnableInstanceLogging( loggingEnabled );
161  return true;
162  }
163 
170  bool setWarningLoggingEnabled(const bool loggingEnabled){
171  warningLog.setEnableInstanceLogging( loggingEnabled );
172  return true;
173  }
174 
181  bool setErrorLoggingEnabled(const bool loggingEnabled){
182  errorLog.setEnableInstanceLogging( loggingEnabled );
183  return true;
184  }
185 
186 protected:
187  InfoLog infoLog;
188  WarningLog warningLog;
189  ErrorLog errorLog;
190  std::map< std::string, std::string > options;
191  std::map< std::string, std::string > results;
192 
193 };
194 
195 GRT_END_NAMESPACE
196 
197 #endif //GRT_COMMAND_LINE_PARSER_HEADER
bool addOption(const std::string &option, const std::string &id)
bool validate(const std::string &id)
bool setWarningLoggingEnabled(const bool loggingEnabled)
virtual ~CommandLineParser()
bool parse(const int argc, char *argv[])
bool setErrorLoggingEnabled(const bool loggingEnabled)
bool setInfoLoggingEnabled(const bool loggingEnabled)