PTLib  Version 2.10.11
args.h
Go to the documentation of this file.
1 /*
2  * args.h
3  *
4  * Program Argument Parsing class
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 24177 $
30  * $Author: rjongbloed $
31  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
32  */
33 
34 #ifndef PTLIB_ARGLIST_H
35 #define PTLIB_ARGLIST_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
45 class PArgList : public PObject
46 {
47  PCLASSINFO(PArgList, PObject);
48 
49  public:
62  PArgList(
63  const char * theArgPtr = NULL,
64  const char * argumentSpecPtr = NULL,
65  PBoolean optionsBeforeParams = true
66  );
68  PArgList(
69  const PString & theArgStr,
70  const char * argumentSpecPtr = NULL,
71  PBoolean optionsBeforeParams = true
72  );
74  PArgList(
75  const PString & theArgStr,
76  const PString & argumentSpecStr,
77  PBoolean optionsBeforeParams = true
78  );
80  PArgList(
81  int theArgc,
82  char ** theArgv,
83  const char * argumentSpecPtr = NULL,
84  PBoolean optionsBeforeParams = true
85  );
87  PArgList(
88  int theArgc,
89  char ** theArgv,
90  const PString & argumentSpecStr,
91  PBoolean optionsBeforeParams = true
92  );
94 
99  virtual void PrintOn(
100  ostream & strm
101  ) const;
102 
106  virtual void ReadFrom(
107  istream & strm
108  );
110 
115  void SetArgs(
116  const PString & theArgStr
117  );
119  void SetArgs(
120  int theArgc,
121  char ** theArgv
122  );
124  void SetArgs(
125  const PStringArray & theArgs
126  );
127 
150  virtual PBoolean Parse(
151  const char * theArgumentSpec,
152  PBoolean optionsBeforeParams = true
153  );
155  virtual PBoolean Parse(
156  const PString & theArgumentStr,
157  PBoolean optionsBeforeParams = true
158  );
160 
168  virtual PINDEX GetOptionCount(
169  char optionChar
170  ) const;
172  virtual PINDEX GetOptionCount(
173  const char * optionStr
174  ) const;
176  virtual PINDEX GetOptionCount(
177  const PString & optionName
178  ) const;
179 
186  char optionChar
187  ) const;
190  const char * optionStr
191  ) const;
194  const PString & optionName
195  ) const;
196 
205  virtual PString GetOptionString(
206  char optionChar,
207  const char * dflt = NULL
208  ) const;
210  virtual PString GetOptionString(
211  const char * optionStr,
212  const char * dflt = NULL
213  ) const;
215  virtual PString GetOptionString(
216  const PString & optionName,
217  const char * dflt = NULL
218  ) const;
219 
227  PINDEX GetCount() const;
228 
234  PINDEX first = 0,
235  PINDEX last = P_MAX_INDEX
236  ) const;
237 
243  PINDEX num
244  ) const;
245 
252  PINDEX num
253  ) const;
254 
258  void Shift(
259  int sh
260  );
261 
266  int sh
267  );
268 
273  int sh
274  );
276 
283  virtual void IllegalArgumentIndex(
284  PINDEX idx
285  ) const;
286 
291  virtual void UnknownOption(
292  const PString & option
293  ) const;
294 
300  virtual void MissingArgument(
301  const PString & option
302  ) const;
304 
305  protected:
319  int shift;
320 
321  private:
322  PBoolean ParseOption(PINDEX idx, PINDEX offset, PINDEX & arg, const PIntArray & canHaveOptionString);
323  PINDEX GetOptionCountByIndex(PINDEX idx) const;
324  PString GetOptionStringByIndex(PINDEX idx, const char * dflt) const;
325  int m_argsParsed;
326 };
327 
328 
329 #ifdef P_CONFIG_FILE
330 
334 class PConfigArgs : public PArgList
335 {
337  public:
340  PConfigArgs(
341  const PArgList & args
342  );
344 
352  virtual PINDEX GetOptionCount(
353  char optionChar
354  ) const;
356  virtual PINDEX GetOptionCount(
357  const char * optionStr
358  ) const;
360  virtual PINDEX GetOptionCount(
361  const PString & optionName
362  ) const;
363 
372  virtual PString GetOptionString(
373  char optionChar,
374  const char * dflt = NULL
375  ) const;
376 
378  virtual PString GetOptionString(
379  const char * optionStr,
380  const char * dflt = NULL
381  ) const;
382 
384  virtual PString GetOptionString(
385  const PString & optionName,
386  const char * dflt = NULL
387  ) const;
389 
398  void Save(
399  const PString & optionName
400  );
401 
405  const PString & section
406  ) { sectionName = section; }
407 
410  const PString & GetSectionName() const { return sectionName; }
411 
416  const PString & prefix
417  ) { negationPrefix = prefix; }
418 
422  const PString & GetNegationPrefix() const { return negationPrefix; }
424 
425 
426  protected:
427  PString CharToString(char ch) const;
431 };
432 
433 #endif // P_CONFIG_FILE
434 
435 
436 #endif // PTLIB_ARGLIST_H
437 
438 
439 // End Of File ///////////////////////////////////////////////////////////////
PIntArray parameterIndex
The index of each .
Definition: args.h:317
void SetArgs(const PString &theArgStr)
Set the internal copy of the program arguments.
virtual void UnknownOption(const PString &option) const
This function is called when an unknown option was specified on the command line. ...
PString operator[](PINDEX num) const
Get the parameter that was parsed in the argument list.
PStringArray optionString
The array of associated strings to options.
Definition: args.h:315
int shift
Shift count for the parameters in the argument list.
Definition: args.h:319
PStringArray GetParameters(PINDEX first=0, PINDEX last=P_MAX_INDEX) const
Get the parameters that were parsed in the argument list.
PArgList(const char *theArgPtr=NULL, const char *argumentSpecPtr=NULL, PBoolean optionsBeforeParams=true)
Create an argument list.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PArgList & operator>>(int sh)
Shift the parameters by the specified amount.
A class representing a configuration for the application.
Definition: config.h:67
PString GetParameter(PINDEX num) const
Get the parameter that was parsed in the argument list.
PString negationPrefix
Definition: args.h:430
PArgList & operator<<(int sh)
Shift the parameters by the specified amount.
This is an array collection class of PString objects.
Definition: pstring.h:2024
const PString & GetNegationPrefix() const
Get the prefix for option negation.
Definition: args.h:422
void SetSectionName(const PString &section)
Set the PConfig section name for options.
Definition: args.h:404
BOOL PBoolean
Definition: object.h:102
PBoolean HasOption(char optionChar) const
Get if option present.
void SetNegationPrefix(const PString &prefix)
Set the prefix for option negation.
Definition: args.h:415
PString optionLetters
The specification letters for options.
Definition: args.h:309
PStringArray optionNames
The specification strings for options.
Definition: args.h:311
virtual PBoolean Parse(const char *theArgumentSpec, PBoolean optionsBeforeParams=true)
Parse the arguments.
This class parse command line arguments with the ability to override them from a PConfig file/registr...
Definition: args.h:334
virtual void ReadFrom(istream &strm)
Input the string from the specified stream.
The character string class.
Definition: pstring.h:108
virtual PINDEX GetOptionCount(char optionChar) const
Get the count of the number of times the option was specified on the command line.
This class allows the parsing of a set of program arguments.
Definition: args.h:45
PIntArray optionCount
The count of the number of times an option appeared in the command line.
Definition: args.h:313
PStringArray argumentArray
The original program arguments.
Definition: args.h:307
virtual void PrintOn(ostream &strm) const
Output the string to the specified stream.
const PString & GetSectionName() const
Get the PConfig section name for options.
Definition: args.h:410
PConfig config
Definition: args.h:428
virtual void IllegalArgumentIndex(PINDEX idx) const
This function is called when access to illegal parameter index is made in the GetParameter function...
PString sectionName
Definition: args.h:429
PINDEX GetCount() const
Get the argument count.
Array of integers.
Definition: array.h:616
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
virtual void MissingArgument(const PString &option) const
This function is called when an option that requires an associated string was specified on the comman...
virtual PString GetOptionString(char optionChar, const char *dflt=NULL) const
Get option string.
void Shift(int sh)
Shift the parameters by the specified amount.