00001 //---------------------------------------------------------------------------- 00002 /** @file SgCmdLineOpt.h 00003 Parser for command line options. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef SG_CMDLINEOPT_H 00008 #define SG_CMDLINEOPT_H 00009 00010 #include <map> 00011 #include <string> 00012 #include <vector> 00013 00014 //---------------------------------------------------------------------------- 00015 00016 /** Parser for command line options. 00017 00018 @deprecated Use boost::program_options instead 00019 00020 Options start with the character '-'. 00021 The end of options can be indicated with "--" to allow arguments 00022 beginning with '-'. 00023 00024 Example:<br> 00025 In this example the allowed options are: 00026 a required option with integer argument "-optint", 00027 a option with string argument "-optstr" and default value "" 00028 and a option with no argument "-optbool" and default off. 00029 @verbatim 00030 static bool s_optBool; 00031 00032 static int s_optInt; 00033 00034 static string s_optStr; 00035 00036 static void ParseOptions(int argc, char** argv) 00037 { 00038 SgCmdLineOpt cmdLineOpt; 00039 vector<string> specs; 00040 specs.push_back("optint:"); 00041 specs.push_back("optstr:"); 00042 specs.push_back("optbool"); 00043 cmdLineOpt.Parse(argc, argv, specs); 00044 if (! cmdLineOpt.Contains("optint")) 00045 throw SgException("Requires option -optint"); 00046 cmdLineOpt.GetInteger("optint", s_optInt); 00047 s_optStr = cmdLineOpt.GetString("optstr", ""); 00048 s_optBool = cmdLineOpt.Contains("optbool"); 00049 } 00050 00051 int main(int argc, char** argv) 00052 { 00053 try 00054 { 00055 ParseOptions(argc, argv); 00056 // ... 00057 } 00058 catch (const std::exception& e) 00059 { 00060 SgDebug() << e.what() << '\n'; 00061 return 1; 00062 } 00063 return 0; 00064 } 00065 @endverbatim 00066 */ 00067 class SgCmdLineOpt 00068 { 00069 public: 00070 SgCmdLineOpt(); 00071 00072 bool Contains(const char* option) const; 00073 00074 /** Get a list of the remaining command line arguments that are not an 00075 option. 00076 */ 00077 const std::vector<std::string>& GetArguments() const; 00078 00079 /** Get value of a floating point option. 00080 @throws SgException on error 00081 */ 00082 double GetDouble(const char* option) const; 00083 00084 /** Get value of a floating point option or use default value. 00085 @throws SgException on error 00086 */ 00087 double GetDouble(const char* option, double defaultValue) const; 00088 00089 /** Get value of an integer option. 00090 @throws SgException on error 00091 */ 00092 int GetInteger(const char* option) const; 00093 00094 /** Get value of an integer option or use default value. 00095 @throws SgException on error 00096 */ 00097 int GetInteger(const char* option, int defaultValue) const; 00098 00099 /** Get value of a string option. */ 00100 std::string GetString(const char* option) const; 00101 00102 /** Get value of a string option or use default value. */ 00103 std::string GetString(const char* option, 00104 const std::string& defaultValue) const; 00105 00106 /** Parse options from main(argc, argv). 00107 @param argc Number of elements in argv 00108 @param argv Argument vector from main(). 00109 @param specs 00110 Array of valid options (not including the leading '-'). 00111 Append ':' to options that are allowed to have an argument. 00112 @throws SgException on error 00113 */ 00114 void Parse(int argc, char* argv[], const std::vector<std::string>& specs); 00115 00116 /** Parse options from user created array. 00117 Uses const char* arguments. The only portable version of a C++ main() 00118 function uses char* arguments, but for testing this class with a 00119 user created array, it is better to use const char*, because 00120 assigning a string constant to char* causes compiler warnings with 00121 some compilers. 00122 */ 00123 void Parse(int argc, const char* argv[], 00124 const std::vector<std::string>& specs); 00125 00126 private: 00127 std::vector<std::string> m_args; 00128 00129 std::map<std::string, std::string> m_map; 00130 }; 00131 00132 //---------------------------------------------------------------------------- 00133 00134 #endif // SG_CMDLINEOPT_H