26 exit(1); |
26 exit(1); |
27 } |
27 } |
28 |
28 |
29 ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv), |
29 ArgParser::ArgParser(int argc, char **argv) :_argc(argc), _argv(argv), |
30 _command_name(argv[0]) { |
30 _command_name(argv[0]) { |
31 option("-help","Print a short help message",_showHelp,this); |
31 refOption("-help","Print a short help message",_showHelp,this); |
32 synonym("help","-help"); |
32 synonym("help","-help"); |
33 synonym("h","-help"); |
33 synonym("h","-help"); |
34 |
34 |
35 } |
35 } |
36 |
36 |
37 ArgParser &ArgParser::option(const std::string &name, |
37 ArgParser::~ArgParser() |
|
38 { |
|
39 for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
|
40 if(i->second.self_delete) |
|
41 switch(i->second.type) { |
|
42 case BOOL: |
|
43 delete i->second.bool_p; |
|
44 break; |
|
45 case STRING: |
|
46 delete i->second.string_p; |
|
47 break; |
|
48 case DOUBLE: |
|
49 delete i->second.double_p; |
|
50 break; |
|
51 case INTEGER: |
|
52 delete i->second.int_p; |
|
53 break; |
|
54 case UNKNOWN: |
|
55 break; |
|
56 case FUNC: |
|
57 break; |
|
58 } |
|
59 } |
|
60 |
|
61 |
|
62 ArgParser &ArgParser::intOption(const std::string &name, |
|
63 const std::string &help, |
|
64 int value, bool obl) |
|
65 { |
|
66 ParData p; |
|
67 p.int_p=new int(value); |
|
68 p.self_delete=true; |
|
69 p.help=help; |
|
70 p.type=INTEGER; |
|
71 p.mandatory=obl; |
|
72 p.self_delete=true; |
|
73 _opts[name]=p; |
|
74 return *this; |
|
75 } |
|
76 |
|
77 ArgParser &ArgParser::doubleOption(const std::string &name, |
|
78 const std::string &help, |
|
79 double value, bool obl) |
|
80 { |
|
81 ParData p; |
|
82 p.double_p=new double(value); |
|
83 p.self_delete=true; |
|
84 p.help=help; |
|
85 p.type=DOUBLE; |
|
86 p.mandatory=obl; |
|
87 _opts[name]=p; |
|
88 return *this; |
|
89 } |
|
90 |
|
91 ArgParser &ArgParser::boolOption(const std::string &name, |
|
92 const std::string &help, |
|
93 bool value, bool obl) |
|
94 { |
|
95 ParData p; |
|
96 p.bool_p=new bool(value); |
|
97 p.self_delete=true; |
|
98 p.help=help; |
|
99 p.type=BOOL; |
|
100 p.mandatory=obl; |
|
101 _opts[name]=p; |
|
102 |
|
103 value = false; |
|
104 |
|
105 return *this; |
|
106 } |
|
107 |
|
108 ArgParser &ArgParser::stringOption(const std::string &name, |
|
109 const std::string &help, |
|
110 std::string value, bool obl) |
|
111 { |
|
112 ParData p; |
|
113 p.string_p=new std::string(value); |
|
114 p.self_delete=true; |
|
115 p.help=help; |
|
116 p.type=STRING; |
|
117 p.mandatory=obl; |
|
118 _opts[name]=p; |
|
119 return *this; |
|
120 } |
|
121 |
|
122 ArgParser &ArgParser::refOption(const std::string &name, |
38 const std::string &help, |
123 const std::string &help, |
39 int &value, bool obl) |
124 int &value, bool obl) |
40 { |
125 { |
41 ParData p; |
126 ParData p; |
42 p.int_p=&value; |
127 p.int_p=&value; |
|
128 p.self_delete=false; |
43 p.help=help; |
129 p.help=help; |
44 p.type=INTEGER; |
130 p.type=INTEGER; |
45 p.mandatory=obl; |
131 p.mandatory=obl; |
46 _opts[name]=p; |
132 _opts[name]=p; |
47 return *this; |
133 return *this; |
48 } |
134 } |
49 |
135 |
50 ArgParser &ArgParser::option(const std::string &name, |
136 ArgParser &ArgParser::refOption(const std::string &name, |
51 const std::string &help, |
137 const std::string &help, |
52 double &value, bool obl) |
138 double &value, bool obl) |
53 { |
139 { |
54 ParData p; |
140 ParData p; |
55 p.double_p=&value; |
141 p.double_p=&value; |
|
142 p.self_delete=false; |
56 p.help=help; |
143 p.help=help; |
57 p.type=DOUBLE; |
144 p.type=DOUBLE; |
58 p.mandatory=obl; |
145 p.mandatory=obl; |
59 _opts[name]=p; |
146 _opts[name]=p; |
60 return *this; |
147 return *this; |
61 } |
148 } |
62 |
149 |
63 ArgParser &ArgParser::option(const std::string &name, |
150 ArgParser &ArgParser::refOption(const std::string &name, |
64 const std::string &help, |
151 const std::string &help, |
65 bool &value, bool obl) |
152 bool &value, bool obl) |
66 { |
153 { |
67 ParData p; |
154 ParData p; |
68 p.bool_p=&value; |
155 p.bool_p=&value; |
|
156 p.self_delete=false; |
69 p.help=help; |
157 p.help=help; |
70 p.type=BOOL; |
158 p.type=BOOL; |
71 p.mandatory=obl; |
159 p.mandatory=obl; |
72 _opts[name]=p; |
160 _opts[name]=p; |
73 |
161 |
74 value = false; |
162 value = false; |
75 |
163 |
76 return *this; |
164 return *this; |
77 } |
165 } |
78 |
166 |
79 ArgParser &ArgParser::option(const std::string &name, |
167 ArgParser &ArgParser::refOption(const std::string &name, |
80 const std::string &help, |
168 const std::string &help, |
81 std::string &value, bool obl) |
169 std::string &value, bool obl) |
82 { |
170 { |
83 ParData p; |
171 ParData p; |
84 p.string_p=&value; |
172 p.string_p=&value; |
|
173 p.self_delete=false; |
85 p.help=help; |
174 p.help=help; |
86 p.type=STRING; |
175 p.type=STRING; |
87 p.mandatory=obl; |
176 p.mandatory=obl; |
88 _opts[name]=p; |
177 _opts[name]=p; |
89 return *this; |
178 return *this; |
90 } |
179 } |
91 |
180 |
92 ArgParser &ArgParser::option(const std::string &name, |
181 ArgParser &ArgParser::refOption(const std::string &name, |
93 const std::string &help, |
182 const std::string &help, |
94 void (*func)(void *),void *data) |
183 void (*func)(void *),void *data) |
95 { |
184 { |
96 ParData p; |
185 ParData p; |
97 p.func_p.p=func; |
186 p.func_p.p=func; |
98 p.func_p.data=data; |
187 p.func_p.data=data; |
|
188 p.self_delete=false; |
99 p.help=help; |
189 p.help=help; |
100 p.type=FUNC; |
190 p.type=FUNC; |
101 p.mandatory=false; |
191 p.mandatory=false; |
102 _opts[name]=p; |
192 _opts[name]=p; |
103 return *this; |
193 return *this; |
104 } |
194 } |
|
195 |
105 ArgParser &ArgParser::optionGroup(const std::string &group, |
196 ArgParser &ArgParser::optionGroup(const std::string &group, |
106 const std::string &opt) |
197 const std::string &opt) |
107 { |
198 { |
108 Opts::iterator i = _opts.find(opt); |
199 Opts::iterator i = _opts.find(opt); |
109 if(i==_opts.end()) exit(3); ///\todo throw exception instead |
200 if(i==_opts.end()) exit(3); ///\todo throw exception instead |