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