Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# CmdLineParser.h

Go to the documentation of this file.

    00001
    00037 #ifndef __CMD_LINE_PARSER_H__
    00038 #define  __CMD_LINE_PARSER_H__
    00039
    00040 #include <stdio.h>
    00041 #include <string.h>
    00042 #include <stdlib.h>
    00043 #include <iostream.h>
    00044
    00045 #ifdef WIN32
    00046         #include <UnixFuncPrototypes.h>
    00047 #else
    00048         #include <getopt.h>
    00049 #endif
    00050
    00051 #define MAX_OPT_ARGS 500
    00052
    00053 class CmdLineParser;
    00054
    00055 typedef bool (CmdLineParser::*CLP_ACTION)();
    00056 typedef const char* LPCSTR;
    00057
    00058 LPCSTR asciitoascii(LPCSTR sz);
    00059
    00060 class CmdLineArgT
    00061 {
    00062         char m_cShortName;
    00063         LPCSTR m_szLongName;
    00064         LPCSTR m_szDesc;
    00065         CLP_ACTION m_pfAction;
    00066         bool m_bSelected;
    00067
    00068 public:
    00069         CmdLineArgT(char cShortName, LPCSTR szLongName, LPCSTR szDesc, CLP_ACTION pfAction = NULL)
    00070         {
    00071                 m_bSelected = false;
    00072                 m_cShortName = cShortName;
    00073                 m_szLongName = szLongName;
    00074                 m_szDesc = szDesc;
    00075                 m_pfAction = pfAction;
    00076         }
    00077
    00078         virtual ~CmdLineArgT() {}
    00079         virtual void Select(LPCSTR szOptArg) { m_bSelected = true; }
    00080         virtual bool HasOptArg() const { return false; }
    00081         virtual void Print(ostream& os = cout) const { }
    00082
    00083         bool HasShortName() const { return m_cShortName != 0; }
    00084         bool HasLongName() const { return m_szLongName != 0 && *m_szLongName != '\0'; }
    00085         bool IsSelected() const { return m_bSelected; }
    00086         bool HasAction() const { return m_pfAction != 0; }
    00087         CLP_ACTION GetAction() const { return m_pfAction; }
    00088         char GetShortName() const { return m_cShortName; }
    00089         LPCSTR GetLongName() const { return m_szLongName; }
    00090         LPCSTR GetDesc() const { return m_szDesc; }
    00091 };
    00092
    00093 template<class T> class CmdLineArg : public CmdLineArgT
    00094 {
    00095         T* m_pOptArg;
    00096         T (*m_pfAtoT)(LPCSTR);
    00097
    00098 public:
    00099         CmdLineArg(char cShortName, LPCSTR szLongName, LPCSTR szDesc,
    00100                 T* pOptArg, const T& defVal, CLP_ACTION pfAction, T (*pfAtoT)(LPCSTR))
    00101                 : CmdLineArgT(cShortName, szLongName, szDesc, pfAction)
    00102         {
    00103                 m_pOptArg = pOptArg;
    00104                 *m_pOptArg = defVal;
    00105                 m_pfAtoT = pfAtoT;
    00106         }
    00107
    00108         void Select(LPCSTR szOptArg)
    00109         {
    00110                 CmdLineArgT::Select(szOptArg);
    00111                 *m_pOptArg = (*m_pfAtoT)(szOptArg);
    00112         }
    00113
    00114         void Print(ostream& os = cout) const
    00115         {
    00116                 os << GetLongName() << " = " << *m_pOptArg << endl;
    00117         }
    00118
    00119         bool HasOptArg() const { return true; }
    00120 };
    00121
    00122 struct OPTION : public option
    00123 {
    00124         void operator=(const CmdLineArgT& arg)
    00125         {
    00126                 name = arg.GetLongName();
    00127                 has_arg = arg.HasOptArg();
    00128                 flag = 0;
    00129                 val = arg.GetShortName();
    00130         }
    00131
    00132         void SetToZero()
    00133         {
    00134                 name = 0;
    00135                 has_arg = 0;
    00136                 flag = 0;
    00137                 val = 0;
    00138         }
    00139 };
    00140
    00148 class CmdLineParser
    00149 {
    00150 protected:
    00151         CmdLineArgT* m_pOptArgs[MAX_OPT_ARGS];
    00152         char** m_pNonOptArgs;
    00153         int m_nOptArgs;
    00154         int m_nNonOptArgs;
    00155         const char* m_szProgramName;
    00156         const char* m_szOptArgHelp;
    00157
    00158 public:
    00159         CmdLineParser()
    00160         {
    00161                 m_pNonOptArgs = NULL;
    00162                 m_nOptArgs = 0;
    00163                 m_nNonOptArgs = 0;
    00164                 m_szProgramName = "prgname";
    00165                 m_szOptArgHelp = NULL;
    00166         }
    00167
    00168         ~CmdLineParser()
    00169         {
    00170                 while (m_nOptArgs)
    00171                         delete m_pOptArgs[--m_nOptArgs];
    00172         }
    00173
    00174         int GetArgCount() const { return m_nNonOptArgs; }
    00175         const char* GetArg(int i) const { return m_pNonOptArgs[i]; }
    00176
    00177         void Add(char cShortName, LPCSTR szLongName, LPCSTR szDesc, CLP_ACTION pfAction = NULL)
    00178         {
    00179                 m_pOptArgs[m_nOptArgs++] = new CmdLineArgT(cShortName, szLongName,
    00180                         szDesc, pfAction);
    00181         }
    00182
    00183         void Add(char cShortName, LPCSTR szLongName, LPCSTR szDesc,
    00184                 int* pOptArg, int defVal, CLP_ACTION pfAction = NULL)
    00185         {
    00186                 m_pOptArgs[m_nOptArgs++] = new CmdLineArg<int>(cShortName, szLongName,
    00187                         szDesc, pOptArg, defVal, pfAction, atoi);
    00188         }
    00189
    00190         void Add(char cShortName, LPCSTR szLongName, LPCSTR szDesc,
    00191                 double* pOptArg, double defVal, CLP_ACTION pfAction = NULL)
    00192         {
    00193                 m_pOptArgs[m_nOptArgs++] = new CmdLineArg<double>(cShortName, szLongName,
    00194                         szDesc, pOptArg, defVal, pfAction, atof);
    00195         }
    00196
    00197         void Add(char cShortName, LPCSTR szLongName, LPCSTR szDesc,
    00198                 LPCSTR* pOptArg, LPCSTR defVal, CLP_ACTION pfAction = NULL)
    00199         {
    00200                 m_pOptArgs[m_nOptArgs++] = new CmdLineArg<LPCSTR>(cShortName, szLongName, szDesc,
    00201                         pOptArg, defVal, pfAction, asciitoascii);
    00202         }
    00203
    00204         void SetOptArgHelp(const char* szOptArgHelp)
    00205         {
    00206                 m_szOptArgHelp = szOptArgHelp;
    00207         }
    00208
    00209         void SetPrgName(const char* szProgramName);
    00210
    00211         void Print(ostream& os = cout);
    00212
    00213         void ShowUsageMsg();
    00214         void ShowErrorMsg(LPCSTR szPgmName);
    00215         bool Parse(int argc, char** argv);
    00216         void ExecActions();
    00217         int GetArgIndex(char c);
    00218         int GetArgIndex(const char* szArgName);
    00219         bool IsSelected(char c);
    00220         bool IsSelected(const char* szArgName);
    00221
    00222         virtual void InitOptions() = 0;
    00223         virtual bool ExecAction(CLP_ACTION pAction) = 0;
    00224         virtual void ShowUsageExamples() {}
    00225 };
    00226
    00227 #endif //__CMD_LINE_PARSER_H__