PTLib  Version 2.14.3
 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: 32238 $
30  * $Author: rjongbloed $
31  * $Date: 2014-06-27 14:23:17 +1000 (Fri, 27 Jun 2014) $
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  ) { SetArgs((const char *)theArgStr); }
118  void SetArgs(
119  const char * theArgStr
120  );
122  void SetArgs(
123  int theArgc,
124  char ** theArgv
125  );
127  void SetArgs(
128  const PStringArray & theArgs
129  );
130 
134  const PString & name
135  ) { m_commandName = name; }
136 
139  const PString & GetCommandName() const { return m_commandName; }
140 
184  virtual bool Parse(
185  const char * theArgumentSpec = NULL,
186  PBoolean optionsBeforeParams = true
187  );
189  virtual bool Parse(
190  const PString & theArgumentStr,
191  PBoolean optionsBeforeParams = true
192  );
193 
196  bool IsParsed() const { return m_parsed; }
197 
202  const PString & GetParseError() const { return m_parseError; }
203 
209  ostream & Usage(
210  ostream & strm,
211  const char * usage = NULL,
212  const char * prefix = "Usage: "
213  ) const;
214  PString Usage(
215  const char * usage = NULL,
216  const char * prefix = "Usage: "
217  ) const;
219 
227  virtual PINDEX GetOptionCount(
228  char optionChar
229  ) const;
231  virtual PINDEX GetOptionCount(
232  const char * optionStr
233  ) const;
235  virtual PINDEX GetOptionCount(
236  const PString & optionName
237  ) const;
238 
245  char optionChar
246  ) const;
249  const char * optionStr
250  ) const;
253  const PString & optionName
254  ) const;
255 
264  virtual PString GetOptionString(
265  char optionChar,
266  const char * dflt = NULL
267  ) const;
269  virtual PString GetOptionString(
270  const char * optionStr,
271  const char * dflt = NULL
272  ) const;
274  virtual PString GetOptionString(
275  const PString & optionName,
276  const char * dflt = NULL
277  ) const;
278 
281  template <typename ValueType, typename OptionType>
282  ValueType GetOptionAs(
283  OptionType option,
284  ValueType value = 0
285  ) const {
286  PStringStream strm(GetOptionString(option));
287  if (!strm.IsEmpty())
288  strm >> value;
289  return value;
290  }
291 
299  PINDEX GetCount() const;
300 
306  PINDEX first = 0,
307  PINDEX last = P_MAX_INDEX
308  ) const;
309 
315  PINDEX num
316  ) const;
317 
324  PINDEX num
325  ) const;
326 
330  void Shift(
331  int sh
332  );
333 
338  int sh
339  );
340 
345  int sh
346  );
348 
349  protected:
351  bool m_parsed;
352  PString m_parseError; // An error was detected during parsing of arguments
353  PStringArray m_argumentArray; // The original program arguments.
354 
355  enum OptionType {
359  };
360  struct OptionSpec {
362  char m_letter;
367 
368  unsigned m_count;
370  };
371  vector<OptionSpec> m_options;
372 
375 
377  int m_shift;
379 
380  // Internal stuff
381  bool InternalSpecificationError(bool isError, const PString & msg);
382  size_t InternalFindOption(const PString & name) const;
383  int InternalParseOption(const PString & opt, PINDEX offset, PINDEX & arg);
384  PINDEX InternalGetOptionCountByIndex(size_t idx) const;
385  PString InternalGetOptionStringByIndex(size_t idx, const char * dflt) const;
386 };
387 
388 
389 #ifdef P_CONFIG_FILE
390 
394 class PConfigArgs : public PArgList
395 {
396  PCLASSINFO(PConfigArgs, PArgList);
397  public:
400  PConfigArgs(
401  const PArgList & args
402  );
403 
404  ~PConfigArgs();
406 
414  virtual PINDEX GetOptionCount(
415  char optionChar
416  ) const;
418  virtual PINDEX GetOptionCount(
419  const char * optionStr
420  ) const;
422  virtual PINDEX GetOptionCount(
423  const PString & optionName
424  ) const;
425 
434  virtual PString GetOptionString(
435  char optionChar,
436  const char * dflt = NULL
437  ) const;
438 
440  virtual PString GetOptionString(
441  const char * optionStr,
442  const char * dflt = NULL
443  ) const;
444 
446  virtual PString GetOptionString(
447  const PString & optionName,
448  const char * dflt = NULL
449  ) const;
451 
460  void Save(
461  const PString & optionName
462  );
463 
467  const PString & section
468  ) { m_sectionName = section; }
469 
472  const PString & GetSectionName() const { return m_sectionName; }
473 
478  const PString & prefix
479  ) { m_negationPrefix = prefix; }
480 
484  const PString & GetNegationPrefix() const { return m_negationPrefix; }
486 
487 
488  protected:
489  PString CharToString(char ch) const;
490 
494 };
495 
496 #endif // P_CONFIG_FILE
497 
498 
499 #endif // PTLIB_ARGLIST_H
500 
501 
502 // End Of File ///////////////////////////////////////////////////////////////