Main Page | Class List | File List | Class Members | File Members

ProgramOptions.h

Go to the documentation of this file.
00001 // ProgramOptions.h
00002 
00003 #ifndef PROGRAM_OPTIONS_H
00004 #define PROGRAM_OPTIONS_H
00005 
00006 #if _MSC_VER == 1200
00007 #pragma warning(disable : 4786)
00008 #endif // MSC ver 6.0
00009 
00010 #include <string>
00011 #include <iosfwd>
00012 #include <vector>
00013 #include "safe_assign.h"
00014 
00020 class ProgramOption
00021 {
00022 protected:
00023   enum OptionType { opt_boolean, opt_string, opt_long };
00024 
00025   ProgramOption(char cShortOption, const char* pszLongOption, OptionType type, bool bHasParameter)
00026   {
00027     m_cShortOption = cShortOption;
00028     safe_assign(m_sLongOption, pszLongOption);
00029     m_bDefined = false;
00030     m_type = type;
00031     m_bHasParameter = bHasParameter;
00032     m_bValue = false;
00033     m_lValue = 0;
00034     m_lMinValue = LONG_MIN;
00035     m_lMaxValue = LONG_MAX;
00036   }
00037   
00039   bool IsOption(char c) const
00040   {
00041     if (IsCaseSensitive())
00042       return (_toupper(__toascii(GetShortOption())) == _toupper(__toascii(c)));
00043     else
00044       return (c == GetShortOption());
00045   }
00047   bool IsOption(const char* psz) const
00048   {
00049     return (stricmp(psz, GetLongOption().c_str()) == 0);
00050   }
00051 
00052   bool IsDefined() const { return m_bDefined; }
00053   char GetShortOption() const { return m_cShortOption; }
00054   const std::string& GetLongOption() const { return m_sLongOption; }
00055   bool HasParameter() const { return m_bHasParameter; }
00056   bool HasShortOption() const { return m_cShortOption != '\0'; }
00057   bool HasLongOption() const { return !m_sLongOption.empty(); }
00059   bool IsCaseSensitive() const { return m_bCaseSensitive; }
00060   const std::string& GetHelpText() const { return m_sHelpText; }
00061   const std::string& GetParameterHint() const { return m_sParameterHint; }
00062   OptionType GetType() const { return m_type; }
00063 
00064   bool GetBoolValue() const { return m_bValue; }
00065   void SetBoolValue() {
00066     m_bValue = true;
00067     m_bDefined = true;
00068   }
00069   long GetLongValue() const { return m_lValue; }
00071   bool SetLongValue(long l) {
00072     if (l >= GetLongValueMin() && l <= GetLongValueMax())
00073     {
00074       m_lValue = l;
00075       m_bDefined = true;
00076       return true;
00077     }
00078     return false;
00079   }
00080   const std::string& GetStringValue() const { return m_sValue; }
00081   void SetStringValue(const char* psz)
00082   {
00083     safe_assign(m_sValue, psz);
00084     m_bDefined = true;
00085   }
00086 
00087   long GetLongValueMin() const { return m_lMinValue; }
00088   long GetLongValueMax() const { return m_lMaxValue; }
00089 
00090   char m_cShortOption;
00091   std::string m_sLongOption;
00092   bool m_bDefined;
00093   bool m_bCaseSensitive;
00094   bool m_bHasParameter;
00095   std::string m_sParameterHint;
00096   std::string m_sHelpText;
00097   OptionType m_type;
00098 
00099   bool m_bValue;
00100   long m_lValue;
00101   long m_lMinValue;
00102   long m_lMaxValue;
00103   std::string m_sValue;
00104 
00105   friend class ProgramOptions;
00106   friend std::ostream& operator<< (std::ostream&, const ProgramOption &);
00107 };
00108 
00113 class ProgramOptions
00114 {
00115 public:
00116   ProgramOptions();
00117   ~ProgramOptions();
00119   void AddBoolean(
00120     char cShortOption, const char* pszLongOption=0, bool bDefault=false,
00121     bool bCaseSensitive=false, const char* pszHelpText=0
00122   );
00124   void AddString(
00125     char cShortOption, const char* pszLongOption=0, const char* pszDefault=0,
00126     bool bCaseSensitive=false,
00127     const char* pszParameterHint=0, const char* pszHelpText=0
00128   );
00130   void AddLong(
00131     char cShortOption, const char* pszLongOption=0, long lDefault=0,
00132     long lMin=LONG_MIN, long lMax=LONG_MAX,
00133     bool bCaseSensitive=false,
00134     const char* pszParameterHint=0, const char* pszHelpText=0
00135   );
00137   void PrintHelp(std::ostream& os, int width=79) const;
00142   bool IsOptionChar(char c) const;
00144   ProgramOption* GetOption(char c) const;
00146   ProgramOption* GetOption(const char* psz) const;
00151   bool Parse(int argc, char* argv[], std::locale loc=std::locale::classic());
00152   typedef std::vector<ProgramOption*>::const_iterator const_option_iterator;
00153   const_option_iterator options_begin() const { return m_options.begin(); }
00154   const_option_iterator options_end() const { return m_options.end(); }
00155   typedef std::vector<std::string>::const_iterator const_parameter_iterator;
00156   const_parameter_iterator parameter_begin() const { return m_parameters.begin(); }
00157   const_parameter_iterator parameter_end() const { return m_parameters.end(); }
00158   bool GetBoolValue(char option) const;
00159   bool GetBoolValue(const char* pszOption) const;
00160   long GetLongValue(char option) const;
00161   long GetLongValue(const char* pszOption) const;
00162   std::string GetStringValue(char option) const;
00163   std::string GetStringValue(const char* pszOption) const;
00164 protected:
00165   typedef std::vector<ProgramOption*>::iterator option_iterator;
00166   void Add(ProgramOption* pOption);
00167   std::vector<ProgramOption*> m_options;
00168   void AddParameter(const char* psz);
00169   std::vector<std::string> m_parameters;
00170 
00171   std::string m_sOptionChars;
00172 
00174   std::string GetStringParameter(const char* psz) const;
00175 
00176   friend std::ostream& operator<< (std::ostream&, const ProgramOptions &);
00177 };
00178 
00179 #endif // PROGRAM_OPTIONS_H

Generated on Sun Jun 26 13:43:46 2005 for pingem by  doxygen 1.4.3