00001 /** 00002 * @file popt_options.h 00003 * option parsing 00004 * 00005 * This provides a simple facility for adding command-line 00006 * options, and parsing them. 00007 * 00008 * You can add a number of options and then call parse_options() 00009 * to process them, for example : 00010 * 00011 * \code 00012 * bool allow_frob; 00013 * string frob; 00014 * static popt::option allow_frob_opt(allow_frob, "allow-frob", 'a', "allow frobs"); 00015 * static popt::option frob_opt(frob, "frob", 'f', "what to frob", "name"); 00016 * 00017 * ... 00018 * popt::parse_options(argc, argv, add_params); 00019 * \endcode 00020 * 00021 * Note than if you try to implement an option for an unsupported type like : 00022 * \code 00023 * static unsigned int i; 00024 * static popt::option i_opt(i, ....); 00025 * \endcode 00026 * you don't get a compile time error but a link time error. 00027 * 00028 * The call to parse_options() will fill in allow_frob and frob, if they 00029 * are passed to the program (myfrobber --allow-frob --frob foo), and place 00030 * any left over command line arguments in the add_params vector. Note 00031 * that the template parameter denotes the type of the option argument. 00032 * 00033 * When the template parameter type is bool, option starting with "no-" prefix 00034 * are implicitely considered as negated before writing the associated bool so 00035 * this will work as expected: 00036 * \code 00037 * bool demangle; 00038 * popt::option(demangle, "demangle", 'd', "demangle C++ symbols"), 00039 * popt::option(demangle, "no-demangle", '\0', "don't demangle C++ symbols"), 00040 * \endcode 00041 * 00042 * @remark Copyright 2002 OProfile authors 00043 * @remark Read the file COPYING 00044 * 00045 * @author Philippe Elie 00046 * @author John Levon 00047 */ 00048 00049 #ifndef POPT_OPTIONS_H 00050 #define POPT_OPTIONS_H 00051 00052 #include <string> 00053 #include <vector> 00054 00055 namespace popt { 00056 00057 /** 00058 * parse_options - parse command line options 00059 * @param argc like the parameter of main() 00060 * @param argv like the parameter of main() 00061 * @param additional_params additional options are stored here 00062 * 00063 * Parse the given command line with the previous 00064 * options created. Multiple additional arguments 00065 * that are not recognised will be added to the additional_params 00066 * vector. 00067 */ 00068 void parse_options(int argc, char const ** argv, 00069 std::vector<std::string> & additional_params); 00070 00071 class option_base; 00072 00073 /** 00074 * option - base class for a command line option 00075 * 00076 * Every command line option added before calling parse_options() 00077 * is of this type. 00078 */ 00079 class option { 00080 public: 00081 /** 00082 * Templatized constructor for an option. This adds the option 00083 * to the option list on construction. This is specialized for 00084 * each recognised option value type below. 00085 */ 00086 template <class T> option(T &, char const * option_name, 00087 char short_name, char const * help_str, 00088 char const * arg_help_str); 00089 00090 /** 00091 * boolean operations don't get the same set of parameters as other 00092 * option, as there is no argument to give help for. 00093 * Due to a bug in gcc 2.95 we can't use a default parameter 00094 * in the templatized ctor above because 2.95 is unable to match 00095 * the right ctor. So on we add a non-templatized ctor with an exact 00096 * match for boolean option. 00097 */ 00098 option(bool &, char const * option_name, 00099 char short_name, char const * help_str); 00100 00101 ~option(); 00102 00103 private: 00104 option_base * the_option; 00105 }; 00106 00107 00108 /** 00109 * The supported option type, boolean option are matched by a non templatized 00110 * ctor above. 00111 */ 00112 template <> option::option(int &, char const * option_name, char short_name, 00113 char const * help_str, char const * arg_help_str); 00114 template <> option::option(std::string &, char const * option_name, 00115 char short_name, char const * help_str, 00116 char const * arg_help_str); 00117 template <> option::option(std::vector<std::string> &, 00118 char const * option_name, char short_name, 00119 char const * help_str, char const * arg_help_str); 00120 00121 } // namespace popt 00122 00123 #endif // POPT_OPTIONS_H
1.6.1