PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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: 30782 $
30 * $Author: rjongbloed $
31 * $Date: 2013-10-30 13:56:29 +1100 (Wed, 30 Oct 2013) $
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
130 void SetCommandName(
131 const PString & name
132 ) { m_commandName = name; }
133
136 const PString & GetCommandName() const { return m_commandName; }
137
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;
209 PString Usage() const;
211
219 virtual PINDEX GetOptionCount(
220 char optionChar
221 ) const;
223 virtual PINDEX GetOptionCount(
224 const char * optionStr
225 ) const;
227 virtual PINDEX GetOptionCount(
228 const PString & optionName
229 ) const;
230
236 PBoolean HasOption(
237 char optionChar
238 ) const;
240 PBoolean HasOption(
241 const char * optionStr
242 ) const;
244 PBoolean HasOption(
245 const PString & optionName
246 ) const;
247
256 virtual PString GetOptionString(
257 char optionChar,
258 const char * dflt = NULL
259 ) const;
261 virtual PString GetOptionString(
262 const char * optionStr,
263 const char * dflt = NULL
264 ) const;
266 virtual PString GetOptionString(
267 const PString & optionName,
268 const char * dflt = NULL
269 ) const;
270
278 PINDEX GetCount() const;
279
284 PStringArray GetParameters(
285 PINDEX first = 0,
286 PINDEX last = P_MAX_INDEX
287 ) const;
288
293 PString GetParameter(
294 PINDEX num
295 ) const;
296
302 PString operator[](
303 PINDEX num
304 ) const;
305
309 void Shift(
310 int sh
311 );
312
316 PArgList & operator<<(
317 int sh
318 );
319
323 PArgList & operator>>(
324 int sh
325 );
327
328 protected:
329 PString m_commandName;
330 bool m_parsed;
331 PString m_parseError; // An error was detected during parsing of arguments
332 PStringArray m_argumentArray; // The original program arguments.
333
334 enum OptionType {
335 NoString,
336 HasString,
337 StringWithLetter
338 };
339 struct OptionSpec {
340 OptionSpec() : m_letter('\0'), m_type(NoString), m_count(0) { }
341 char m_letter;
342 PString m_name;
343 PString m_usage;
344 PString m_section;
345 OptionType m_type;
346
347 unsigned m_count;
348 PString m_string;
349 };
350 vector<OptionSpec> m_options;
351
353 PIntArray m_parameterIndex;
354
356 int m_shift;
357 int m_argsParsed;
358
359 // Internal stuff
360 bool InternalSpecificationError(bool isError, const PString & msg);
361 size_t InternalFindOption(const PString & name) const;
362 int InternalParseOption(const PString & opt, PINDEX offset, PINDEX & arg);
363 PINDEX InternalGetOptionCountByIndex(size_t idx) const;
364 PString InternalGetOptionStringByIndex(size_t idx, const char * dflt) const;
365 };
366
367
368 #ifdef P_CONFIG_FILE
369
373 class PConfigArgs : public PArgList
374 {
375 PCLASSINFO(PConfigArgs, PArgList);
376 public:
379 PConfigArgs(
380 const PArgList & args
381 );
383
391 virtual PINDEX GetOptionCount(
392 char optionChar
393 ) const;
395 virtual PINDEX GetOptionCount(
396 const char * optionStr
397 ) const;
399 virtual PINDEX GetOptionCount(
400 const PString & optionName
401 ) const;
402
411 virtual PString GetOptionString(
412 char optionChar,
413 const char * dflt = NULL
414 ) const;
415
417 virtual PString GetOptionString(
418 const char * optionStr,
419 const char * dflt = NULL
420 ) const;
421
423 virtual PString GetOptionString(
424 const PString & optionName,
425 const char * dflt = NULL
426 ) const;
428
437 void Save(
438 const PString & optionName
439 );
440
443 void SetSectionName(
444 const PString & section
445 ) { sectionName = section; }
446
449 const PString & GetSectionName() const { return sectionName; }
450
454 void SetNegationPrefix(
455 const PString & prefix
456 ) { negationPrefix = prefix; }
457
461 const PString & GetNegationPrefix() const { return negationPrefix; }
463
464
465 protected:
466 PString CharToString(char ch) const;
467 PConfig config;
468 PString sectionName;
469 PString negationPrefix;
470 };
471
472 #endif // P_CONFIG_FILE
473
474
475 #endif // PTLIB_ARGLIST_H
476
477
478 // End Of File ///////////////////////////////////////////////////////////////