File: option.hh

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (335 lines) | stat: -rw-r--r-- 9,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#ifndef __OPTION_HH__
#define __OPTION_HH__

/*
 * Infrastructure for parsing options.
 *
 * This can be used on its own with concrete_option_set::operator()(), or
 * used with something like options.{cc,hh} and option_set. The former is
 * very simple to do, while the latter should allow slightly better code
 * structure for more involved uses.
 */

#include <stdexcept>
#include <map>
#include <set>
#include "vector.hh"

#include <boost/function.hpp>
#include "lexical_cast.hh"

#include "sanity.hh"
#include "vocab.hh"

// The types to represent the command line's parameters.
class arg_type : public utf8 {
public:
  explicit arg_type(void) : utf8() {}
  arg_type(std::string const & s, origin::type f) : utf8(s, f) {}
  explicit arg_type(utf8 const & u) : utf8(u) {}
};
template <>
inline void dump(arg_type const & a, std::string & out) { out = a(); }
typedef std::vector< arg_type > args_vector;

namespace option {
  // Base for errors thrown by this code.
  struct option_error : public std::invalid_argument
  {
    option_error(std::string const & str);
  };
  struct unknown_option : public option_error
  {
    unknown_option(std::string const & opt);
  };
  struct missing_arg : public option_error
  {
    missing_arg(std::string const & opt);
  };
  // -ofoo or --opt=foo when the option doesn't take an argument
  struct extra_arg : public option_error
  {
    extra_arg(std::string const & opt);
  };
  // thrown by from_command_line when setting an option fails
  // by either boost::bad_lexical_cast or bad_arg_internal
  struct bad_arg : public option_error
  {
    bad_arg(std::string const & opt, arg_type const & arg);
    bad_arg(std::string const & opt,
            arg_type const & arg,
            std::string const & reason);
  };
  // from_command_line() catches this and boost::bad_lexical_cast
  // and converts them to bad_arg exceptions
  struct bad_arg_internal
  {
    std::string reason;
    bad_arg_internal(std::string const & str = "");
  };

  // Split a "long,s" option name into long and short names.
  void splitname(char const * from, std::string & name, std::string & n);

  // An option that can be set and reset.
  struct concrete_option
  {
    char const * description;
    std::string longname;
    std::string shortname;
    bool has_arg;
    boost::function<void (std::string)> setter;
    boost::function<void ()> resetter;

    concrete_option();
    concrete_option(char const * names,
                    char const * desc,
                    bool arg,
                    boost::function<void (std::string)> set,
                    boost::function<void ()> reset);

    bool operator<(concrete_option const & other) const;
  };

  // A group of options, which can be set from a command line
  // and can produce a usage string.
  struct concrete_option_set
  {
    std::set<concrete_option> options;
    concrete_option_set();
    concrete_option_set(std::set<concrete_option> const & other);
    concrete_option_set(concrete_option const & opt);

    // for building a concrete_option_set directly (as done in unit_tests.cc),
    // rather than using intermediate machinery like in options*
    concrete_option_set &
    operator()(char const * names,
               char const * desc,
               boost::function<void ()> set,
               boost::function<void ()> reset = 0);
    concrete_option_set &
    operator()(char const * names,
               char const * desc,
               boost::function<void (std::string)> set,
               boost::function<void ()> reset = 0);

    concrete_option_set operator | (concrete_option_set const & other) const;
    void reset() const;
    void get_usage_strings(std::vector<std::string> & names,
                           std::vector<std::string> & descriptions,
                           unsigned int & maxnamelen) const;
    void from_command_line(args_vector & args, bool allow_xargs = true);
    void from_command_line(int argc, char const * const * argv);
    void from_key_value_pairs(std::vector<std::pair<std::string, std::string> > const & keyvals);
  };
  concrete_option_set
  operator | (concrete_option const & a, concrete_option const & b);

  // used by the setter() functions below
  template<typename T>
  struct setter_class
  {
    T & item;
    setter_class(T & i)
      : item(i)
    {}
    void operator()(std::string s)
    {
      item = boost::lexical_cast<T>(s);
    }
  };
  template<>
  struct setter_class<bool>
  {
    bool & item;
    setter_class(bool & i)
      : item(i)
    {}
    void operator()()
    {
      item = true;
    }
  };
  template<typename T>
  struct setter_class<std::vector<T> >
  {
    std::vector<T> & items;
    setter_class(std::vector<T> & i)
      : items(i)
    {}
    void operator()(std::string s)
    {
      items.push_back(boost::lexical_cast<T>(s));
    }
  };
  template<typename T>
  struct resetter_class
  {
    T & item;
    T value;
    resetter_class(T & i, T const & v)
      : item(i), value(v)
    {}
    void operator()()
    {
      item = value;
    }
  };

  // convenience functions to generate a setter for a var
  template<typename T> inline
  boost::function<void(std::string)> setter(T & item)
  {
    return setter_class<T>(item);
  }
  inline boost::function<void()> setter(bool & item)
  {
    return setter_class<bool>(item);
  }
  // convenience function to generate a resetter for a var
  template<typename T> inline
  boost::function<void()> resetter(T & item, T const & value = T())
  {
    return resetter_class<T>(item, value);
  }

  // because std::bind1st can't handle producing a nullary functor
  template<typename T>
  struct binder_only
  {
    T * obj;
    boost::function<void(T*)> fun;
    binder_only(boost::function<void(T*)> const & f, T * o)
      : obj(o), fun(f)
      {}
    void operator()()
    {
      fun(obj);
    }
  };

  // Options that need to be attached to some other object
  // in order for set and reset to be meaningful.
  template<typename T>
  struct option
  {
    char const * description;
    char const * names;
    bool has_arg;
    boost::function<void (T*, std::string)> setter;
    boost::function<void (T*)> resetter;

    option(char const * name,
           char const * desc,
           bool arg,
           void(T::*set)(std::string),
           void(T::*reset)())
    {
      I((name && name[0]) || (desc && desc[0]));
      description = desc;
      names = name;
      has_arg = arg;
      setter = set;
      resetter = reset;
    }

    concrete_option instantiate(T * obj) const
    {
      concrete_option out;
      out.description = description;
      splitname(names, out.longname, out.shortname);
      out.has_arg = has_arg;

      if (setter)
        out.setter = std::bind1st(setter, obj);
      if (resetter)
        out.resetter = binder_only<T>(resetter, obj);
      return out;
    }

    bool operator<(option const & other) const
    {
      if (names != other.names)
        return names < other.names;
      return description < other.description;
    }
  };

  // A group of unattached options, which can be given an object
  // to attach themselves to.
  template<typename T>
  struct option_set
  {
    std::set<option<T> > options;
    option_set(){}
    option_set(option_set<T> const & other)
      : options(other.options)
    {}
    option_set(option<T> const & opt)
    {
      options.insert(opt);
    }

    option_set(char const * name,
               char const * desc,
               bool arg,
               void(T::*set)(std::string),
               void(T::*reset)())
    {
      options.insert(option<T>(name, desc, arg, set, reset));
    }
    concrete_option_set instantiate(T * obj) const
    {
      std::set<concrete_option> out;
      for (typename std::set<option<T> >::const_iterator i = options.begin();
           i != options.end(); ++i)
        out.insert(i->instantiate(obj));
      return out;
    }
    option_set<T> operator | (option_set<T> const & other) const
    {
      option_set<T> combined;
      std::set_union(options.begin(), options.end(),
                     other.options.begin(), other.options.end(),
                     std::inserter(combined.options, combined.options.begin()));
      return combined;
    }
    option_set<T> operator - (option_set<T> const & other) const
    {
      option_set<T> combined;
      std::set_difference(options.begin(), options.end(),
                          other.options.begin(), other.options.end(),
                          std::inserter(combined.options,
                                        combined.options.begin()));
      return combined;
    }
    bool empty() const {return options.empty();}
  };
  template<typename T>
  option_set<T>
  operator | (option<T> const & a, option<T> const & b)
  {
    return option_set<T>(a) | b;
  }

}


#endif

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: