PTLib  Version 2.18.8
 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 
30 #ifndef PTLIB_ARGLIST_H
31 #define PTLIB_ARGLIST_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
41 class PArgList : public PObject
42 {
43  PCLASSINFO(PArgList, PObject);
44 
45  public:
58  PArgList(
59  const char * theArgPtr = NULL,
60  const char * argumentSpecPtr = NULL,
61  PBoolean optionsBeforeParams = true
62  );
64  PArgList(
65  const PString & theArgStr,
66  const char * argumentSpecPtr = NULL,
67  PBoolean optionsBeforeParams = true
68  );
70  PArgList(
71  const PString & theArgStr,
72  const PString & argumentSpecStr,
73  PBoolean optionsBeforeParams = true
74  );
76  PArgList(
77  int theArgc,
78  char ** theArgv,
79  const char * argumentSpecPtr = NULL,
80  PBoolean optionsBeforeParams = true
81  );
83  PArgList(
84  int theArgc,
85  char ** theArgv,
86  const PString & argumentSpecStr,
87  PBoolean optionsBeforeParams = true
88  );
90 
95  virtual void PrintOn(
96  ostream & strm
97  ) const;
98 
102  virtual void ReadFrom(
103  istream & strm
104  );
106 
111  void SetArgs(
112  const PString & theArgStr
113  ) { SetArgs((const char *)theArgStr); }
114  void SetArgs(
115  const char * theArgStr
116  );
118  void SetArgs(
119  int theArgc,
120  char ** theArgv
121  );
123  void SetArgs(
124  const PStringArray & theArgs
125  );
126 
130  const PString & name
131  ) { m_commandName = name; }
132 
135  const PString & GetCommandName() const { return m_commandName; }
136 
180  virtual bool Parse(
181  const char * theArgumentSpec = NULL,
182  PBoolean optionsBeforeParams = true
183  );
185  virtual bool Parse(
186  const PString & theArgumentStr,
187  PBoolean optionsBeforeParams = true
188  );
189 
192  bool IsParsed() const { return m_parsed; }
193 
198  const PString & GetParseError() const { return m_parseError; }
199 
205  ostream & Usage(
206  ostream & strm,
207  const char * usage = NULL,
208  const char * prefix = "Usage: "
209  ) const;
210  PString Usage(
211  const char * usage = NULL,
212  const char * prefix = "Usage: "
213  ) const;
215 
223  virtual PINDEX GetOptionCount(
224  char optionChar
225  ) const;
227  virtual PINDEX GetOptionCount(
228  const char * optionStr
229  ) const;
231  virtual PINDEX GetOptionCount(
232  const PString & optionName
233  ) const;
234 
241  char optionChar
242  ) const;
245  const char * optionStr
246  ) const;
249  const PString & optionName
250  ) const;
251 
260  virtual PString GetOptionString(
261  char optionChar,
262  const char * dflt = NULL
263  ) const;
265  virtual PString GetOptionString(
266  const char * optionStr,
267  const char * dflt = NULL
268  ) const;
270  virtual PString GetOptionString(
271  const PString & optionName,
272  const char * dflt = NULL
273  ) const;
274 
277  template <typename ValueType, typename OptionType>
278  ValueType GetOptionAs(
279  OptionType option,
280  ValueType value = 0
281  ) const {
282  PStringStream strm(GetOptionString(option));
283  if (!strm.IsEmpty())
284  strm >> value;
285  return value;
286  }
287 
295  PINDEX GetCount() const;
296 
302  PINDEX first = 0,
303  PINDEX last = P_MAX_INDEX
304  ) const;
305 
311  PINDEX num
312  ) const;
313 
320  PINDEX num
321  ) const;
322 
326  void Shift(
327  int sh
328  );
329 
334  int sh
335  );
336 
341  int sh
342  );
344 
345  protected:
347  bool m_parsed;
348  PString m_parseError; // An error was detected during parsing of arguments
349  PStringArray m_argumentArray; // The original program arguments.
350 
351  enum OptionType {
355  };
356  struct OptionSpec {
358  char m_letter;
363 
364  unsigned m_count;
366  };
367  vector<OptionSpec> m_options;
368 
371 
373  int m_shift;
375 
376  // Internal stuff
377  bool InternalSpecificationError(bool isError, const PString & msg);
378  size_t InternalFindOption(const PString & name) const;
379  int InternalParseOption(const PString & opt, PINDEX offset, PINDEX & arg);
380  PINDEX InternalGetOptionCountByIndex(size_t idx) const;
381  PString InternalGetOptionStringByIndex(size_t idx, const char * dflt) const;
382 };
383 
384 
385 #ifdef P_CONFIG_FILE
386 
390 class PConfigArgs : public PArgList
391 {
392  PCLASSINFO(PConfigArgs, PArgList);
393  public:
396  PConfigArgs(
397  const PArgList & args
398  );
399 
400  ~PConfigArgs();
402 
410  virtual PINDEX GetOptionCount(
411  char optionChar
412  ) const;
414  virtual PINDEX GetOptionCount(
415  const char * optionStr
416  ) const;
418  virtual PINDEX GetOptionCount(
419  const PString & optionName
420  ) const;
421 
430  virtual PString GetOptionString(
431  char optionChar,
432  const char * dflt = NULL
433  ) const;
434 
436  virtual PString GetOptionString(
437  const char * optionStr,
438  const char * dflt = NULL
439  ) const;
440 
442  virtual PString GetOptionString(
443  const PString & optionName,
444  const char * dflt = NULL
445  ) const;
447 
456  void Save(
457  const PString & optionName
458  );
459 
463  const PString & section
464  ) { m_sectionName = section; }
465 
468  const PString & GetSectionName() const { return m_sectionName; }
469 
474  const PString & prefix
475  ) { m_negationPrefix = prefix; }
476 
480  const PString & GetNegationPrefix() const { return m_negationPrefix; }
482 
483 
484  protected:
485  PString CharToString(char ch) const;
486 
490 };
491 
492 #endif // P_CONFIG_FILE
493 
494 
495 #endif // PTLIB_ARGLIST_H
496 
497 
498 // End Of File ///////////////////////////////////////////////////////////////
PString m_negationPrefix
Definition: args.h:489
size_t InternalFindOption(const PString &name) const
void SetArgs(const PString &theArgStr)
Set the internal copy of the program arguments.
Definition: args.h:111
PString m_commandName
Definition: args.h:346
PString operator[](PINDEX num) const
Get the parameter that was parsed in the argument list.
bool IsParsed() const
Determine if already parsed at least once.
Definition: args.h:192
int m_argsParsed
Definition: args.h:374
virtual bool Parse(const char *theArgumentSpec=NULL, PBoolean optionsBeforeParams=true)
Parse the arguments.
PString m_name
Definition: args.h:359
virtual PINDEX GetOptionCount(char optionChar) const
Get the count of the number of times the option was specified on the command line.
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.
PConfig * m_config
Definition: args.h:487
PArgList & operator>>(int sh)
Shift the parameters by the specified amount.
PString m_usage
Definition: args.h:360
PString m_parseError
Definition: args.h:348
PConfigArgs(const PArgList &args)
A class representing a configuration for the application.
Definition: config.h:62
This class is a standard C++ stream class descendent for reading or writing streamed data to or from ...
Definition: pstring.h:2188
PString InternalGetOptionStringByIndex(size_t idx, const char *dflt) const
PString GetParameter(PINDEX num) const
Get the parameter that was parsed in the argument list.
PINDEX InternalGetOptionCountByIndex(size_t idx) const
PArgList & operator<<(int sh)
Shift the parameters by the specified amount.
void Save(const PString &optionName)
Save the current options to the PConfig.
PString m_string
Definition: args.h:365
This is an array collection class of PString objects.
Definition: pstring.h:2365
OptionSpec()
Definition: args.h:357
const PString & GetNegationPrefix() const
Get the prefix for option negation.
Definition: args.h:480
virtual PBoolean IsEmpty() const
Determine if the string is empty.
void SetSectionName(const PString &section)
Set the PConfig section name for options.
Definition: args.h:462
PStringArray m_argumentArray
Definition: args.h:349
PBoolean HasOption(char optionChar) const
Get if option present.
Definition: args.h:356
PString CharToString(char ch) const
char m_letter
Definition: args.h:358
void SetNegationPrefix(const PString &prefix)
Set the prefix for option negation.
Definition: args.h:473
Definition: args.h:352
ValueType GetOptionAs(OptionType option, ValueType value=0) const
Get option as specicied type.
Definition: args.h:278
PString m_sectionName
Definition: args.h:488
vector< OptionSpec > m_options
Definition: args.h:367
bool PBoolean
Definition: object.h:174
Definition: args.h:354
virtual PString GetOptionString(char optionChar, const char *dflt=NULL) const
Get option string.
This class parse command line arguments with the ability to override them from a PConfig file/registr...
Definition: args.h:390
virtual void ReadFrom(istream &strm)
Input the string from the specified stream.
#define P_MAX_INDEX
Definition: object.h:80
Definition: args.h:353
unsigned m_count
Definition: args.h:364
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.
int m_shift
Shift count for the parameters in the argument list.
Definition: args.h:373
This class allows the parsing of a set of program arguments.
Definition: args.h:41
bool InternalSpecificationError(bool isError, const PString &msg)
virtual void PrintOn(ostream &strm) const
Output the string to the specified stream.
int InternalParseOption(const PString &opt, PINDEX offset, PINDEX &arg)
const PString & GetSectionName() const
Get the PConfig section name for options.
Definition: args.h:468
const PString & GetCommandName() const
Get the command name.
Definition: args.h:135
PIntArray m_parameterIndex
The index of each parameter.
Definition: args.h:370
PString m_section
Definition: args.h:361
const PString & GetParseError() const
Return error message after a call to Parse().
Definition: args.h:198
PINDEX GetCount() const
Get the argument count.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
OptionType
Definition: args.h:351
void SetCommandName(const PString &name)
Set the comand name.
Definition: args.h:129
ostream & Usage(ostream &strm, const char *usage=NULL, const char *prefix="Usage: ") const
Output usage text for parsed arguments.
virtual PString GetOptionString(char optionChar, const char *dflt=NULL) const
Get option string.
OptionType m_type
Definition: args.h:362
void Shift(int sh)
Shift the parameters by the specified amount.
bool m_parsed
Definition: args.h:347