JWS C++ Library
C++ language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00054 #ifndef OPTION_H 00055 #define OPTION_H 00056 00057 00058 #include <jwsc++/config.h> 00059 00060 #include <iostream> 00061 #include <list> 00062 00063 #include "jwsc++/base/exception.h" 00064 00065 00066 using std::istream; 00067 using std::ostream; 00068 using std::list; 00069 00070 00071 namespace jwscxx { 00072 namespace base { 00073 00074 00101 class Option 00102 { 00103 public: 00104 00106 Option(char s_name, const char* l_name, const char* desc) 00107 throw (Arg_error); 00108 00109 00111 virtual ~Option() { } 00112 00113 00115 virtual int process(int argc, char** argv) const 00116 throw (Arg_error) = 0; 00117 00118 00120 virtual void print(ostream& out, int width) const throw (Arg_error) = 0; 00121 00122 00123 protected: 00124 00129 bool is_s_name_option(const char* arg) const; 00130 00131 00136 bool is_l_name_option(const char* arg) const; 00137 00138 00139 protected: 00140 00146 char s_name; 00147 00153 const char* l_name; 00154 00160 const char* desc; 00161 }; 00162 00163 00164 00165 00192 class Option_with_arg : public Option 00193 { 00194 public: 00195 00197 Option_with_arg 00198 ( 00199 char s_name, 00200 const char* l_name, 00201 const char* desc, 00202 void (*func)(const char* arg) throw (Arg_error) 00203 ) 00204 throw (Arg_error); 00205 00206 00208 int process(int argc, char** argv) const throw (Arg_error); 00209 00210 00212 void print(ostream& out, int width) const throw (Arg_error); 00213 00214 00215 protected: 00216 00218 const char* get_l_name_arg(const char* str) const; 00219 00220 00221 protected: 00222 00228 void (*func)(const char* arg) throw (Arg_error); 00229 }; 00230 00231 00232 00233 00260 class Option_no_arg : public Option 00261 { 00262 public: 00263 00265 Option_no_arg 00266 ( 00267 char s_name, 00268 const char* l_name, 00269 const char* desc, 00270 void (*func)(void) 00271 ) 00272 throw (Arg_error); 00273 00274 00276 int process(int argc, char** argv) const throw (Arg_error); 00277 00278 00280 void print(ostream& out, int width) const throw (Arg_error); 00281 00282 00283 protected: 00284 00286 void (*func)(void); 00287 }; 00288 00289 00290 00291 00297 class Options 00298 { 00299 public: 00300 00302 Options() : opts() { } 00303 00304 00306 ~Options(); 00307 00308 00310 void add(Option* opt); 00311 00312 00317 int process(int argc, char** argv) const throw (Arg_error); 00318 00319 00321 void process(const char* fname) const throw (IO_error, Arg_error); 00322 00323 00328 void process(istream& in) const throw (Arg_error); 00329 00330 00332 void print(ostream& out=cerr, int width=20) const; 00333 00334 00335 private: 00336 00338 bool is_an_option(const char* arg) const; 00339 00341 list<Option *> opts; 00342 }; 00343 00344 00345 }} 00346 00347 00348 #endif