1 /** 2 * @file popt_options.h 3 * option parsing 4 * 5 * This provides a simple facility for adding command-line 6 * options, and parsing them. 7 * 8 * You can add a number of options and then call parse_options() 9 * to process them, for example : 10 * 11 * \code 12 * bool allow_frob; 13 * string frob; 14 * static popt::option allow_frob_opt(allow_frob, "allow-frob", 'a', "allow frobs"); 15 * static popt::option frob_opt(frob, "frob", 'f', "what to frob", "name"); 16 * 17 * ... 18 * popt::parse_options(argc, argv, add_params); 19 * \endcode 20 * 21 * Note than if you try to implement an option for an unsupported type like : 22 * \code 23 * static unsigned int i; 24 * static popt::option i_opt(i, ....); 25 * \endcode 26 * you don't get a compile time error but a link time error. 27 * 28 * The call to parse_options() will fill in allow_frob and frob, if they 29 * are passed to the program (myfrobber --allow-frob --frob foo), and place 30 * any left over command line arguments in the add_params vector. Note 31 * that the template parameter denotes the type of the option argument. 32 * 33 * When the template parameter type is bool, option starting with "no-" prefix 34 * are implicitely considered as negated before writing the associated bool so 35 * this will work as expected: 36 * \code 37 * bool demangle; 38 * popt::option(demangle, "demangle", 'd', "demangle C++ symbols"), 39 * popt::option(demangle, "no-demangle", '\0', "don't demangle C++ symbols"), 40 * \endcode 41 * 42 * @remark Copyright 2002 OProfile authors 43 * @remark Read the file COPYING 44 * 45 * @author Philippe Elie 46 * @author John Levon 47 */ 48 49 #ifndef POPT_OPTIONS_H 50 #define POPT_OPTIONS_H 51 52 #include <string> 53 #include <vector> 54 55 namespace popt { 56 57 /** 58 * parse_options - parse command line options 59 * @param argc like the parameter of main() 60 * @param argv like the parameter of main() 61 * @param additional_params additional options are stored here 62 * 63 * Parse the given command line with the previous 64 * options created. Multiple additional arguments 65 * that are not recognised will be added to the additional_params 66 * vector. 67 */ 68 void parse_options(int argc, char const ** argv, 69 std::vector<std::string> & additional_params); 70 71 class option_base; 72 73 /** 74 * option - base class for a command line option 75 * 76 * Every command line option added before calling parse_options() 77 * is of this type. 78 */ 79 class option { 80 public: 81 /** 82 * Templatized constructor for an option. This adds the option 83 * to the option list on construction. This is specialized for 84 * each recognised option value type below. 85 */ 86 template <class T> option(T &, char const * option_name, 87 char short_name, char const * help_str, 88 char const * arg_help_str); 89 90 /** 91 * boolean operations don't get the same set of parameters as other 92 * option, as there is no argument to give help for. 93 * Due to a bug in gcc 2.95 we can't use a default parameter 94 * in the templatized ctor above because 2.95 is unable to match 95 * the right ctor. So on we add a non-templatized ctor with an exact 96 * match for boolean option. 97 */ 98 option(bool &, char const * option_name, 99 char short_name, char const * help_str); 100 101 ~option(); 102 103 private: 104 option_base * the_option; 105 }; 106 107 108 /** 109 * The supported option type, boolean option are matched by a non templatized 110 * ctor above. 111 */ 112 template <> option::option(int &, char const * option_name, char short_name, 113 char const * help_str, char const * arg_help_str); 114 template <> option::option(std::string &, char const * option_name, 115 char short_name, char const * help_str, 116 char const * arg_help_str); 117 template <> option::option(std::vector<std::string> &, 118 char const * option_name, char short_name, 119 char const * help_str, char const * arg_help_str); 120 121 } // namespace popt 122 123 #endif // POPT_OPTIONS_H 124