GestureRecognitionToolkit  Version: 0.2.5
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]"),warningLog("[WARNING CommandLineParser]"),errorLog("[ERROR 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  parsed[ id ] = false; //Flag that this ID has not been parsed
71  return true;
72  }
73 
81  template<class T> bool addOption(const std::string &option,const std::string &id,const T &defaultValue){
82 
83  //Make sure the option has not already been added
84  std::map<std::string,std::string>::iterator it = options.find( option );
85  if( it != options.end() ){
86  return false;
87  }
88 
89  //Add the option and id
90  options[option] = id;
91  results[ id ] = toString( defaultValue ); //Set the default value
92  parsed[ id ] = false; //Flag that this ID has not been parsed
93  return true;
94  }
95 
103  bool parse( const int argc, char * argv[] ){
104 
105  //Reset the parsed state
106  {
107  std::map<std::string,bool>::iterator it;
108  it = parsed.begin();
109  while( it != parsed.end() ){
110  it->second = false;
111  ++it;
112  }
113  }
114 
115  std::map<std::string,std::string>::iterator it;
116  for(int i=1; i<argc; i++){
117  //Search for a match in the options map
118  it = options.find( std::string(argv[i]) );
119  if( it != options.end() ){
120  if( i+1 < argc ){
121  results[ it->second ] = std::string(argv[i+1]);
122  }else{
123  results[ it->second ] = "";
124  }
125  parsed[ it->second ] = true;
126  }
127  }
128  return true;
129  }
130 
140  template<class T> bool get(const std::string &id,T &value){
141 
142  //Find the option in the results buffer
143  std::map<std::string,std::string>::iterator it;
144  it = results.find( id );
145 
146  //If the iterator is empty, then we failed to find a match
147  if( it == results.end() ){
148  warningLog << "get(const std::string &id,T &value) - Failed to find id: " << id << ", it must not have been parsed and no default value set!" << std::endl;
149  return false;
150  }
151 
152  //Convert the data associated with the id to the relevant type
153  try{
154  std::stringstream s( it->second );
155  s >> value;
156  return true;
157  }catch( ... ){
158  warningLog << "get(const std::string &id,T &value) - Failed to convert type: " << typeid( value ).name() << " from: " << it->second << " for " << it->first << std::endl;
159  }
160 
161  return false;
162  }
163 
171  bool getValidId(const std::string &id){
172 
173  //Find the option in the parsed buffer
174  std::map<std::string,bool>::iterator it;
175  it = parsed.find( id );
176 
177  if( it == parsed.end() ){
178  return false;
179  }
180 
181  //If we get this far then we've found a match, we don't care about the
182  //parsed state
183  return true;
184  }
185 
193  bool getOptionParsed(const std::string &id){
194  //Find the id in the parsed buffer
195  std::map<std::string,bool>::iterator it;
196  it = parsed.find( id );
197 
198  if( it == parsed.end() ){
199  return false;
200  }
201 
202  //If we get this far then we've found a match, return the parsed state
203  return it->second;
204  }
205 
209  unsigned int getNumOptions() const {
210  return (unsigned int)options.size();
211  }
212 
219  bool setInfoLoggingEnabled(const bool loggingEnabled){
220  return infoLog.setInstanceLoggingEnabled( loggingEnabled );
221  }
222 
229  bool setWarningLoggingEnabled(const bool loggingEnabled){
230  return warningLog.setInstanceLoggingEnabled( loggingEnabled );
231  }
232 
239  bool setErrorLoggingEnabled(const bool loggingEnabled){
240  return errorLog.setInstanceLoggingEnabled( loggingEnabled );
241  }
242 
243 protected:
244  InfoLog infoLog;
245  WarningLog warningLog;
246  ErrorLog errorLog;
247  std::map< std::string, std::string > options;
248  std::map< std::string, std::string > results;
249  std::map< std::string, bool > parsed;
250 
251  template <class T>
252  std::string toString(const T&value){
253  std::ostringstream out;
254  out << value;
255  return out.str();
256  }
257 };
258 
259 GRT_END_NAMESPACE
260 
261 #endif //GRT_COMMAND_LINE_PARSER_HEADER
unsigned int getNumOptions() const
std::map< std::string, std::string > options
Holds the options lookup map.
bool addOption(const std::string &option, const std::string &id)
bool addOption(const std::string &option, const std::string &id, const T &defaultValue)
bool setWarningLoggingEnabled(const bool loggingEnabled)
virtual ~CommandLineParser()
std::map< std::string, std::string > results
Stores the results for each option in the options lookup map.
virtual bool setInstanceLoggingEnabled(const bool loggingEnabled)
sets if logging is enabled for this specific instance
Definition: Log.h:201
bool parse(const int argc, char *argv[])
bool setErrorLoggingEnabled(const bool loggingEnabled)
bool getValidId(const std::string &id)
bool setInfoLoggingEnabled(const bool loggingEnabled)
std::map< std::string, bool > parsed
Stores the results that have been parsed.
bool getOptionParsed(const std::string &id)