00001 #ifndef OPTION_H_INCLUDED 00002 #define OPTION_H_INCLUDED 00003 00004 #ifndef ERR_H 00005 #include "Err.h" 00006 #endif 00007 00008 #ifndef __LIST__ 00009 #include <list> 00010 #endif 00011 00012 namespace rs { 00013 00038 namespace cmdline { 00039 00040 // -------------------------------------------------------------------------------- 00041 // class CmdLineHelpText 00042 // -------------------------------------------------------------------------------- 00044 class CmdLineHelpText : rs_boost::noncopyable { 00045 std::string helptext; // helptext printed out by '-?' 00046 protected: 00054 CmdLineHelpText(const char *helptext_) { 00055 helptext = helptext_; 00056 } 00057 virtual ~CmdLineHelpText() {} 00058 public: 00060 virtual std::string get_helptext() const { return helptext; } 00061 }; 00062 00063 // -------------------------------------------------------------------------------- 00064 // class CmdLineOption 00065 // -------------------------------------------------------------------------------- 00067 class CmdLineOption : public virtual CmdLineHelpText { 00068 bool used; // was option given on command line or set via set()? 00069 char optChar; // the option character ('c' for option '-c') 00070 public: 00080 CmdLineOption(char optChar_, const char *helptext_); 00081 virtual ~CmdLineOption(); 00082 00085 operator bool() const { return used; } 00086 00088 virtual bool reallyUsed() const { return (bool)*this; } 00089 00091 void set() { used = true; } 00092 00094 virtual void clear() { used = false; } 00095 00097 char get_optChar() const { return optChar; } 00098 00099 protected: 00100 friend class ProgramInfo; 00101 virtual void print(std::ostream& out) const; 00102 virtual bool takes_argument() const { return false; } 00103 }; 00104 00105 // -------------------------------------------------------------------------------- 00106 // class CmdLineArgument 00107 // -------------------------------------------------------------------------------- 00109 class CmdLineArgument : public virtual CmdLineHelpText { 00110 std::string name; // name of argument (as referred in helptext_) 00111 std::string value; // argument given on command line 00112 bool has_default_value; // if argument has a default value 00113 bool really_used; // if argument was given on command line 00114 00115 public: 00126 CmdLineArgument(const char *name_, const char *helptext_, const char *value_=0, bool is_real_arg=true); 00127 virtual ~CmdLineArgument(); 00128 00129 bool value_is_set() const { return value.length()>0; } 00130 00133 operator bool() const { return value_is_set(); } 00134 00137 virtual bool reallyUsed() const { return really_used; } 00138 00141 virtual std::string get() { return value; } 00142 00145 virtual int getNumeric(); 00146 00149 virtual double getDouble(); 00150 00154 virtual void set(std::string value_, bool set_default_value=false) { 00155 value = value_; 00156 really_used = !set_default_value; 00157 } 00158 00160 void clear() { 00161 if (value_is_set()) { 00162 value = ""; 00163 has_default_value = false; 00164 really_used = false; 00165 } 00166 } 00167 00169 virtual std::string get_name() const { return std::string("<")+name+'>'; } 00170 00171 protected: 00172 friend class ProgramInfo; 00173 00174 bool has_default() const { return has_default_value; } 00175 void set_has_default() { if (value_is_set()) has_default_value = true; } 00176 00177 virtual std::string get_helptext() const { 00178 if (!has_default()) return CmdLineHelpText::get_helptext(); 00179 return CmdLineHelpText::get_helptext() + " [Default='" + value + "']"; 00180 } 00181 }; 00182 00196 typedef bool (*CmdLineArgumentPredicate)(const std::string *arg); 00197 00198 // ----------------------------------------------------------------- 00199 // class RepeatableCmdLineArgument : public CmdLineArgument 00200 // ----------------------------------------------------------------- 00204 class RepeatableCmdLineArgument : public CmdLineArgument { 00205 private: 00206 int min_occur; 00207 int max_occur; 00208 std::list<std::string> repeated_values; 00209 std::list<std::string> seen_values; 00210 CmdLineArgumentPredicate isArg; 00211 int count; // arguments scanned 00212 00213 protected: 00215 RepeatableCmdLineArgument(const char *name_, const char *helptext_, int min_occur_, int max_occur_, CmdLineArgumentPredicate isArg_, bool is_real_arg); 00216 public: 00217 virtual ~RepeatableCmdLineArgument() {} 00218 00226 std::string get(); 00227 00235 virtual void set(std::string value_, bool set_default_value=false) { 00236 if (value_is_set()) repeated_values.push_back(value_); 00237 else CmdLineArgument::set(value_, set_default_value); 00238 ++count; 00239 } 00240 00242 void reset(); 00243 00245 virtual std::string get_name() const; 00246 00247 protected: 00248 friend class ProgramInfo; 00249 00250 int get_min_occur() const { return min_occur; } 00251 int get_max_occur() const { return max_occur; } 00252 int get_count() const { return count; } 00253 }; 00254 00258 bool acceptAnyArgument(const std::string *arg); 00259 00260 // ------------------------------------------------------------------------- 00261 // class OptionalCmdLineArgument : public RepeatableCmdLineArgument 00262 // ------------------------------------------------------------------------- 00264 class OptionalCmdLineArgument : public RepeatableCmdLineArgument { 00265 public: 00279 OptionalCmdLineArgument(const char *name_, const char *helptext_, CmdLineArgumentPredicate isArg_ = acceptAnyArgument, int max_occur_=1) 00280 : RepeatableCmdLineArgument(name_, helptext_, 0, max_occur_, isArg_, true) 00281 , CmdLineHelpText(helptext_) 00282 {} 00283 virtual ~OptionalCmdLineArgument() {} 00284 }; 00285 00286 // ------------------------------------------------------------------------- 00287 // class RepeatedCmdLineArgument : public RepeatableCmdLineArgument 00288 // ------------------------------------------------------------------------- 00290 class RepeatedCmdLineArgument : public RepeatableCmdLineArgument { 00291 public: 00306 RepeatedCmdLineArgument(const char *name_, const char *helptext_, CmdLineArgumentPredicate isArg_, int min_occur_=1, int max_occur_=-1) 00307 : RepeatableCmdLineArgument(name_, helptext_, min_occur_, max_occur_, isArg_, true) 00308 , CmdLineHelpText(helptext_) 00309 {} 00310 00311 virtual ~RepeatedCmdLineArgument() {} 00312 }; 00313 00314 00315 // -------------------------------------------------------------------------------- 00316 // class CmdLineArgumentOption : public CmdLineOption, public CmdLineArgument 00317 // -------------------------------------------------------------------------------- 00319 class CmdLineArgumentOption : public CmdLineOption, public CmdLineArgument { 00320 public: 00332 CmdLineArgumentOption(char optChar_, const char *argname_, const char *helptext_, const char *value_=0); 00333 virtual ~CmdLineArgumentOption(); 00334 00339 virtual void set(std::string value_, bool set_default_value=false) { 00340 CmdLineOption::set(); 00341 CmdLineArgument::set(value_, set_default_value); // set default argument 00342 } 00343 00345 virtual void clear() { 00346 CmdLineOption::clear(); 00347 CmdLineArgument::clear(); 00348 } 00349 00354 virtual bool reallyUsed() const { return CmdLineArgument::reallyUsed(); } 00355 00356 protected: 00357 virtual void print(std::ostream& out) const; 00358 virtual bool takes_argument() const { return true; } 00359 // virtual string get_helptext() const { return CmdLineHelpText::get_helptext(); } 00360 }; 00361 00362 // ---------------------------------------------------------------------------------------------------- 00363 // class RepeatedCmdLineArgumentOption 00364 // ---------------------------------------------------------------------------------------------------- 00369 class RepeatedCmdLineArgumentOption : public CmdLineOption, public RepeatableCmdLineArgument { 00370 private: 00371 char separator; 00372 00373 public: 00381 RepeatedCmdLineArgumentOption(char optChar_, const char *argname_, char separator_, const char *helptext_, const char *value_=0); 00382 virtual ~RepeatedCmdLineArgumentOption() {} 00383 00388 virtual void set(std::string value_, bool set_default_value=false); 00389 00397 std::string get() { return RepeatableCmdLineArgument::get(); } 00398 00400 virtual void clear() { 00401 CmdLineOption::clear(); 00402 RepeatableCmdLineArgument::clear(); 00403 } 00404 00406 virtual std::string get_name() const; 00407 00412 virtual bool reallyUsed() const { return CmdLineArgument::reallyUsed(); } 00413 00414 protected: 00415 virtual void print(std::ostream& out) const; 00416 virtual bool takes_argument() const { return true; } 00417 // virtual string get_helptext() const { return CmdLineHelpText::get_helptext(); } 00418 }; 00419 00420 00422 typedef CmdLineOption** CmdLineOptionArray; 00423 00424 00425 // -------------------------------------------------------------------------------- 00426 // class CmdLineAnnotation 00427 // -------------------------------------------------------------------------------- 00429 class CmdLineAnnotation : public virtual CmdLineHelpText { 00430 private: 00431 int print_infrontof_option; 00432 bool spaced; // print empty lines above and below 00433 public: 00444 CmdLineAnnotation(const char *helptext_="", bool spaced_=false); 00445 virtual ~CmdLineAnnotation() {} 00446 00447 protected: 00448 friend class ProgramInfo; 00449 int get_print_infrontof_option() const { return print_infrontof_option; } 00450 bool print_spaced() const { return spaced; } 00451 }; 00452 00453 00454 // -------------------------------------------------------------------------------- 00455 // class ProgramInfo 00456 // -------------------------------------------------------------------------------- 00460 class ProgramInfo : rs_boost::singleton<ProgramInfo> { 00461 std::string program_name; 00462 int main_version; 00463 int sub_version; 00464 std::string program_description; 00465 std::string creation_date; 00466 00467 bool any_option_given; 00468 00469 void setProgramPaths(const char *argv0); 00470 00471 std::string program_path; // home directory of the program 00472 std::string call_path; // the directory the program was called from 00473 00474 public: 00492 ProgramInfo(const char *program_name_, int main_version_, int sub_version_, 00493 const char *program_description_, const char *creation_date_ = __DATE__); 00494 ~ProgramInfo() {} 00495 00506 void init(int argc_, char* argv_[]); // do not change! 00507 00526 void exclude(CmdLineOptionArray opt1, CmdLineOptionArray opt2); 00527 void exclude(CmdLineOption& opt1, CmdLineOptionArray opt2) { 00528 CmdLineOption *optList1[] = { &opt1, 0 }; 00529 exclude(optList1, opt2); 00530 } 00531 void exclude(CmdLineOptionArray opt1, CmdLineOption& opt2) { 00532 CmdLineOption *optList2[] = { &opt2, 0 }; 00533 exclude(opt1, optList2); 00534 } 00535 void exclude(CmdLineOption& opt1, CmdLineOption& opt2) { 00536 CmdLineOption *optList1[] = { &opt1, 0 }; 00537 CmdLineOption *optList2[] = { &opt2, 0 }; 00538 exclude(optList1, optList2); 00539 } 00540 00542 00545 void needOne(CmdLineOptionArray options); 00546 00550 void neededBy(const CmdLineOption& drug, const CmdLineOption& addict); 00551 00554 void infos(std::ostream& out) const; 00555 00558 bool optionsSpecified() const { return any_option_given; } 00559 00564 void throw_Error(const std::string& msg) const; 00565 00567 const std::string& CallPath() const; 00568 00570 const std::string& ProgramPath() const; 00571 }; 00572 00573 }; 00574 }; 00575 00576 #else 00577 #error Option.h included twice 00578 #endif 00579
Contact me in case of errors or questions. This documentation is powered by . |
(C) 2000-2002 |