# 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: 24177 $
    00030  * $Author: rjongbloed $
    00031  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
    00032  */
    00033
    00034 #ifndef PTLIB_ARGLIST_H
    00035 #define PTLIB_ARGLIST_H
    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 = true
    00066     );
    00068     PArgList(
    00069       const PString & theArgStr,
    00070       const char * argumentSpecPtr = NULL,
    00071       PBoolean optionsBeforeParams = true
    00072     );
    00074     PArgList(
    00075       const PString & theArgStr,
    00076       const PString & argumentSpecStr,
    00077       PBoolean optionsBeforeParams = true
    00078     );
    00080     PArgList(
    00081       int theArgc,
    00082       char ** theArgv,
    00083       const char * argumentSpecPtr = NULL,
    00084       PBoolean optionsBeforeParams = true
    00085     );
    00087     PArgList(
    00088       int theArgc,
    00089       char ** theArgv,
    00090       const PString & argumentSpecStr,
    00091       PBoolean optionsBeforeParams = true
    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 = true
    00153     );
    00155     virtual PBoolean Parse(
    00156       const PString & theArgumentStr,
    00157       PBoolean optionsBeforeParams = true
    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     int m_argsParsed;
    00326 };
    00327
    00328
    00329 #ifdef P_CONFIG_FILE
    00330
    00334 class PConfigArgs : public PArgList
    00335 {
    00336     PCLASSINFO(PConfigArgs, PArgList);
    00337   public:
    00340     PConfigArgs(
    00341       const PArgList & args
    00342     );
    00344
    00352     virtual PINDEX GetOptionCount(
    00353       char optionChar
    00354     ) const;
    00356     virtual PINDEX GetOptionCount(
    00357       const char * optionStr
    00358     ) const;
    00360     virtual PINDEX GetOptionCount(
    00361       const PString & optionName
    00362     ) const;
    00363
    00372     virtual PString GetOptionString(
    00373       char optionChar,
    00374       const char * dflt = NULL
    00375     ) const;
    00376
    00378     virtual PString GetOptionString(
    00379       const char * optionStr,
    00380       const char * dflt = NULL
    00381     ) const;
    00382
    00384     virtual PString GetOptionString(
    00385       const PString & optionName,
    00386       const char * dflt = NULL
    00387     ) const;
    00389
    00398     void Save(
    00399       const PString & optionName
    00400     );
    00401
    00404     void SetSectionName(
    00405       const PString & section
    00406     ) { sectionName = section; }
    00407
    00410     const PString & GetSectionName() const { return sectionName; }
    00411
    00415     void SetNegationPrefix(
    00416       const PString & prefix
    00417     ) { negationPrefix = prefix; }
    00418
    00422     const PString & GetNegationPrefix() const { return negationPrefix; }
    00424
    00425
    00426   protected:
    00427     PString CharToString(char ch) const;
    00428     PConfig config;
    00429     PString sectionName;
    00430     PString negationPrefix;
    00431 };
    00432
    00433 #endif // P_CONFIG_FILE
    00434
    00435
    00436 #endif // PTLIB_ARGLIST_H
    00437
    00438
    00439 // End Of File ///////////////////////////////////////////////////////////////