]> git.tdb.fi Git - libs/core.git/blob - source/core/getopt.h
Use lexical_cast instead of stringstream in GetOpt
[libs/core.git] / source / core / getopt.h
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_CORE_GETOPT_H_
9 #define MSP_CORE_GETOPT_H_
10
11 #include <list>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15 #include <msp/strings/lexicalcast.h>
16
17 namespace Msp {
18
19 class usage_error: public std::runtime_error
20 {
21 private:
22         std::string help_;
23
24 public:
25         usage_error(const std::string &w, const std::string &h = std::string()): std::runtime_error(w), help_(h) { }
26         ~usage_error() throw() { }
27
28         const char *help() const throw() { return help_.c_str(); }
29 };
30
31
32 class GetOpt
33 {
34 public:
35         enum ArgType
36         {
37                 NO_ARG,
38                 OPTIONAL_ARG,
39                 REQUIRED_ARG
40         };
41         
42         class OptBase
43         {
44         protected:
45                 char shrt;
46                 std::string lng;
47                 ArgType arg_type;
48                 unsigned seen_count;
49                 std::string help;
50                 std::string metavar;
51
52                 OptBase(char, const std::string &, ArgType);
53         public:
54                 virtual ~OptBase() { }
55
56                 OptBase &set_help(const std::string &);
57                 OptBase &set_help(const std::string &, const std::string &);
58                 char get_short() const { return shrt; }
59                 const std::string &get_long() const { return lng; }
60                 ArgType get_arg_type() const { return arg_type; }
61                 const std::string &get_help() const { return help; }
62                 const std::string &get_metavar() const { return metavar; }
63                 unsigned get_seen_count() const { return seen_count; }
64                 void process();
65                 void process(const std::string &);
66         protected:
67                 virtual void store() = 0;
68                 virtual void store(const std::string &) = 0;
69         };
70
71 private:
72         template<typename T>
73         class Option: public OptBase
74         {
75         public:
76                 Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
77
78                 virtual void store() { }
79
80                 virtual void store(const std::string &a)
81                 {
82                         try
83                         {
84                                 data = lexical_cast<T>(a);
85                         }
86                         catch(const lexical_error &e)
87                         {
88                                 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
89                         }
90                 }
91         private:
92                 T &data;
93         };
94
95         template<typename T>
96         class ListOption: public OptBase
97         {
98         public:
99                 ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d)
100                 { if(arg_type!=REQUIRED_ARG) throw std::invalid_argument("ListOption arg_type!=REQUIRED"); }
101
102                 virtual void store() { }
103
104                 virtual void store(const std::string &a)
105                 {
106                         try
107                         {
108                                 data.push_back(lexical_cast<typename T::value_type>(a));
109                         }
110                         catch(const lexical_error &e)
111                         {
112                                 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
113                         }
114                 }
115         private:
116                 T &data;
117         };
118
119         bool help;
120         std::list<OptBase *> opts;
121         std::vector<std::string> args;
122
123 public:
124         GetOpt();
125         ~GetOpt();
126
127         const std::vector<std::string> &get_args() const { return args; }
128
129         template<typename T>
130         OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
131         { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
132         
133         template<typename T>
134         OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
135         { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
136         
137         template<typename T>
138         OptBase &add_option(const std::string &l, T &d, ArgType a)
139         { return add_option(0, l, d, a); }
140
141 private:
142         OptBase &get_option(char);
143         OptBase &get_option(const std::string &);
144
145 public:
146         /** Processes argc/argv style command line arguments.  The contents of argv
147         will be unchanged; use get_args to access non-option arguments. */
148         void operator()(unsigned, const char *const *);
149
150 private:
151         /** Processes a long option.  Returns the number of arguments eaten. */
152         unsigned process_long(const char *const *);
153
154         /** Processes short options.  Returns the number of arguments eaten. */
155         unsigned process_short(const char *const *);
156
157 public:
158         /** Generates a single line that describes known options. */
159         std::string generate_usage(const std::string &) const;
160
161         /** Generates help for known options in tabular format, one option per
162         line.  The returned string will have a linefeed at the end. */
163         std::string generate_help() const;
164 };
165
166 template<> inline void GetOpt::Option<bool>::store()     { data = true; }
167 template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
168
169 template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
170 { data = a; }
171
172 template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
173 { data.push_back(a); }
174
175 } // namespace Msp
176
177 #endif