]> git.tdb.fi Git - libs/core.git/blob - source/core/getopt.h
Assimilate exceptions and RefPtr from mspmisc
[libs/core.git] / source / core / getopt.h
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef MSP_CORE_GETOPT_H_
8 #define MSP_CORE_GETOPT_H_
9
10 #include <list>
11 #include <sstream>
12 #include <string>
13 #include "error.h"
14
15 namespace Msp {
16
17 class GetOpt
18 {
19 public:
20         enum ArgType
21         {
22                 NO_ARG,
23                 OPTIONAL_ARG,
24                 REQUIRED_ARG
25         };
26         
27         class OptBase
28         {
29         public:
30                 OptBase           &set_help(const std::string &h) { help=h; return *this; }
31                 char              get_short() const               { return shrt; }
32                 const std::string &get_long() const               { return lng; }
33                 ArgType           get_arg_type() const            { return arg_type; }
34                 const std::string &get_help() const               { return help; }
35                 unsigned          get_seen_count() const          { return seen_count; }
36                 virtual void      process()=0;
37                 virtual void      process(const std::string &)=0;
38                 virtual ~OptBase() { }
39         protected:
40                 char        shrt;
41                 std::string lng;
42                 ArgType     arg_type;
43                 unsigned    seen_count;
44                 std::string help;
45
46                 OptBase(char s, const std::string &l, ArgType a): shrt(s), lng(l), arg_type(a), seen_count(0) { }
47         };
48
49         const std::list<std::string> &get_args() const { return args; }
50
51         template<typename T>
52         OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG)
53         { opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
54         
55         template<typename T>
56         OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a=REQUIRED_ARG)
57         { opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
58         
59         template<typename T>
60         OptBase &add_option(const std::string &l, T &d, ArgType a)
61         { return add_option(0, l, d, a); }
62
63         std::string generate_usage(const std::string &) const;
64         std::string generate_help() const;
65         void operator()(unsigned, const char *const *);
66
67         ~GetOpt();
68 private:
69         template<typename T>
70         class Option: public OptBase
71         {
72         public:
73                 Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
74
75                 virtual void process()
76                 {
77                         if(arg_type==REQUIRED_ARG)
78                                 throw UsageError("--"+lng+" requires an argument");
79                         process_();
80                         ++seen_count;
81                 }
82
83                 virtual void process(const std::string &a)
84                 {
85                         if(arg_type==NO_ARG)
86                                 throw UsageError("--"+lng+" takes no argument");
87
88                         T tmp;
89                         std::istringstream ss(a);
90                         ss>>tmp;
91                         if(ss.fail())
92                                 throw UsageError("Invalid argument for --"+lng);
93
94                         data=tmp;
95                         ++seen_count;
96                 }
97         private:
98                 T &data;
99
100                 void process_() { }
101         };
102
103         template<typename T>
104         class ListOption: public OptBase
105         {
106         public:
107                 ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d)
108                 { if(arg_type!=REQUIRED_ARG) throw Exception("ListOption with arg_type!=REQUIRED makes no sense"); }
109
110                 virtual void process()
111                 {
112                         throw UsageError("--"+lng+" requires an argument");
113                 }
114
115                 virtual void process(const std::string &a)
116                 {
117                         typename T::value_type tmp;
118                         std::istringstream ss(a);
119                         ss>>tmp;
120                         if(ss.fail())
121                                 throw UsageError("Invalid argument for --"+lng);
122
123                         data.push_back(tmp);
124                         ++seen_count;
125                 }
126         private:
127                 T &data;
128         };
129
130         std::list<OptBase *>   opts;
131         std::list<std::string> args;
132
133         OptBase &get_option(char);
134         OptBase &get_option(const std::string &);
135         unsigned process_long(const char *const *);
136         unsigned process_short(const char *const *);
137 };
138
139 template<> inline void GetOpt::Option<bool>::process_()     { data=true; }
140 template<> inline void GetOpt::Option<unsigned>::process_() { ++data; }
141
142 } // namespace Msp
143
144 #endif