args.h

Go to the documentation of this file.
00001 /*
00002  * args.h
00003  *
00004  * Program Argument Parsing class
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 20385 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00032  */
00033 
00034 #ifndef _PARGLIST
00035 #define _PARGLIST
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00045 class PArgList : public PObject
00046 {
00047   PCLASSINFO(PArgList, PObject);
00048 
00049   public:
00062     PArgList(
00063       const char * theArgPtr = NULL,        
00064       const char * argumentSpecPtr = NULL,  
00065       PBoolean optionsBeforeParams = PTrue       
00066     );
00068     PArgList(
00069       const PString & theArgStr,             
00070       const char * argumentSpecPtr = NULL,   
00071       PBoolean optionsBeforeParams = PTrue        
00072     );
00074     PArgList(
00075       const PString & theArgStr,             
00076       const PString & argumentSpecStr,       
00077       PBoolean optionsBeforeParams = PTrue        
00078     );
00080     PArgList(
00081       int theArgc,                           
00082       char ** theArgv,                       
00083       const char * argumentSpecPtr = NULL,   
00084       PBoolean optionsBeforeParams = PTrue        
00085     );
00087     PArgList(
00088       int theArgc,                           
00089       char ** theArgv,                       
00090       const PString & argumentSpecStr,       
00091       PBoolean optionsBeforeParams = PTrue        
00092     );
00094 
00099     virtual void PrintOn(
00100       ostream & strm  
00101     ) const;
00102 
00106     virtual void ReadFrom(
00107       istream & strm  
00108     );
00110 
00115     void SetArgs(
00116       const PString & theArgStr 
00117     );
00119     void SetArgs(
00120       int theArgc,     
00121       char ** theArgv  
00122     );
00124     void SetArgs(
00125       const PStringArray & theArgs 
00126     );
00127 
00150     virtual PBoolean Parse(
00151       const char * theArgumentSpec,    
00152       PBoolean optionsBeforeParams = PTrue  
00153     );
00155     virtual PBoolean Parse(
00156       const PString & theArgumentStr,  
00157       PBoolean optionsBeforeParams = PTrue  
00158     );
00160 
00168     virtual PINDEX GetOptionCount(
00169       char optionChar        
00170     ) const;
00172     virtual PINDEX GetOptionCount(
00173       const char * optionStr 
00174     ) const;
00176     virtual PINDEX GetOptionCount(
00177       const PString & optionName 
00178     ) const;
00179 
00185     PBoolean HasOption(
00186       char optionChar             
00187     ) const;
00189     PBoolean HasOption(
00190       const char * optionStr     
00191     ) const;
00193     PBoolean HasOption(
00194       const PString & optionName 
00195     ) const;
00196 
00205     virtual PString GetOptionString(
00206       char optionChar,          
00207       const char * dflt = NULL  
00208     ) const;
00210     virtual PString GetOptionString(
00211       const char * optionStr,   
00212       const char * dflt = NULL  
00213     ) const;
00215     virtual PString GetOptionString(
00216       const PString & optionName, 
00217       const char * dflt = NULL    
00218     ) const;
00219 
00227     PINDEX GetCount() const;
00228 
00233     PStringArray GetParameters(
00234       PINDEX first = 0,
00235       PINDEX last = P_MAX_INDEX
00236     ) const;
00237 
00242     PString GetParameter(
00243       PINDEX num   
00244     ) const;
00245 
00251     PString operator[](
00252       PINDEX num   
00253     ) const;
00254 
00258     void Shift(
00259       int sh 
00260     );
00261 
00265     PArgList & operator<<(
00266       int sh 
00267     );
00268 
00272     PArgList & operator>>(
00273       int sh 
00274     );
00276 
00283     virtual void IllegalArgumentIndex(
00284       PINDEX idx 
00285     ) const;
00286 
00291     virtual void UnknownOption(
00292       const PString & option   
00293     ) const;
00294 
00300     virtual void MissingArgument(
00301       const PString & option  
00302     ) const;
00304 
00305   protected:
00307     PStringArray argumentArray;
00309     PString      optionLetters;
00311     PStringArray optionNames;
00313     PIntArray    optionCount;
00315     PStringArray optionString;
00317     PIntArray    parameterIndex;
00319     int          shift;
00320 
00321   private:
00322     PBoolean ParseOption(PINDEX idx, PINDEX offset, PINDEX & arg, const PIntArray & canHaveOptionString);
00323     PINDEX GetOptionCountByIndex(PINDEX idx) const;
00324     PString GetOptionStringByIndex(PINDEX idx, const char * dflt) const;
00325 };
00326 
00327 
00328 #ifdef P_CONFIG_FILE
00329 
00333 class PConfigArgs : public PArgList
00334 {
00335     PCLASSINFO(PConfigArgs, PArgList);
00336   public:
00339     PConfigArgs(
00340       const PArgList & args   
00341     );
00343 
00351     virtual PINDEX GetOptionCount(
00352       char optionChar  
00353     ) const;
00355     virtual PINDEX GetOptionCount(
00356       const char * optionStr 
00357     ) const;
00359     virtual PINDEX GetOptionCount(
00360       const PString & optionName 
00361     ) const;
00362 
00371     virtual PString GetOptionString(
00372       char optionChar,          
00373       const char * dflt = NULL  
00374     ) const;
00375 
00377     virtual PString GetOptionString(
00378       const char * optionStr,   
00379       const char * dflt = NULL  
00380     ) const;
00381 
00383     virtual PString GetOptionString(
00384       const PString & optionName, 
00385       const char * dflt = NULL    
00386     ) const;
00388 
00397     void Save(
00398       const PString & optionName   
00399     );
00400 
00403     void SetSectionName(
00404       const PString & section 
00405     ) { sectionName = section; }
00406 
00409     const PString & GetSectionName() const { return sectionName; }
00410 
00414     void SetNegationPrefix(
00415       const PString & prefix 
00416     ) { negationPrefix = prefix; }
00417 
00421     const PString & GetNegationPrefix() const { return negationPrefix; }
00423 
00424 
00425   protected:
00426     PString CharToString(char ch) const;
00427     PConfig config;
00428     PString sectionName;
00429     PString negationPrefix;
00430 };
00431 
00432 #endif // P_CONFIG_FILE
00433 
00434 #endif
00435 
00436 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Sep 15 01:21:34 2008 for PTLib by  doxygen 1.5.1