00001 #ifndef CMDPARSER_H
00002 #define CMDPARSER_H
00003 #include "StringUtils.h"
00004 #include "Messaging.h"
00005
00006 class CmdParser
00007 {
00008 public:
00009 CmdParser()
00010 : m_isInitialized(false) {};
00011 ~CmdParser(){};
00012
00013 Int_t countLeading(const TString& text, const TString& characters, Int_t nMax) {
00014 Int_t pos = 0;
00015 while (pos < text.Length() && characters.Index(text[pos]) != kNPOS && (nMax < 0 || pos < nMax)) {
00016 pos++;
00017 }
00018
00019 return pos;
00020 }
00021
00022 Int_t removeLeading (TString &text, TString characters, Int_t nMax = -1) {
00023 Int_t pos = countLeading(text, characters, nMax);
00024 text.Remove(0, pos);
00025 return pos;
00026 };
00027
00028
00029
00030 void addOption(TString lopt, TString sopt, bool hasArg=true)
00031 {
00032 m_lopts.push_back(lopt);
00033 m_sopts.push_back(sopt);
00034 m_lspairs.push_back(std::make_pair(sopt,lopt));
00035 m_argpairs.push_back(std::make_pair(sopt,hasArg));
00036 };
00037
00038 TString getlopt(TString sopt)
00039 {
00040 for (unsigned int i = 0; i<m_lspairs.size(); ++i)
00041 {
00042 if (m_lspairs.at(i).first == sopt)
00043 return m_lspairs.at(i).second;
00044 }
00045 return "";
00046 }
00047
00048 TString getsopt(TString lopt)
00049 {
00050 for (unsigned int i = 0; i<m_lspairs.size(); ++i)
00051 {
00052 if (m_lspairs.at(i).second == lopt)
00053 return m_lspairs.at(i).first;
00054 }
00055 return "";
00056 }
00057
00058 int Init(int argc, char** argv)
00059 {
00060 TString arg = "";
00061 m_isInitialized = false;
00062 TString sopt,lopt,t;
00063 bool hasArg;
00064 for(int i = 1; i<argc; i++)
00065 {
00066 arg = StringUtils::toTString(argv[i]);
00067
00068 if ( arg.BeginsWith("--") || ((arg.BeginsWith("-") && arg.Remove(0,1).IsAlpha() )) )
00069 {
00070 removeLeading(arg,"-");
00071 if (arg.Length() == 1)
00072 {
00073 sopt = arg;
00074 lopt = this->getlopt(sopt);
00075 }
00076 else
00077 {
00078 lopt = arg;
00079 sopt = this->getsopt(lopt);
00080 }
00081 hasArg = true;
00082 for (unsigned int j=0; j<m_argpairs.size(); ++j)
00083 {
00084 if (m_argpairs.at(j).first == sopt)
00085 hasArg = m_argpairs.at(j).second;
00086 }
00087 if (!hasArg)
00088 t = "true";
00089 else
00090 {
00091 if (argc-1 == i)
00092 {
00093 MSG_ERROR("command line flag found without argument");
00094 return 0;
00095 }
00096 else if (argc > i+1 && ( (StringUtils::toTString(argv[i+1]).BeginsWith("-") && (StringUtils::toTString(argv[i+1]).Remove(0,1)).IsAlpha() ) || StringUtils::toTString(argv[i+1]).BeginsWith("--")) )
00097 {
00098 MSG_ERROR("command line flag found without argument");
00099 return 0;
00100 }
00101 t = StringUtils::toTString(argv[i+1]);
00102 }
00103 m_pairs.push_back(std::make_pair(StringUtils::toTString(argv[i]),t));
00104 }
00105 }
00106 m_isInitialized=kTRUE;
00107 return 1;
00108 };
00109
00110 bool getBool(TString opt)
00111 {
00112 TString t = this->getArg(opt);
00113 if (t=="true")
00114 return true;
00115 else
00116 return false;
00117 }
00118
00119 TString getArg(TString opt)
00120 {
00121 TString lopt, sopt;
00122 for (unsigned int i = 0; i<m_lspairs.size(); i++)
00123 {
00124 if (m_lspairs.at(i).first == opt)
00125 {
00126 lopt = m_lspairs.at(i).second;
00127 sopt = opt;
00128 }
00129 if (m_lspairs.at(i).second == opt)
00130 {
00131 sopt = m_lspairs.at(i).first;
00132 lopt = opt;
00133 }
00134 }
00135
00136 TString t;
00137 if (!m_isInitialized)
00138 MSG_ABORT("CommandLine parser is not initialized!");
00139 for (unsigned int i = 0; i<m_pairs.size(); i++)
00140 {
00141 if (m_pairs.at(i).first == "-"+sopt || m_pairs.at(i).first == "--"+lopt)
00142 {
00143 t=m_pairs.at(i).second;
00144 }
00145 }
00146 return t;
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 }
00164
00165 private:
00166
00167 std::vector<TString> m_lopts;
00168 std::vector<TString> m_sopts;
00169 std::vector< std::pair<TString,TString> >m_pairs;
00170 std::vector< std::pair<TString,TString> >m_lspairs;
00171 std::vector< std::pair<TString, bool> >m_argpairs;
00172 bool m_isInitialized;
00173
00174
00175
00176
00177
00178 };
00179 #endif
00180