|
LeoArg.hhGo to the documentation of this file.00001 #ifndef LeoArg_hh 00002 #define LeoArg_hh 00003 00004 #include <list> 00005 #include <string> 00006 00008 00012 class LeoArg 00013 { 00014 public: 00015 typedef list<std::string>ArgList; 00016 typedef list<std::string>::iterator ArgListIt; 00017 00018 struct Option 00019 { 00020 std::string long_option; 00021 std::string short_option; 00022 ArgList arglist; 00023 bool bool_value; 00024 int int_num; 00025 double double_num; 00026 std::string str; 00027 bool flag; 00028 00029 Option( std::string long_option, std::string short_option ); 00030 }; 00031 00032 typedef std::list<Option>OptionList; 00033 typedef OptionList::iterator OptionListIt; 00034 00035 struct InvalidOption 00036 { 00037 Option option; 00038 std::string err; 00039 InvalidOption( Option &o, std::string e ) : option( o ), err( e ){} 00040 }; 00041 00042 struct InvalidSetting 00043 { 00044 std::string err; 00045 InvalidSetting( std::string e ) : err( e ){} 00046 }; 00047 00048 struct Setting 00049 { 00050 int argc; 00051 char **argv; 00052 std::string long_option_switch; 00053 std::string short_option_switch; 00054 mutable OptionList option_list; 00055 bool sos; 00056 00057 Setting( int argc, char **argv, std::string long_option_switch, std::string short_option_switch ) 00058 : argc( argc ), argv( argv ), long_option_switch( long_option_switch ), short_option_switch( short_option_switch ) 00059 {} 00060 00061 Setting() 00062 : argc(0), argv(0) 00063 {} 00064 00066 void add( const Option& option ) { option_list.push_back( option ); } 00067 00069 /*** if you want to set varios short options and its easyer to set one short option string. 00070 eg.: 00071 the options: -s -p -q 00072 the sort option string can be: -spq 00073 00074 \Warning only one character short options are possible 00075 \Warning the short option string doesn't looks like the short option of an option 00076 \Warning the option can only be a boolean option 00077 */ 00078 void short_option_string( bool short_os ) { sos = short_os; } 00079 bool short_option_string() const { return sos; } 00080 }; 00081 00082 private: 00083 Setting setting; 00084 00085 public: 00086 LeoArg( int argc, char **argv, std::string long_option_switch = "--", std::string short_option_switch = "-" ); 00087 LeoArg( Setting& setting ); 00088 00090 void short_option_string( bool short_os ) { setting.short_option_string(short_os); } 00091 00093 bool short_option_string() const { return setting.short_option_string(); } 00094 00095 Setting get_setting() const { return setting; } 00096 00097 bool has_parameters() const { return setting.argc > 1; } 00098 bool get_option( Option& option ); 00099 00101 00104 bool get_first( std::string& ret_value ) const; 00105 00107 00110 bool get_first( ArgList& arglist ) const; 00111 00113 00116 bool get_last( std::string & ret_value ) const; 00117 00119 00122 bool get_last( ArgList& arglist ) const; 00123 00124 std::string get_prog_name() const { return std::string(setting.argv[0]); } 00125 00127 void clear_cache() { setting.option_list.clear(); } 00128 private: 00129 bool parse_option( Option& option ) const; 00130 void correct_option( Option& option ) const; 00131 void correct_setting(); 00132 bool find_option( Option& option ) const; 00133 bool is_in_sos( const Option& option ) const; 00134 int has_sos() const; 00135 }; 00136 00137 00138 extern "C" { 00140 /*** For autoconf check add this line to your configure.in file: 00141 AC_CHECK_LIB(LeoArg, leoarg, echo -n;, echo not found ;exit, -lstdc++) 00142 */ 00143 char leoarg(); 00144 00145 /*** For autoconf check add this line to your configure.in file: 00146 AC_CHECK_LIB(LeoArg, leoarg_version_2_2, echo "LeoArg >= 2.2 (yes)", \ 00147 echo "LeoArg >= 2.2 (no)"; exit, -lstdc++ ) 00148 */ 00149 char leoarg_version_2_2(); 00150 } 00151 00152 #endif
LeoArg (C) under the terms of the
GPL
by
King Leo (Martin Oberzalek)
Documentation created by Doxygen 1.2.2 |