18cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd/**
28cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @file popt_options.h
38cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * option parsing
48cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
58cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * This provides a simple facility for adding command-line
68cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * options, and parsing them.
78cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
88cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * You can add a number of options and then call parse_options()
98cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * to process them, for example :
108cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
118cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * \code
128cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * bool allow_frob;
138cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * string frob;
148cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * static popt::option allow_frob_opt(allow_frob, "allow-frob", 'a', "allow frobs");
158cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * static popt::option frob_opt(frob, "frob", 'f', "what to frob", "name");
168cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
178cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * ...
188cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * popt::parse_options(argc, argv, add_params);
198cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * \endcode
208cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
218cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * Note than if you try to implement an option for an unsupported type  like :
228cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * \code
238cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * static unsigned int i;
248cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * static popt::option i_opt(i, ....);
258cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * \endcode
268cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * you don't get a compile time error but a link time error.
278cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
288cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * The call to parse_options() will fill in allow_frob and frob, if they
298cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * are passed to the program (myfrobber --allow-frob --frob foo), and place
308cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * any left over command line arguments in the add_params vector. Note
318cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * that the template parameter denotes the type of the option argument.
328cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
338cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * When the template parameter type is bool, option starting with "no-" prefix
348cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * are implicitely considered as negated before writing the associated bool so
358cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * this will work as expected:
368cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * \code
378cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * bool demangle;
388cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * popt::option(demangle, "demangle", 'd', "demangle C++ symbols"),
398cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * popt::option(demangle, "no-demangle", '\0', "don't demangle C++ symbols"),
408cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * \endcode
418cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
428cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @remark Copyright 2002 OProfile authors
438cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @remark Read the file COPYING
448cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
458cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @author Philippe Elie
468cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @author John Levon
478cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd */
488cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
498cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd#ifndef POPT_OPTIONS_H
508cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd#define POPT_OPTIONS_H
518cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
528cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd#include <string>
538cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd#include <vector>
548cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
558cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddnamespace popt {
568cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
578cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd/**
588cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * parse_options - parse command line options
598cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @param argc like the parameter of main()
608cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @param argv like the parameter of main()
618cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * @param additional_params additional options are stored here
628cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
638cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * Parse the given command line with the previous
648cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * options created. Multiple additional arguments
658cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * that are not recognised will be added to the additional_params
668cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * vector.
678cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd */
688cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddvoid parse_options(int argc, char const ** argv,
698cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd                   std::vector<std::string> & additional_params);
708cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
718cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddclass option_base;
728cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
738cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd/**
748cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * option - base class for a command line option
758cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd *
768cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * Every command line option added before calling parse_options()
778cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * is of this type.
788cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd */
798cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddclass option {
808cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddpublic:
818cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	/**
828cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * Templatized constructor for an option. This adds the option
838cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * to the option list on construction. This is specialized for
848cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * each recognised option value type below.
858cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 */
868cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	template <class T> option(T &, char const * option_name,
878cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	                          char short_name, char const * help_str,
888cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	                          char const * arg_help_str);
898cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
908cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	/**
918cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * boolean operations don't get the same set of parameters as other
928cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * option, as there is no argument to give help for.
938cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * Due to a bug in gcc 2.95 we can't use a default parameter
948cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * in the templatized ctor above because 2.95 is unable to match
958cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * the right ctor. So on we add a non-templatized ctor with an exact
968cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 * match for boolean option.
978cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	 */
988cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	option(bool &, char const * option_name,
998cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	       char short_name, char const * help_str);
1008cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
1018cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	~option();
1028cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
1038cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddprivate:
1048cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd	option_base * the_option;
1058cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd};
1068cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
1078cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
1088cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd/**
1098cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * The supported option type, boolean option are matched by a non templatized
1108cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd * ctor above.
1118cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd */
1128cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddtemplate <> option::option(int &, char const * option_name, char short_name,
1138cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd                           char const * help_str, char const * arg_help_str);
1148cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddtemplate <> option::option(std::string &, char const * option_name,
1158cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd                           char short_name, char const * help_str,
1168cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd                           char const * arg_help_str);
1178cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Doddtemplate <> option::option(std::vector<std::string> &,
1188cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd                           char const * option_name, char short_name,
1198cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd                           char const * help_str, char const * arg_help_str);
1208cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
1218cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd} // namespace popt
1228cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd
1238cfa702f803c5ef6a2b062a489a1b2cf66b45b5eMike Dodd#endif // POPT_OPTIONS_H
124