libassa
3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // $Id: CmdLineOpts.h,v 1.7 2005/10/12 02:28:58 vlg Exp $ 00004 //------------------------------------------------------------------------------ 00005 // CmdLineOpts.h 00006 //------------------------------------------------------------------------------ 00007 // Copyright (C) 2000,2005 Vladislav Grinchenko 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Library General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2 of the License, or (at your option) any later version. 00013 //------------------------------------------------------------------------------ 00014 00015 #ifndef CMD_LINE_OPTS_H 00016 #define CMD_LINE_OPTS_H 00017 00018 #include "assa/Assure.h" 00019 00020 #include <string> 00021 #include <vector> 00022 using std::string; 00023 using std::vector; 00024 00025 namespace ASSA { 00026 00027 class CmdLineOpts; 00028 00039 class Option { 00040 public: 00041 friend class CmdLineOpts; 00042 00047 enum type_t { 00048 string_t=0, 00049 int_t, 00050 uint_t, 00051 long_t, 00052 ulong_t, 00053 double_t, 00054 float_t, 00055 flag_t, 00056 func_t, 00057 func_one_t, 00058 none_t 00059 }; 00060 00061 private: 00063 Option (); 00064 00066 Option (char shopt_, const string& lopt_, type_t type_, void* val_); 00067 00069 void dump () const; 00070 00072 const char* type_c_str (); 00073 00074 private: 00076 char m_short_name; 00077 00079 string m_long_name; 00080 00082 type_t m_type; 00083 00085 void* m_val; 00086 }; 00087 00088 inline 00089 Option::Option () : 00090 m_short_name (' '), m_long_name (""), m_type (none_t), m_val (NULL) 00091 { 00092 /* empty */ 00093 } 00094 00095 inline 00096 Option::Option (char shopt_, const string& lopt_, type_t type_, void* val_) : 00097 m_short_name (shopt_), m_long_name (lopt_), 00098 m_type (type_), m_val (val_) 00099 { 00100 trace_with_mask("Option::Option", CMDLINEOPTS); 00101 } 00102 00103 /*----------------------------------------------------------------------------*/ 00104 class IniFile; 00105 00113 class CmdLineOpts 00114 { 00115 public: 00116 typedef void (* OPTS_FUNC) (void); 00117 typedef void (* OPTS_FUNC_ONE) (const string&); 00118 00119 typedef vector<Option> OptionSet; 00120 00122 CmdLineOpts (); 00123 00125 virtual ~CmdLineOpts () { 00126 trace_with_mask ("CmdLineOpts::~CmdLineOpts", CMDLINEOPTS); 00127 } 00128 00135 bool add_flag_opt (const char c, const string& s, bool* f); 00136 00143 bool add_opt (const char c, const string& s, string* str); 00144 00151 bool add_opt (const char c, const string& s, int* i); 00152 00159 bool add_opt (const char c, const string& s, unsigned int* ui); 00160 00167 bool add_opt (const char c, const string& s, long* l); 00168 00175 bool add_opt (const char c, const string& s, unsigned long* ul); 00176 00183 bool add_opt (const char c, const string& s, double* d); 00184 00191 bool add_opt (const char c, const string& s, float* f); 00192 00204 bool add_opt (const char c_, const string& s_, OPTS_FUNC f_); 00205 00216 bool add_opt (const char c_, const string& s_, OPTS_FUNC_ONE f_); 00217 00223 bool rm_opt (const char c_, const string& s_); 00224 00228 bool parse_args (const char* argv[]); 00229 00237 int parse_config_file (IniFile& inifile_); 00238 00242 const char* get_opt_error () const; 00243 00252 static void str_to_argv (const string& src_, int& argc_, char**& argv_); 00253 00255 static void free_argv (char**& argv_); 00256 00258 void dump () const; 00259 00260 protected: 00262 bool is_valid (const char sopt_, const string& lopt_); 00263 00265 void set_error_none (); 00266 00268 bool assign (Option* node_, const char* op_); 00269 00271 Option* find_option (const char* str_); 00272 00274 Option* find_option (const char letter_); 00275 00280 virtual void pos_arg (const char* arg_); 00281 00282 private: 00284 OptionSet m_opts_set; 00285 00287 string m_error; 00288 }; 00289 00290 00291 inline void 00292 CmdLineOpts::pos_arg (const char* /*arg_*/) { /* no-opt*/ } 00293 00294 inline 00295 CmdLineOpts::CmdLineOpts () : m_opts_set (), m_error ("") 00296 { 00297 trace_with_mask("CmdLineOpts::CmdLineOpts", CMDLINEOPTS); 00298 set_error_none (); 00299 } 00300 00301 inline void 00302 CmdLineOpts::set_error_none () 00303 { 00304 trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS); 00305 m_error = ""; 00306 } 00307 00308 inline const char* 00309 CmdLineOpts::get_opt_error () const 00310 { 00311 return (m_error.c_str ()); 00312 } 00313 00314 00315 } // end namespace ASSA 00316 00317 #endif /* CMD_LINE_OPTS_H */