Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgCmdLineOpt.h

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


Sun Mar 13 2011 Doxygen 1.7.1