anyoption.h

Go to the documentation of this file.
00001 #ifndef _ANYOPTION_H
00002 #define _ANYOPTION_H
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <stdlib.h>
00007 #include <string>
00008 
00009 #define COMMON_OPT  1
00010 #define COMMAND_OPT     2
00011 #define FILE_OPT    3
00012 #define COMMON_FLAG     4
00013 #define COMMAND_FLAG    5
00014 #define FILE_FLAG   6
00015 
00016 #define COMMAND_OPTION_TYPE     1
00017 #define COMMAND_FLAG_TYPE   2
00018 #define FILE_OPTION_TYPE    3
00019 #define FILE_FLAG_TYPE      4 
00020 #define UNKNOWN_TYPE        5
00021 
00022 #define DEFAULT_MAXOPTS     10
00023 #define MAX_LONG_PREFIX_LENGTH  2
00024 
00025 #define DEFAULT_MAXUSAGE    3
00026 #define DEFAULT_MAXHELP         10  
00027 
00028 #define TRUE_FLAG "true" 
00029 
00030 using namespace std;
00031 
00032 class AnyOption 
00033 {
00034 
00035 public: /* the public interface */
00036     AnyOption();
00037     AnyOption(int maxoptions ); 
00038     AnyOption(int maxoptions , int maxcharoptions); 
00039     ~AnyOption();
00040 
00041     /* 
00042          * following set methods specifies the  
00043      * special characters and delimiters 
00044      * if not set traditional defaults will be used
00045          */
00046 
00047     void setCommandPrefixChar( char _prefix );   /* '-' in "-w" */
00048     void setCommandLongPrefix( char *_prefix );  /* '--' in "--width" */
00049     void setFileCommentChar( char _comment );    /* '#' in shellscripts */
00050     void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */
00051 
00052     /* 
00053      * provide the input for the options
00054          * like argv[] for commndline and the 
00055          * option file name  to use;
00056      */
00057 
00058     void useCommandArgs( int _argc, char **_argv );
00059     void useFiileName( const char *_filename );
00060 
00061     /* 
00062          * turn off the POSIX style options 
00063          * this means anything starting with a '-' or "--"
00064          * will be considered a valid option 
00065          * which alo means you cannot add a bunch of 
00066          * POIX options chars together like "-lr"  for "-l -r"
00067          * 
00068          */
00069 
00070     void noPOSIX();
00071 
00072     /*
00073          * prints warning verbose if you set anything wrong 
00074          */
00075     void setVerbose();
00076 
00077 
00078     /* 
00079          * there are two types of options  
00080          *
00081          * Option - has an associated value ( -w 100 )
00082          * Flag  - no value, just a boolean flag  ( -nogui )
00083          * 
00084      * the options can be either a string ( GNU style )
00085          * or a character ( traditional POSIX style ) 
00086          * or both ( --width, -w )
00087          *
00088          * the options can be common to the commandline and 
00089          * the optionfile, or can belong only to either of 
00090          * commandline and optionfile
00091          *
00092          * following set methods, handle all the aboove 
00093      * cases of options.
00094          */
00095 
00096     /* options comman to command line and option file */
00097     void setOption( const char *opt_string );
00098     void setOption( char  opt_char );
00099     void setOption( const char *opt_string , char opt_char );
00100     void setFlag( const char *opt_string );
00101     void setFlag( char  opt_char );
00102     void setFlag( const char *opt_string , char opt_char );
00103 
00104     /* options read from commandline only */
00105     void setCommandOption( const char *opt_string );
00106     void setCommandOption( char  opt_char );
00107     void setCommandOption( const char *opt_string , char opt_char );
00108     void setCommandFlag( const char *opt_string );
00109     void setCommandFlag( char  opt_char );
00110     void setCommandFlag( const char *opt_string , char opt_char );
00111 
00112     /* options read from an option file only  */
00113     void setFileOption( const char *opt_string );
00114     void setFileOption( char  opt_char );
00115     void setFileOption( const char *opt_string , char opt_char );
00116     void setFileFlag( const char *opt_string );
00117     void setFileFlag( char  opt_char );
00118     void setFileFlag( const char *opt_string , char opt_char );
00119 
00120     /*
00121          * process the options, registerd using 
00122          * useCommandArgs() and useFileName();
00123          */
00124     void processOptions();  
00125     void processCommandArgs();
00126     void processCommandArgs( int max_args );
00127     bool processFile();
00128 
00129     /*
00130          * process the specified options 
00131          */
00132     void processCommandArgs( int _argc, char **_argv );
00133     void processCommandArgs( int _argc, char **_argv, int max_args );
00134     bool processFile( const char *_filename );
00135     
00136     /*
00137          * get the value of the options 
00138      * will return NULL if no value is set 
00139          */
00140     char *getValue( const char *_option );
00141     bool  getFlag( const char *_option );
00142     char *getValue( char _optchar );
00143     bool  getFlag( char _optchar );
00144 
00145     /*
00146      * Print Usage
00147      */
00148     void printUsage();
00149     void printAutoUsage();
00150     void addUsage( const char *line );
00151     void printHelp();
00152         /* print auto usage printing for unknown options or flag */
00153     void autoUsagePrint(bool flag);
00154     
00155     /* 
00156          * get the argument count and arguments sans the options
00157          */
00158     int   getArgc();
00159     char* getArgv( int index );
00160     bool  hasOptions();
00161 
00162 private: /* the hidden data structure */
00163     int argc;       /* commandline arg count  */
00164     char **argv;        /* commndline args */
00165     const char* filename;   /* the option file */
00166     char* appname;  /* the application name from argv[0] */
00167 
00168     int *new_argv;      /* arguments sans options (index to argv) */
00169     int new_argc;       /* argument count sans the options */
00170     int max_legal_args;     /* ignore extra arguments */
00171 
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     bool hasoptions;
00219     bool autousage;
00220 
00221 private: /* the hidden utils */
00222     void init();    
00223     void init(int maxopt, int maxcharopt ); 
00224     bool alloc();
00225     void cleanup();
00226     bool valueStoreOK();
00227 
00228     /* grow storage arrays as required */
00229     bool doubleOptStorage();
00230     bool doubleCharStorage();
00231     bool doubleUsageStorage();
00232 
00233     bool setValue( const char *option , char *value );
00234     bool setFlagOn( const char *option );
00235     bool setValue( char optchar , char *value);
00236     bool setFlagOn( char optchar );
00237 
00238     void addOption( const char* option , int type );
00239     void addOption( char optchar , int type );
00240     void addOptionError( const char *opt);
00241     void addOptionError( char opt);
00242     bool findFlag( char* value );
00243     void addUsageError( const char *line );
00244     bool CommandSet();
00245     bool FileSet();
00246     bool POSIX();
00247 
00248     char parsePOSIX( char* arg );
00249     int parseGNU( char *arg );
00250     bool matchChar( char c );
00251     int matchOpt( char *opt );
00252 
00253     /* dot file methods */
00254     char *readFile();
00255     char *readFile( const char* fname );
00256     bool consumeFile( char *buffer );
00257     void processLine( char *theline, int length );
00258     char *chomp( char *str );
00259     void valuePairs( char *type, char *value ); 
00260     void justValue( char *value );
00261 
00262     void printVerbose( const char *msg );
00263     void printVerbose( char *msg );
00264     void printVerbose( char ch );
00265     void printVerbose( );
00266 
00267 
00268 };
00269 
00270 #endif /* ! _ANYOPTION_H */