00001
00002
00003
00004
00005
00006 #include "CmdLine.h"
00007 #ifndef STL96 // preprocessor token for very old STL, circa 1996 that does not have namespace functionality
00008 using namespace std;
00009 #endif
00010
00011 CmdLine::CmdLine(){
00012 debug=false;
00013 }
00014 void CmdLine::addParameterSwitch(const string flag,const string initialValue,const string help){
00015 m.insert(mapType::value_type(flag,initialValue));
00016 h.insert(mapType::value_type(flag,help));
00017 }
00018 void CmdLine::addStandaloneSwitch(const string flag,const string help){
00019 addParameterSwitch(flag,"unset",help);
00020 }
00021 const string& CmdLine::switchValue(const string flag){
00022 return m[flag];
00023 }
00024 const string& CmdLine::switchHelp(const string flag){
00025 return h[flag];
00026 }
00027 void CmdLine::process(int argc,char** argv){
00028 if(debug==true) cout << "Processing input arguments\n";
00029 for(int i=1;i<argc;i++){
00030 if(debug==true){
00031 cout << "\targv[" << i << "] " << argv[i] << "\n";
00032 }
00033 string flag=argv[i];
00034 mapType::iterator pos=m.find(flag);
00035
00036 if(pos==m.end()){
00037 l.push_back(argv[i]);
00038 }else if ("unset"==(*pos).second){
00039 m.erase(pos);
00040 m.insert(mapType::value_type(flag,"set"));
00041 }else{
00042 m.erase(pos);
00043 m.insert(mapType::value_type(flag,argv[++i]));
00044 }
00045 }
00046 }
00047 int CmdLine::NumberOfRemainingParameters(){
00048 return l.size();
00049 }
00050 const char * CmdLine::RemainingParameter(int index){
00051 return l[index];
00052
00053 }
00054 void CmdLine::printSwitches(ostream& s){
00055 mapType::const_iterator mI;
00056 s.setf(ios::left);
00057 for( mI=m.begin();mI!=m.end();++mI){
00058 s << "\t";
00059 s.width(10);
00060 #ifdef STL96
00061 s << (*mI).first;
00062 s.width(20);
00063 s << switchHelp((*mI).first) << "\n";
00064 #else
00065 s << mI->first;
00066 s.width(20);
00067 s << switchHelp(mI->first) << "\n";
00068 #endif
00069 }
00070 s.unsetf(ios::left);
00071 }
00072 void CmdLine::dump(ostream& s) const{
00073 const_cast<CmdLine *>(this)->dump(s);
00074 }
00075 void CmdLine::dump(ostream& s){
00076 s << "State of switches\n";
00077 mapType::const_iterator mI;
00078 s.setf(ios::left);
00079 for( mI=m.begin();mI!=m.end();++mI){
00080 s << "\t";
00081 s.width(10);
00082 #ifdef STL96
00083 s << (*mI).first;
00084 s.width(20);
00085 s << (*mI).second << "\t:"<< switchHelp((*mI).first) << "\n";
00086 #else
00087 s << mI->first;
00088 s.width(20);
00089 s << mI->second << "\t:"<< switchHelp(mI->first) << "\n";
00090 #endif
00091 }
00092 s.unsetf(ios::left);
00093 s << "Remaining parameters\n";
00094 for(int i=0;i<NumberOfRemainingParameters();++i){
00095 s << "\t" << RemainingParameter(i) << "\n";
00096 }
00097 }
00098
00099 ostream& operator<<(ostream &s,const CmdLine& c){
00100 c.dump(s);
00101 return s;
00102 }