| 1 |
#include <iostream> |
| 2 |
#include "CmdLineParser.h" |
| 3 |
|
| 4 |
namespace FCUtils { |
| 5 |
|
| 6 |
void CmdLineParser::ParseCmdLine(int argc, char ** argv) { |
| 7 |
int i=1; |
| 8 |
string arg,name,val; |
| 9 |
while( i < argc ) { |
| 10 |
// check for options starting with - or + |
| 11 |
arg=argv[i++]; |
| 12 |
string::size_type p= arg.find_first_of("-",0,1); |
| 13 |
switch (p) { |
| 14 |
case 0: |
| 15 |
// If is an option add it to name and store the position to |
| 16 |
// save the val into |
| 17 |
name=arg.substr(1,arg.size()-1); |
| 18 |
// Let's start supposing is a boolean parameter |
| 19 |
args_[name]="true"; |
| 20 |
break; |
| 21 |
case string::npos : |
| 22 |
// Here no - or + found, assign it to the latest saved |
| 23 |
// parameter |
| 24 |
args_[name]=arg; |
| 25 |
break; |
| 26 |
default: |
| 27 |
// put here an error message ! |
| 28 |
break; |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
void CmdLineParser::MoveToOptions() { |
| 35 |
for (ArgMap::const_iterator i = args_.begin(); |
| 36 |
i != args_.end(); ++ i) { |
| 37 |
switch(optmode_) { |
| 38 |
case UPDATE: |
| 39 |
Options::Get()->UpdateOption(i->first, i->second); |
| 40 |
break; |
| 41 |
case DEFINE: |
| 42 |
Options::Get()->SetOption(i->first, i->second); |
| 43 |
break; |
| 44 |
default: |
| 45 |
break; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
void CmdLineParser::Print() const { |
| 52 |
for (ArgMap::const_iterator i = args_.begin(); |
| 53 |
i != args_.end(); ++ i) { |
| 54 |
std::cout << " Arg : " << i->first |
| 55 |
<<", value: " <<i->second << std::endl; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
}; // namespace FCUtils |