libshevek
args.hh
1 /* args.hh - argument parsing made easy
2  * Copyright 2003-2010 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_ARGS_HH
19 #define SHEVEK_ARGS_HH
20 
21 #include <getopt.h>
22 #include <string>
23 #include <vector>
24 #include <list>
25 #include <sigc++/sigc++.h>
26 #include <glibmm/ustring.h>
27 #include "debug.hh"
28 
29 // Allow using this class without using automake.
30 #ifndef PACKAGE_NAME
31 #define PACKAGE_NAME "[name not defined]"
32 #endif
33 #ifndef PACKAGE_TARNAME
34 #define PACKAGE_TARNAME PACKAGE_NAME
35 #endif
36 #ifndef PACKAGE_BUGREPORT
37 #define PACKAGE_BUGREPORT "[bug report e-mail not defined]"
38 #endif
39 #ifndef PACKAGE_VERSION
40 #define PACKAGE_VERSION "[version not defined]"
41 #endif
42 #ifndef COPYRIGHT_YEARS
43 #define COPYRIGHT_YEARS "[no time of copyright defined]"
44 #endif
45 #ifndef COPYRIGHT_EMAIL
46 #define COPYRIGHT_EMAIL ""
47 #endif
48 #ifndef COPYRIGHT_AUTHOR
49 #define COPYRIGHT_AUTHOR "[no author defined]"
50 #endif
51 
52 namespace shevek
53 {
55 
65  class args
66  {
67  public:
68  class option;
71  args (int &argc, char **&argv, int min_args, int max_args, Glib::ustring const &description,
72  /* The parameters below should always have their default values,
73  * they are just here to get the #define'd values into the library.
74  * The COPYRIGHT_* values should be defined in configure.ac
75  */
76  Glib::ustring const &copyright_years = COPYRIGHT_YEARS,
77  Glib::ustring const &copyright_email = (COPYRIGHT_EMAIL[0] == '\0' ? PACKAGE_BUGREPORT : COPYRIGHT_EMAIL),
78  Glib::ustring const &programmer = COPYRIGHT_AUTHOR,
79  Glib::ustring const &email = PACKAGE_BUGREPORT,
80  char const *programname = PACKAGE_NAME,
81  char const *packagename = PACKAGE_TARNAME,
82  char const *version = PACKAGE_VERSION);
84  template <unsigned size_>
85  args (int &argc, char **&argv, option (&o)[size_], int min_args,
86  int max_args, Glib::ustring const &description);
88  unsigned size () const;
90  std::string const &operator[] (unsigned idx) const;
92  std::vector <std::string>::const_iterator begin () const;
94  std::vector <std::string>::const_iterator end () const;
95  private:
96  std::vector <std::string> m_args;
97  void l_setup (int &argc, char **&argv, option *o, unsigned num_options,
98  int min_args, int max_args,
99  Glib::ustring const &description,
100  struct ::option *longopts,
101  Glib::ustring const &copyright_years = COPYRIGHT_YEARS,
102  Glib::ustring const &copyright_email = (COPYRIGHT_EMAIL[0] == '\0' ? PACKAGE_BUGREPORT : COPYRIGHT_EMAIL),
103  Glib::ustring const &programmer = COPYRIGHT_AUTHOR,
104  Glib::ustring const &email = PACKAGE_BUGREPORT,
105  char const *programname = PACKAGE_NAME,
106  char const *packagename = PACKAGE_TARNAME,
107  char const *version = PACKAGE_VERSION);
108  };
109 
112  {
113  public:
115  typedef sigc::slot1 <void, bool> callback0;
117  typedef sigc::slot2 <void, bool, Glib::ustring const &> callback1;
119  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, callback0 handle, bool *used = NULL);
121  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, callback1 handle, Glib::ustring default_val = Glib::ustring (), bool *used = NULL);
123  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, callback0 handle0, callback1 handle1, bool *used = NULL);
125  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool &var, bool value, bool *used = NULL);
127  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, Glib::ustring &var, bool *used = NULL);
129  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, std::string &var, bool *used = NULL);
131  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, unsigned long &var, bool *used = NULL);
133  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, long &var, bool *used = NULL);
135  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, unsigned &var, bool *used = NULL);
137  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, int &var, bool *used = NULL);
139  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, unsigned short &var, bool *used = NULL);
141  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, short &var, bool *used = NULL);
143  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, float &var, bool *used = NULL);
145  option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, double &var, bool *used = NULL);
148  template <typename _T> option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, std::list <_T> &list);
149  private:
150  friend class args;
151  enum opt_t {NEED_ARG, NO_ARG, OPT_ARG};
152  void setup (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, bool have_default, opt_t have_arg, callback0 handle0, callback1 handle1, bool *used);
153  void call (bool is_double, char const *optarg);
154  char m_shortopt;
155  Glib::ustring m_longopt;
156  Glib::ustring m_help;
157  bool m_have_default;
158  Glib::ustring m_default;
159  opt_t m_have_arg;
160  callback0 m_handle0;
161  callback1 m_handle1;
162  bool *m_used;
163  bool m_is_used;
164  static void l_set (bool is_double, Glib::ustring const &arg, Glib::ustring *var);
165  static void l_setstd (bool is_double, Glib::ustring const &arg, std::string *var);
166  static void l_setint (bool is_double, Glib::ustring const &arg, int *var);
167  static void l_setuint (bool is_double, Glib::ustring const &arg, unsigned *var);
168  static void l_setlint (bool is_double, Glib::ustring const &arg, long *var);
169  static void l_setulint (bool is_double, Glib::ustring const &arg, unsigned long *var);
170  static void l_setsint (bool is_double, Glib::ustring const &arg, short *var);
171  static void l_setusint (bool is_double, Glib::ustring const &arg, unsigned short *var);
172  static void l_setbool (bool is_double, bool val, bool *var);
173  static void l_setfloat (bool is_double, Glib::ustring const &arg, float *var);
174  static void l_setdfloat (bool is_double, Glib::ustring const &arg, double *var);
175  template <typename _T>
176  static void l_setlist (bool is_double, Glib::ustring const &arg, std::list <_T> *list);
177  };
178 
179  template <unsigned size_> args::args (int &argc, char **&argv,
180  option (&o)[size_],
181  int min_args, int max_args,
182  Glib::ustring const &description)
183  {
184  startfunc;
185  struct ::option longopts[size_ + 4];
186  l_setup (argc, argv, o, size_, min_args, max_args, description, longopts);
187  }
188 
189 
190  template <typename _T>
191  args::option::option (char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, std::list <_T> &list)
192  {
193  startfunc;
194  setup (shortopt, longopt, help_line, false, NEED_ARG, callback0 (),
195  sigc::bind (sigc::ptr_fun (&l_setlist <_T>), &list), NULL);
196  }
197 
198  template <typename _T>
199  void args::option::l_setlist (bool is_double, Glib::ustring const &arg, std::list <_T> *list)
200  {
201  startfunc;
202  (void *)&is_double;
203  list->push_back (_T () );
204  option tmp ('x', "x", "x", false, list->back () );
205  tmp.m_handle1 (false, arg);
206  }
207 }
208 
209 #endif
sigc::slot2< void, bool, Glib::ustring const & > callback1
Callback for options with an argument.
Definition: args.hh:117
sigc::slot1< void, bool > callback0
Callback for options without an argument.
Definition: args.hh:115
Define an option which can be given to the program.
Definition: args.hh:111
unsigned size() const
The number of non-option arguments.
std::vector< std::string >::const_iterator begin() const
Iterate over the non-option arguments.
option(char shortopt, Glib::ustring const &longopt, Glib::ustring const &help_line, callback0 handle, bool *used=NULL)
Option has no argument and calls a function.
std::string const & operator[](unsigned idx) const
Get the non-option arguments.
std::vector< std::string >::const_iterator end() const
Iterate over the non-option arguments.
args(int &argc, char **&argv, int min_args, int max_args, Glib::ustring const &description, Glib::ustring const &copyright_years=COPYRIGHT_YEARS, Glib::ustring const &copyright_email=(COPYRIGHT_EMAIL[0]== '\0'?PACKAGE_BUGREPORT:COPYRIGHT_EMAIL), Glib::ustring const &programmer=COPYRIGHT_AUTHOR, Glib::ustring const &email=PACKAGE_BUGREPORT, char const *programname=PACKAGE_NAME, char const *packagename=PACKAGE_TARNAME, char const *version=PACKAGE_VERSION)
Commandline and configuration file parsing helper.
Definition: args.hh:65