PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 {
336  PCLASSINFO(PConfigArgs, PArgList);
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 ///////////////////////////////////////////////////////////////