4 * Program Argument Parsing class
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
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/
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
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
30 * Revision 1.24 2003/09/17 01:18:01 csoutheren
31 * Removed recursive include file system and removed all references
32 * to deprecated coooperative threading support
34 * Revision 1.23 2003/03/27 07:27:07 robertj
35 * Added function to get a bunch of arguments as a string array.
37 * Revision 1.22 2002/09/16 01:08:59 robertj
38 * Added #define so can select if #pragma interface/implementation is used on
39 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
41 * Revision 1.21 2001/12/15 04:49:06 robertj
42 * Added stream I/O functions for argument list.
44 * Revision 1.20 2000/05/25 11:05:31 robertj
45 * Added PConfigArgs class so can save program arguments to config files.
47 * Revision 1.19 1999/03/09 02:59:49 robertj
48 * Changed comments to doc++ compatible documentation.
50 * Revision 1.18 1999/02/16 08:07:10 robertj
51 * MSVC 6.0 compatibility changes.
53 * Revision 1.17 1998/11/01 04:56:51 robertj
54 * Added BOOl return value to Parse() to indicate there are parameters available.
56 * Revision 1.16 1998/10/30 11:22:14 robertj
57 * Added constructors that take strings as well as const char *'s.
59 * Revision 1.15 1998/10/30 05:24:29 robertj
60 * Added return value to << and >> operators for shifting arguments.
62 * Revision 1.14 1998/10/29 05:35:14 robertj
63 * Fixed porblem with GetCount() == 0 if do not call Parse() function.
65 * Revision 1.13 1998/10/28 03:26:41 robertj
66 * Added multi character arguments (-abc style) and options precede parameters mode.
68 * Revision 1.12 1998/10/28 00:59:46 robertj
69 * New improved argument parsing.
71 * Revision 1.11 1998/09/23 06:20:14 robertj
72 * Added open source copyright license.
74 * Revision 1.10 1995/12/10 11:26:38 robertj
75 * Fixed signed/unsigned bug in shift count.
77 * Revision 1.9 1995/06/17 11:12:17 robertj
78 * Documentation update.
80 * Revision 1.8 1995/03/14 12:40:58 robertj
81 * Updated documentation to use HTML codes.
83 * Revision 1.7 1994/12/05 11:15:13 robertj
86 * Revision 1.6 1994/11/26 03:44:19 robertj
89 * Revision 1.6 1994/11/24 11:48:26 robertj
92 * Revision 1.5 1994/08/23 11:32:52 robertj
95 * Revision 1.4 1994/08/22 00:46:48 robertj
96 * Added pragma fro GNU C++ compiler.
98 * Revision 1.3 1994/07/27 05:58:07 robertj
101 * Revision 1.2 1994/07/17 10:46:06 robertj
102 * Changed to use container classes to plug memory leak.
104 * Revision 1.1 1994/04/01 14:08:52 robertj
116 /** This class allows the parsing of a set of program arguments. This translates
117 the standard argc/argv style variables passed into the main() function into
118 a set of options (preceded by a '-' character) and parameters.
120 class PArgList
: public PObject
122 PCLASSINFO(PArgList
, PObject
);
125 /**@name Construction */
127 /** Create an argument list.
128 An argument list is created given the standard arguments and a
129 specification for options. The program arguments are parsed from this
130 into options and parameters.
132 The specification string consists of case significant letters for each
133 option. If the letter is followed by the ':' character then the option
134 has an associated string. This string must be in the argument or in the
138 const char * theArgPtr
= NULL
, /// A string constituting the arguments
139 const char * argumentSpecPtr
= NULL
,
140 /** The specification C string for argument options. See description for
143 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
145 /** Create an argument list. */
147 const PString
& theArgStr
, /// A string constituting the arguments
148 const char * argumentSpecPtr
= NULL
,
149 /** The specification C string for argument options. See description for
152 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
154 /** Create an argument list. */
156 const PString
& theArgStr
, /// A string constituting the arguments
157 const PString
& argumentSpecStr
,
158 /** The specification string for argument options. See description for
161 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
163 /** Create an argument list. */
165 int theArgc
, /// Count of argument strings in theArgv
166 char ** theArgv
, /// An array of strings constituting the arguments
167 const char * argumentSpecPtr
= NULL
,
168 /** The specification C string for argument options. See description for
171 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
173 /** Create an argument list. */
175 int theArgc
, /// Count of argument strings in theArgv
176 char ** theArgv
, /// An array of strings constituting the arguments
177 const PString
& argumentSpecStr
,
178 /** The specification string for argument options. See description for
181 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
185 /**@name Overrides from class PObject */
187 /**Output the string to the specified stream.
189 virtual void PrintOn(
190 ostream
& strm
/// I/O stream to output to.
193 /**Input the string from the specified stream. This will read all
194 characters until a end of line is reached, then parsing the arguments.
196 virtual void ReadFrom(
197 istream
& strm
/// I/O stream to input from.
201 /**@name Setting & Parsing */
203 /** Set the internal copy of the program arguments.
206 const PString
& theArgStr
/// A string constituting the arguments
208 /** Set the internal copy of the program arguments. */
210 int theArgc
, /// Count of argument strings in theArgv
211 char ** theArgv
/// An array of strings constituting the arguments
213 /** Set the internal copy of the program arguments. */
215 const PStringArray
& theArgs
/// A string array constituting the arguments
218 /** Parse the arguments.
219 Parse the standard C program arguments into an argument of options and
220 parameters. Consecutive calls with #optionsBeforeParams# set
221 to TRUE will parse out different options and parameters. If SetArgs()
222 function is called then the Parse() function will restart from the
223 beginning of the argument list.
225 The specification string consists of case significant letters for each
226 option. If the letter is followed by a '-' character then a long name
227 version of the option is present. This is terminated either by a '.' or
228 a ':' character. If the single letter or long name is followed by the
229 ':' character then the option has may have an associated string. This
230 string must be within the argument or in the next argument. If a single
231 letter option is followed by a ';' character, then the option may have
232 an associated string but this MUST follow the letter immediately, if
233 it is present at all.
235 For example, "ab:c" allows for "-a -b arg -barg -c" and
236 "a-an-arg.b-option:c;" allows for "-a --an-arg --option arg -c -copt".
238 @return TRUE if there is at least one parameter after parsing.
241 const char * theArgumentSpec
,
242 /** The specification string for argument options. See description for
245 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
247 /** Parse the arguments. */
249 const PString
& theArgumentStr
,
250 /** The specification string for argument options. See description for
253 BOOL optionsBeforeParams
= TRUE
/// Parse options only before parameters
257 /**@name Getting parsed arguments */
259 /** Get the count of the number of times the option was specified on the
262 @return option repeat count.
264 virtual PINDEX
GetOptionCount(
265 char optionChar
/// Character letter code for the option
267 /** Get the count of option */
268 virtual PINDEX
GetOptionCount(
269 const char * optionStr
/// String code for the option
271 /** Get the count of option */
272 virtual PINDEX
GetOptionCount(
273 const PString
& optionName
/// String code for the option
276 /** Get if option present.
277 Determines whether the option was specified on the command line.
279 @return TRUE if the option was present.
282 char optionChar
/// Character letter code for the option
284 /** Get if option present. */
286 const char * optionStr
/// String letter code for the option
288 /** Get if option present. */
290 const PString
& optionName
/// String code for the option
293 /** Get option string.
294 Gets the string associated with an option e.g. -ofile or -o file
295 would return the string "file". An option may have an associated string
296 if it had a ':' character folowing it in the specification string passed
297 to the Parse() function.
299 @return the options associated string.
301 virtual PString
GetOptionString(
302 char optionChar
, /// Character letter code for the option
303 const char * dflt
= NULL
/// Default value of the option string
305 /** Get option string. */
306 virtual PString
GetOptionString(
307 const char * optionStr
, /// String letter code for the option
308 const char * dflt
= NULL
/// Default value of the option string
310 /** Get option string. */
311 virtual PString
GetOptionString(
312 const PString
& optionName
, /// String code for the option
313 const char * dflt
= NULL
/// Default value of the option string
316 /** Get the argument count.
317 Get the number of parameters that may be obtained via the
318 #GetParameter()# function. Note that this does not include options
321 @return count of parameters.
323 PINDEX
GetCount() const;
325 /** Get the parameters that were parsed in the argument list.
327 @return array of parameter strings at the specified index range.
329 PStringArray
GetParameters(
331 PINDEX last
= P_MAX_INDEX
334 /** Get the parameter that was parsed in the argument list.
336 @return parameter string at the specified index.
338 PString
GetParameter(
339 PINDEX num
/// Number of the parameter to retrieve.
342 /** Get the parameter that was parsed in the argument list. The argument
343 list object can thus be treated as an "array" of parameters.
345 @return parameter string at the specified index.
348 PINDEX num
/// Number of the parameter to retrieve.
351 /** Shift the parameters by the specified amount. This allows the parameters
352 to be parsed at the same position in the argument list "array".
355 int sh
/// Number of parameters to shift forward through list
358 /** Shift the parameters by the specified amount. This allows the parameters
359 to be parsed at the same position in the argument list "array".
361 PArgList
& operator<<(
362 int sh
/// Number of parameters to shift forward through list
365 /** Shift the parameters by the specified amount. This allows the parameters
366 to be parsed at the same position in the argument list "array".
368 PArgList
& operator>>(
369 int sh
/// Number of parameters to shift backward through list
375 /** This function is called when access to illegal parameter index is made
376 in the GetParameter function. The default behaviour is to output a
377 message to the standard #PError# stream.
379 virtual void IllegalArgumentIndex(
380 PINDEX idx
/// Number of the parameter that was accessed.
383 /** This function is called when an unknown option was specified on the
384 command line. The default behaviour is to output a message to the
385 standard #PError# stream.
387 virtual void UnknownOption(
388 const PString
& option
/// Option that was illegally placed on command line.
391 /** This function is called when an option that requires an associated
392 string was specified on the command line but no associated string was
393 provided. The default behaviour is to output a message to the standard
396 virtual void MissingArgument(
397 const PString
& option
/// Option for which the associated string was missing.
402 /// The original program arguments.
403 PStringArray argumentArray
;
404 /// The specification letters for options
405 PString optionLetters
;
406 /// The specification strings for options
407 PStringArray optionNames
;
408 /// The count of the number of times an option appeared in the command line.
409 PIntArray optionCount
;
410 /// The array of associated strings to options.
411 PStringArray optionString
;
412 /// The index of each .
413 PIntArray parameterIndex
;
414 /// Shift count for the parameters in the argument list.
418 BOOL
ParseOption(PINDEX idx
, PINDEX offset
, PINDEX
& arg
, const PIntArray
& canHaveOptionString
);
419 PINDEX
GetOptionCountByIndex(PINDEX idx
) const;
420 PString
GetOptionStringByIndex(PINDEX idx
, const char * dflt
) const;
424 /**This class parse command line arguments with the ability to override them
425 from a PConfig file/registry.
427 class PConfigArgs
: public PArgList
429 PCLASSINFO(PConfigArgs
, PArgList
);
431 /**@name Construction */
434 const PArgList
& args
/// Raw argument list.
438 /**@name Overrides from class PArgList */
440 /** Get the count of the number of times the option was specified on the
443 @return option repeat count.
445 virtual PINDEX
GetOptionCount(
446 char optionChar
/// Character letter code for the option
448 /** Get the count of option */
449 virtual PINDEX
GetOptionCount(
450 const char * optionStr
/// String code for the option
452 /** Get the count of option */
453 virtual PINDEX
GetOptionCount(
454 const PString
& optionName
/// String code for the option
457 /** Get option string.
458 Gets the string associated with an option e.g. -ofile or -o file
459 would return the string "file". An option may have an associated string
460 if it had a ':' character folowing it in the specification string passed
461 to the Parse() function.
463 @return the options associated string.
465 virtual PString
GetOptionString(
466 char optionChar
, /// Character letter code for the option
467 const char * dflt
= NULL
/// Default value of the option string
469 /** Get option string. */
470 virtual PString
GetOptionString(
471 const char * optionStr
, /// String letter code for the option
472 const char * dflt
= NULL
/// Default value of the option string
474 /** Get option string. */
475 virtual PString
GetOptionString(
476 const PString
& optionName
, /// String code for the option
477 const char * dflt
= NULL
/// Default value of the option string
481 /**@name Overrides from class PArgList */
483 /**Save the current options to the PConfig.
484 This function will check to see if the option name is present and if
485 so, save to the PConfig all of the arguments present in the currently
486 parsed list. Note that the optionName for saving is not saved to the
487 PConfig itself as this would cause the data to be saved always!
490 const PString
& optionName
/// Option name for saving.
493 /**Set the PConfig section name for options.
496 const PString
& section
/// New section name
497 ) { sectionName
= section
; }
499 /**Get the PConfig section name for options.
501 const PString
& GetSectionName() const { return sectionName
; }
503 /**Set the prefix for option negation.
504 The default is "no-".
506 void SetNegationPrefix(
507 const PString
& prefix
/// New prefix string
508 ) { negationPrefix
= prefix
; }
510 /**Get the prefix for option negation.
511 The default is "no-".
513 const PString
& GetNegationPrefix() const { return negationPrefix
; }
518 PString
CharToString(char ch
) const;
521 PString negationPrefix
;
526 // End Of File ///////////////////////////////////////////////////////////////