MTK++ Latest version: 0.2.0
00001 00008 #ifndef COMMLINEOPTIONS_H 00009 #define COMMLINEOPTIONS_H 00010 00011 #include <iostream> 00012 #include <fstream> 00013 #include <stdlib.h> 00014 #include <string.h> 00015 00016 #define COMMON_OPT 1 00017 #define COMMAND_OPT 2 00018 #define FILE_OPT 3 00019 #define COMMON_FLAG 4 00020 #define COMMAND_FLAG 5 00021 #define FILE_FLAG 6 00022 00023 #define COMMAND_OPTION_TYPE 1 00024 #define COMMAND_FLAG_TYPE 2 00025 #define FILE_OPTION_TYPE 3 00026 #define FILE_FLAG_TYPE 4 00027 #define UNKNOWN_TYPE 5 00028 00029 #define DEFAULT_MAXOPTS 10 00030 #define MAX_LONG_PREFIX_LENGTH 2 00031 00032 #define DEFAULT_MAXUSAGE 3 00033 #define DEFAULT_MAXHELP 10 00034 00035 #define TRUE_FLAG "true" 00036 00037 class commLineOptions 00038 { 00039 00040 public: /* the public interface */ 00041 commLineOptions(); 00042 commLineOptions(int maxoptions ); 00043 commLineOptions(int maxoptions , int maxcharoptions); 00044 ~commLineOptions(); 00045 00046 /* 00047 * following set methods specifies the 00048 * special characters and delimiters 00049 * if not set traditional defaults will be used 00050 */ 00051 00052 void setCommandPrefixChar( char _prefix ); /* '-' in "-w" */ 00053 void setCommandLongPrefix( char *_prefix ); /* '--' in "--width" */ 00054 void setFileCommentChar( char _comment ); /* '#' in shellscripts */ 00055 void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */ 00056 00057 /* 00058 * provide the input for the options 00059 * like argv[] for commndline and the 00060 * option file name to use; 00061 */ 00062 void useCommandArgs( int _argc, char **_argv ); 00063 void useFiileName( const char *_filename ); 00064 00065 /* 00066 * turn off the POSIX style options 00067 * this means anything starting with a '-' or "--" 00068 * will be considered a valid option 00069 * which alo means you cannot add a bunch of 00070 * POIX options chars together like "-lr" for "-l -r" 00071 * 00072 */ 00073 00074 void noPOSIX(); 00075 00076 /* 00077 * prints warning verbose if you set anything wrong 00078 */ 00079 void setVerbose(); 00080 00081 /* 00082 * there are two types of options 00083 * 00084 * Option - has an associated value ( -w 100 ) 00085 * Flag - no value, just a boolean flag ( -nogui ) 00086 * 00087 * the options can be either a string ( GNU style ) 00088 * or a character ( traditional POSIX style ) 00089 * or both ( --width, -w ) 00090 * 00091 * the options can be common to the commandline and 00092 * the optionfile, or can belong only to either of 00093 * commandline and optionfile 00094 * 00095 * following set methods, handle all the aboove 00096 * cases of options. 00097 */ 00098 00099 /* options comman to command line and option file */ 00100 void setOption( const char *opt_string ); 00101 void setOption( char opt_char ); 00102 void setOption( const char *opt_string , char opt_char ); 00103 void setFlag( const char *opt_string ); 00104 void setFlag( char opt_char ); 00105 void setFlag( const char *opt_string , char opt_char ); 00106 00107 /* options read from commandline only */ 00108 void setCommandOption( const char *opt_string ); 00109 void setCommandOption( char opt_char ); 00110 void setCommandOption( const char *opt_string , char opt_char ); 00111 void setCommandFlag( const char *opt_string ); 00112 void setCommandFlag( char opt_char ); 00113 void setCommandFlag( const char *opt_string , char opt_char ); 00114 00115 /* options read from an option file only */ 00116 void setFileOption( const char *opt_string ); 00117 void setFileOption( char opt_char ); 00118 void setFileOption( const char *opt_string , char opt_char ); 00119 void setFileFlag( const char *opt_string ); 00120 void setFileFlag( char opt_char ); 00121 void setFileFlag( const char *opt_string , char opt_char ); 00122 00123 /* 00124 * process the options, registerd using 00125 * useCommandArgs() and useFileName(); 00126 */ 00127 void processOptions(); 00128 void processCommandArgs(); 00129 void processCommandArgs( int max_args ); 00130 bool processFile(); 00131 00132 /* 00133 * process the specified options 00134 */ 00135 void processCommandArgs( int _argc, char **_argv ); 00136 void processCommandArgs( int _argc, char **_argv, int max_args ); 00137 bool processFile( const char *_filename ); 00138 00139 /* 00140 * get the value of the options 00141 * will return NULL if no value is set 00142 */ 00143 char *getValue( const char *_option ); 00144 bool getFlag( const char *_option ); 00145 char *getValue( char _optchar ); 00146 bool getFlag( char _optchar ); 00147 00148 /* 00149 * Print Usage 00150 */ 00151 void printUsage(); 00152 void addUsage( const char *line ); 00153 void printHelp(); 00154 void noUsage(); 00155 void usageOn(); 00156 00157 /* 00158 * get the argument count and arguments sans the options 00159 */ 00160 int getArgc(); 00161 char* getArgv( int index ); 00162 00163 protected: /* the hidden data structure */ 00164 int argc; /* commandline arg count */ 00165 char **argv; /* commndline args */ 00166 const char* filename; /* the option file */ 00167 char* appname; /* the application name from argv[0] */ 00168 00169 int *new_argv; /* arguments sans options (index to argv) */ 00170 int new_argc; /* argument count sans the options */ 00171 int max_legal_args; /* ignore extra arguments */ 00172 00173 /* option strings storage + indexing */ 00174 int max_options; /* maximum number of options */ 00175 const char **options; /* storage */ 00176 int *optiontype; /* type - common, command, file */ 00177 int *optionindex; /* index into value storage */ 00178 int option_counter; /* counter for added options */ 00179 00180 /* option chars storage + indexing */ 00181 int max_char_options; /* maximum number options */ 00182 char *optionchars; /* storage */ 00183 int *optchartype; /* type - common, command, file */ 00184 int *optcharindex; /* index into value storage */ 00185 int optchar_counter; /* counter for added options */ 00186 00187 /* values */ 00188 char **values; /* common value storage */ 00189 int g_value_counter; /* globally updated value index LAME! */ 00190 00191 /* help and usage */ 00192 const char **usage; /* usage */ 00193 int max_usage_lines; /* max usage lines reseverd */ 00194 int usage_lines; /* number of usage lines */ 00195 00196 bool command_set;/* if argc/argv were provided */ 00197 bool file_set; /* if a filename was provided */ 00198 bool mem_allocated; /* if memory allocated in init() */ 00199 bool posix_style; /* enables to turn off POSIX style options */ 00200 bool verbose; /* silent|verbose */ 00201 bool print_usage; /* usage verbose */ 00202 bool print_help; /* help verbose */ 00203 00204 char opt_prefix_char; /* '-' in "-w" */ 00205 char long_opt_prefix[MAX_LONG_PREFIX_LENGTH]; /* '--' in "--width" */ 00206 char file_delimiter_char;/* ':' in width : 100 */ 00207 char file_comment_char; /* '#' in "#this is a comment" */ 00208 char equalsign; 00209 char comment; 00210 char delimiter; 00211 char endofline; 00212 char whitespace; 00213 char nullterminate; 00214 00215 bool set; //was static member 00216 bool once; //was static member 00217 00218 protected: /* the hidden utils */ 00219 void init(); 00220 void init(int maxopt, int maxcharopt ); 00221 bool alloc(); 00222 void cleanup(); 00223 bool valueStoreOK(); 00224 00225 /* grow storage arrays as required */ 00226 bool doubleOptStorage(); 00227 bool doubleCharStorage(); 00228 bool doubleUsageStorage(); 00229 00230 bool setValue( const char *option , char *value ); 00231 bool setFlagOn( const char *option ); 00232 bool setValue( char optchar , char *value); 00233 bool setFlagOn( char optchar ); 00234 00235 void addOption( const char* option , int type ); 00236 void addOption( char optchar , int type ); 00237 void addOptionError( const char *opt); 00238 void addOptionError( char opt); 00239 bool findFlag( char* value ); 00240 void addUsageError( const char *line ); 00241 bool CommandSet(); 00242 bool FileSet(); 00243 bool POSIX(); 00244 bool USAGE(); 00245 00246 char parsePOSIX( char* arg ); 00247 int parseGNU( char *arg ); 00248 bool matchChar( char c ); 00249 int matchOpt( char *opt ); 00250 00251 /* dot file methods */ 00252 char *readFile(); 00253 char *readFile( const char* fname ); 00254 bool consumeFile( char *buffer ); 00255 void processLine( char *theline, int length ); 00256 char *chomp( char *str ); 00257 void valuePairs( char *type, char *value ); 00258 void justValue( char *value ); 00259 00260 void printVerbose( const char *msg ); 00261 void printVerbose( char *msg ); 00262 void printVerbose( char ch ); 00263 void printVerbose( ); 00264 }; 00265 00266 #endif // COMMLINEOPTIONS_H
Generated on Fri Dec 23 2011 09:28:49 for MTK++ by Doxygen 1.7.5.1