Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members

simpleOptions.h

00001 // File:    SimpleOptions.h
00002 // Author:  Brodie Thiesfield
00003 // Licence: Freeware (see below)
00004 //
00005 // FEATURES
00006 //  * accepts getopt command line specification
00007 //  * simple
00008 //  * platform independent
00009 //  * case dependent or independent matching
00010 //
00011 // LIMITATIONS
00012 //  * it isn't getopt
00013 //  * all options on the command line must use flags
00014 //      ie  "program [-o] <input_file>" won't work
00015 //      use "program [-o] -i <input_file>" instead
00016 //
00017 // NOTES
00018 //  * uses exceptions
00019 //  * if using case-insensitive option matching, ensure that the string
00020 //    passed through as a_pszOptions is all lowercase
00021 //
00022 // EXAMPLE USAGE
00023 //
00024 //    try
00025 //    {
00026 //        CSimpleOptions cl( argc, argv, "Oa:" );
00027 //        while ( cl.next() )
00028 //        {
00029 //            switch ( cl.option() )
00030 //            {
00031 //            case 'O':
00032 //                bOption = true;
00033 //                break;
00034 //            case 'a':
00035 //                strArgument = cl.arg();
00036 //            }
00037 //        }
00038 //    }
00039 //    catch ( const SIMPLE_OPTIONS_EXCEPTION & e )
00040 //    {
00041 //        fprintf( stderr, "Invalid argument: %s\n", e.what() );
00042 //        return 1;
00043 //    }
00044 //
00045 // DISCLAIMER
00046 // This code is released as freeware, you can do with it whatever you 
00047 // like: use it, modify it, distribute it, sell it, delete it, or send 
00048 // it to your mother-in-law.  I withhold no rights of any kind, and I 
00049 // make no promises that this code will work correctly or at all.  
00050 // Use it completely at your own risk.
00051 //
00052 
00053 #ifndef INCLUDED_CSimpleOptions
00054 #define INCLUDED_CSimpleOptions
00055 
00056 // redefine this exception type if you want to use something else
00057 // it must take a string as a construction parameter
00058 #ifndef SIMPLE_OPTIONS_EXCEPTION
00059 #include <stdexcept>
00060 #define SIMPLE_OPTIONS_EXCEPTION  std::invalid_argument
00061 #endif
00062 
00063 class CSimpleOptions
00064 {
00065 public:
00066     CSimpleOptions( int           a_argc, 
00067                     char *        a_argv[], 
00068                     const char *  a_pszOptions, 
00069                     bool          a_bCaseSensitive = true )
00070     {
00071         m_argc = a_argc;
00072         m_argv = a_argv;
00073         m_pszOptions = a_pszOptions;
00074         m_bCaseSensitive = a_bCaseSensitive;
00075         m_pszArg = 0;
00076         m_nCurr = 1;
00077     }
00078 
00079     // throws SIMPLE_OPTIONS_EXCEPTION
00080     char next()
00081     {
00082         // are we there yet?
00083         m_pszArg = 0;
00084         if ( m_nCurr >= m_argc )
00085         {
00086             return 0;
00087         }
00088 
00089         // ensure we have a switch in form "-c"
00090         if ( (m_argv[m_nCurr][0] != '-' && m_argv[m_nCurr][0] != '/')
00091              || m_argv[m_nCurr][1] == 0 || m_argv[m_nCurr][1] == ':' 
00092              || m_argv[m_nCurr][2] != 0 )
00093         {
00094             throw SIMPLE_OPTIONS_EXCEPTION( m_argv[m_nCurr] );
00095         }
00096 
00097         // find this argument
00098         m_pszArg = strchr( m_pszOptions, 
00099             m_bCaseSensitive ? m_argv[m_nCurr][1] : tolower( m_argv[m_nCurr][1] ) );
00100         if ( !m_pszArg )
00101         {
00102             throw SIMPLE_OPTIONS_EXCEPTION( m_argv[m_nCurr] );
00103         }
00104 
00105         // if we need an arg, ensure we have one
00106         if ( *(m_pszArg+1) == ':' ) 
00107         {
00108             if ( m_nCurr + 1 >= m_argc )
00109             {
00110                 m_pszArg = 0;
00111                 throw SIMPLE_OPTIONS_EXCEPTION( m_argv[m_nCurr] );
00112             }
00113             m_nCurr += 2;
00114         }
00115         else
00116         {
00117             ++m_nCurr;
00118         }
00119 
00120         return *m_pszArg;
00121     }
00122 
00123     // throw SIMPLE_OPTIONS_EXCEPTION
00124     const char * arg()
00125     {
00126         if ( *(m_pszArg+1) != ':' || m_nCurr <= 1 ) 
00127         {
00128             throw SIMPLE_OPTIONS_EXCEPTION( "" );
00129         }
00130         return m_argv[m_nCurr-1];
00131     }
00132 
00133     char option()
00134     {
00135         return m_pszArg ? *m_pszArg : (char) 0;
00136     }
00137 
00138 private:
00139     int          m_argc;
00140     char **      m_argv;
00141     const char * m_pszOptions;
00142     bool         m_bCaseSensitive;
00143     char *       m_pszArg;
00144     int          m_nCurr;
00145 };
00146 
00147 #endif // INCLUDED_CSimpleOptions
00148 

Generated on Mon Aug 15 03:39:31 2005 for mxcpc by  doxygen 1.4.2-20050421