21 #ifndef GRT_COMMAND_LINE_PARSER_HEADER 22 #define GRT_COMMAND_LINE_PARSER_HEADER 36 #include "WarningLog.h" 46 CommandLineParser():infoLog(
"[CommandLineParser]"),warningLog(
"[WARNING CommandLineParser]"),errorLog(
"[ERROR CommandLineParser]"){}
60 bool addOption(
const std::string &option,
const std::string &
id){
63 std::map<std::string,std::string>::iterator it =
options.find( option );
81 template<
class T>
bool addOption(
const std::string &option,
const std::string &
id,
const T &defaultValue){
84 std::map<std::string,std::string>::iterator it =
options.find( option );
91 results[ id ] = toString( defaultValue );
103 bool parse(
const int argc,
char * argv[] ){
107 std::map<std::string,bool>::iterator it;
109 while( it !=
parsed.end() ){
115 std::map<std::string,std::string>::iterator it;
116 for(
int i=1; i<argc; i++){
118 it =
options.find( std::string(argv[i]) );
121 results[ it->second ] = std::string(argv[i+1]);
125 parsed[ it->second ] =
true;
140 template<
class T>
bool get(
const std::string &id,T &value){
143 std::map<std::string,std::string>::iterator it;
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;
154 std::stringstream s( it->second );
158 warningLog <<
"get(const std::string &id,T &value) - Failed to convert type: " <<
typeid( value ).name() <<
" from: " << it->second <<
" for " << it->first << std::endl;
174 std::map<std::string,bool>::iterator it;
195 std::map<std::string,bool>::iterator it;
210 return (
unsigned int)
options.size();
230 return warningLog.setInstanceLoggingEnabled( loggingEnabled );
252 std::string toString(
const T&value){
253 std::ostringstream out;
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
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)