]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/cmdlpars.tex
second merge of the 2.2 branch (RL)
[wxWidgets.git] / docs / latex / wx / cmdlpars.tex
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %% Name: cmdlpars.tex
3 %% Purpose: wxCmdLineParser documentation
4 %% Author: Vadim Zeitlin
5 %% Modified by:
6 %% Created: 27.03.00
7 %% RCS-ID: $Id$
8 %% Copyright: (c) Vadim Zeitlin
9 %% License: wxWindows license
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
12 \section{\class{wxCmdLineParser}}\label{wxcmdlineparser}
13
14 wxCmdLineParser is a class for parsing command line.
15
16 It has the following features:
17 \begin{enumerate}
18 \item distinguishes options, switches and parameters; allows option grouping
19 \item allows both short and long options
20 \item automatically generates the usage message from the command line description
21 \item does type checks on the options values (number, date, $\ldots$).
22 \end{enumerate}
23
24 To use it you should follow these steps:
25 \begin{enumerate}
26 \item \helpref{construct}{wxcmdlineparserconstruction} an object of this class
27 giving it the command line to parse and optionally its description or use
28 {\tt AddXXX()} functions later
29 \item call {\tt Parse()}
30 \item use {\tt Found()} to retrieve the results
31 \end{enumerate}
32
33 In the documentation below the following terminology is used:
34
35 \begin{twocollist}\itemsep=0pt
36 \twocolitem{switch}{This is a boolean option which can be given or not, but
37 which doesn't have any value. We use the word switch to distinguish such boolean
38 options from more generic options like those described below. For example,
39 {\tt -v} might be a switch meaning "enable verbose mode".}
40 \twocolitem{option}{Option for us here is something which comes with a value 0
41 unlike a switch. For example, {\tt -o:filename} might be an option which allows
42 to specify the name of the output file.}
43 \twocolitem{parameter}{This is a required program argument.}
44 \end{twocollist}
45
46 \wxheading{Derived from}
47
48 No base class
49
50 \wxheading{Include files}
51
52 <wx/cmdline.h>
53
54 \wxheading{Constants}
55
56 The structure wxCmdLineEntryDesc is used to describe the one command
57 line switch, option or parameter. An array of such structures should be passed
58 to \helpref{SetDesc()}{wxcmdlineparsersetdesc}. Also, the meanings of parameters
59 of the {\tt AddXXX()} functions are the same as of the corresponding fields in
60 this structure:
61
62 \begin{verbatim}
63 struct wxCmdLineEntryDesc
64 {
65 wxCmdLineEntryType kind;
66 const wxChar *shortName;
67 const wxChar *longName;
68 const wxChar *description;
69 wxCmdLineParamType type;
70 int flags;
71 };
72 \end{verbatim}
73
74 The type of a command line entity is in the {\tt kind} field and may be one of
75 the following constants:
76 {\small \begin{verbatim}
77 enum wxCmdLineEntryType
78 {
79 wxCMD\_LINE\_SWITCH,
80 wxCMD\_LINE\_OPTION,
81 wxCMD\_LINE\_PARAM,
82 wxCMD\_LINE\_NONE // use this to terminate the list
83 }
84 \end{verbatim}}
85
86 The field {\tt shortName} is the usual, short, name of the switch or the option.
87 {\tt longName} is the corresponding long name or NULL if the option has no long
88 name. Both of these fields are unused for the parameters. Both the short and
89 long option names can contain only letters, digits and the underscores.
90
91 {\tt description} is used by the \helpref{Usage()}{wxcmdlineparserusage} method
92 to construct a help message explaining the syntax of the program.
93
94 The possible values of {\tt type} which specifies the type of the value accepted
95 by an option or parameter are:
96 {\small \begin{verbatim}
97 enum wxCmdLineParamType
98 {
99 wxCMD\_LINE\_VAL\_STRING, // default
100 wxCMD\_LINE\_VAL\_NUMBER,
101 wxCMD\_LINE\_VAL\_DATE,
102 wxCMD\_LINE\_VAL\_NONE
103 }
104 \end{verbatim}}
105
106 Finally, the {\tt flags} field is a combination of the following bit masks:
107 {\small \begin{verbatim}
108 enum
109 {
110 wxCMD\_LINE\_OPTION\_MANDATORY = 0x01, // this option must be given
111 wxCMD\_LINE\_PARAM\_OPTIONAL = 0x02, // the parameter may be omitted
112 wxCMD\_LINE\_PARAM\_MULTIPLE = 0x04, // the parameter may be repeated
113 wxCMD\_LINE\_OPTION\_HELP = 0x08, // this option is a help request
114 wxCMD\_LINE\_NEEDS\_SEPARATOR = 0x10, // must have sep before the value
115 }
116 \end{verbatim}}
117
118 Notice that by default (i.e. if flags are just $0$), options are optional (sic)
119 and each call to \helpref{AddParam()}{wxcmdlineparseraddparam} allows one more
120 parameter - this may be changed by giving non-default flags to it, i.e. use
121 {\tt wxCMD\_LINE\_OPTION\_MANDATORY} to require that the option is given and
122 {\tt wxCMD\_LINE\_PARAM\_OPTIONAL} to make a parameter optional. Also,
123 {\tt wxCMD\_LINE\_PARAM\_MULTIPLE} may be specified if the programs accepts a
124 variable number of parameters - but it only can be given for the last parameter
125 in the command line description. If you use this flag, you will probably need to
126 use \helpref{GetParamCount}{wxcmdlineparsergetparamcount} to retrieve the number
127 of parameters effectively specified after calling
128 \helpref{Parse}{wxcmdlineparserparse}.
129
130 The last flag {\tt wxCMD\_LINE\_NEEDS\_SEPARATOR} can be specified to require a
131 separator (either a colon, an equal sign or white space) between the option
132 name and its value. By default, no separator is required.
133
134 \wxheading{See also}
135
136 \helpref{wxApp::argc}{wxappargc} and \helpref{wxApp::argv}{wxappargv}\\
137 console sample
138
139 %%%%%%%%%%%%% Methods by group %%%%%%%%%%%%%
140 \latexignore{\rtfignore{\wxheading{Function groups}}}
141
142 \membersection{Construction}\label{wxcmdlineparserconstruction}
143
144 Before \helpref{Parse}{wxcmdlineparserparse} can be called, the command line
145 parser object must have the command line to parse and also the rules saying
146 which switches, options and parameters are valid - this is called command line
147 description in what follows.
148
149 You have complete freedom of choice as to when specify the required information,
150 the only restriction is that it must be done before calling
151 \helpref{Parse}{wxcmdlineparserparse}.
152
153 To specify the command line to parse you may use either one of constructors
154 accepting it (\helpref{wxCmdLineParser(argc, argv)}{wxcmdlineparserwxcmdlineparserargc} or
155 \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserdescargc} usually) or,
156 if you use \helpref{the default constructor}{wxcmdlineparserwxcmdlineparserdef},
157 you can do it later by calling
158 \helpref{SetCmdLine}{wxcmdlineparsersetcmdlineargc}.
159
160 The same holds for command line description: it can be specified either in
161 the constructor (\helpref{without command line}{wxcmdlineparserwxcmdlineparserdesc} or
162 \helpref{together with it}{wxcmdlineparserwxcmdlineparserdescargc}) or
163 constructed later using either \helpref{SetDesc}{wxcmdlineparsersetdesc} or
164 combination of \helpref{AddSwitch}{wxcmdlineparseraddswitch},
165 \helpref{AddOption}{wxcmdlineparseraddoption} and
166 \helpref{AddParam}{wxcmdlineparseraddparam} methods.
167
168 Using constructors or \helpref{SetDesc}{wxcmdlineparsersetdesc} uses a (usually
169 {\tt const static}) table containing the command line description. If you want
170 to decide which options to acccept during the run-time, using one of the
171 {\tt AddXXX()} functions above might be preferable.
172
173 \membersection{Customization}\label{wxcmdlineparsercustomization}
174
175 wxCmdLineParser has several global options which may be changed by the
176 application. All of the functions described in this section should be called
177 before \helpref{Parse}{wxcmdlineparserparse}.
178
179 First global option is the support for long (also known as GNU-style) options.
180 The long options are the ones which start with two dashes ({\tt "--"}) and look
181 like this: {\tt --verbose}, i.e. they generally are complete words and not some
182 abbreviations of them. As long options are used by more and more applications,
183 they are enabled by default, but may be disabled with
184 \helpref{DisableLongOptions}{wxcmdlineparserdisablelongoptions}.
185
186 Another global option is the set of characters which may be used to start an
187 option (otherwise, the word on the command line is assumed to be a parameter).
188 Under Unix, {\tt '-'} is always used, but Windows has at least two common
189 choices for this: {\tt '-'} and {\tt '/'}. Some programs also use {\tt '+'}.
190 The default is to use what suits most the current platform, but may be changed
191 with \helpref{SetSwitchChars}{wxcmdlineparsersetswitchchars} method.
192
193 Finally, \helpref{SetLogo}{wxcmdlineparsersetlogo} can be used to show some
194 application-specific text before the explanation given by
195 \helpref{Usage}{wxcmdlineparserusage} function.
196
197 \membersection{Parsing command line}\label{wxcmdlineparserparsing}
198
199 After the command line description was constructed and the desiredoptions were
200 set, you can finally call \helpref{Parse}{wxcmdlineparserparse} method.
201 It returns $0$ if the command line was correct and was parsed, $-1$ if the help
202 option was specified (this is a separate case as, normally, the program will
203 terminate after this) or a positive number if there was an error during the
204 command line parsing.
205
206 In the latter case, the appropriate error message and usage information are
207 logged by wxCmdLineParser itself using the standard wxWindows logging functions.
208
209 \membersection{Getting results}\label{wxcmdlineparsergettingresults}
210
211 After calling \helpref{Parse}{wxcmdlineparserparse} (and if it returned $0$),
212 you may access the results of parsing using one of overloaded {\tt Found()}
213 methods.
214
215 For a simple switch, you will simply call
216 \helpref{Found}{wxcmdlineparserfoundswitch} to determine if the switch was given
217 or not, for an option or a parameter, you will call a version of {\tt Found()}
218 which also returns the associated value in the provided variable. All
219 {\tt Found()} functions return TRUE if the switch or option were found in the
220 command line or FALSE if they were not specified.
221
222 %%%%%%%%%%%%% Methods in alphabetic order %%%%%%%%%%%%%
223 \helponly{\insertatlevel{2}{
224
225 \wxheading{Members}
226
227 }}
228
229 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdef}
230
231 \func{}{wxCmdLineParser}{\void}
232
233 Default constructor. You must use
234 \helpref{SetCmdLine}{wxcmdlineparsersetcmdlineargc} or
235 \helpref{SetCmdLinelater.}{wxcmdlineparsersetcmdlinestr} later.
236
237 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserargc}
238
239 \func{}{wxCmdLineParser}{\param{int }{argc}, \param{char** }{argv}}
240
241 Constructor specifies the command line to parse. This is the traditional
242 (Unix) command line format. The parameters {\it argc} and {\it argv} have the
243 same meaning as for {\tt main()} function.
244
245 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserstr}
246
247 \func{}{wxCmdLineParser}{\param{const wxString\& }{cmdline}}
248
249 Constructor specifies the command line to parse in Windows format. The parameter
250 {\it cmdline} has the same meaning as the corresponding parameter of
251 {\tt WinMain()}.
252
253 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdesc}
254
255 \func{}{wxCmdLineParser}{\param{const wxCmdLineEntryDesc* }{desc}}
256
257 Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserdef}, but also
258 specifies the \helpref{command line description}{wxcmdlineparsersetdesc}.
259
260 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdescargc}
261
262 \func{}{wxCmdLineParser}{\param{const wxCmdLineEntryDesc* }{desc}, \param{int }{argc}, \param{char** }{argv}}
263
264 Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserargc}, but also
265 specifies the \helpref{command line description}{wxcmdlineparsersetdesc}.
266
267 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdescstr}
268
269 \func{}{wxCmdLineParser}{\param{const wxCmdLineEntryDesc* }{desc}, \param{const wxString\& }{cmdline}}
270
271 Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserstr}, but also
272 specifies the \helpref{command line description}{wxcmdlineparsersetdesc}.
273
274 \membersection{wxCmdLineParser::SetCmdLine}\label{wxcmdlineparsersetcmdlineargc}
275
276 \func{void}{SetCmdLine}{\param{int }{argc}, \param{char** }{argv}}
277
278 Set command line to parse after using one of the constructors which don't do it.
279
280 \wxheading{See also}
281
282 \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserargc}
283
284 \membersection{wxCmdLineParser::SetCmdLine}\label{wxcmdlineparsersetcmdlinestr}
285
286 \func{void}{SetCmdLine}{\param{const wxString\& }{cmdline}}
287
288 Set command line to parse after using one of the constructors which don't do it.
289
290 \wxheading{See also}
291
292 \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserstr}
293
294 \membersection{wxCmdLineParser::\destruct{wxCmdLineParser}}\label{wxcmdlineparserdtor}
295
296 \func{}{\destruct{wxCmdLineParser}}{\void}
297
298 Frees resources allocated by the object.
299
300 {\bf NB:} destructor is not virtual, don't use this class polymorphically.
301
302 \membersection{wxCmdLineParser::SetSwitchChars}\label{wxcmdlineparsersetswitchchars}
303
304 \func{void}{SetSwitchChars}{\param{const wxString\& }{switchChars}}
305
306 {\it switchChars} contains all characters with which an option or switch may
307 start. Default is {\tt "-"} for Unix, {\tt "-/"} for Windows.
308
309 \membersection{wxCmdLineParser::EnableLongOptions}\label{wxcmdlineparserenablelongoptions}
310
311 \func{void}{EnableLongOptions}{\param{bool }{enable = TRUE}}
312
313 Enable or disable support for the long options.
314
315 As long options are not (yet) POSIX-compliant, this option allows to disable
316 them.
317
318 \wxheading{See also}
319
320 \helpref{Customization}{wxcmdlineparsercustomization}
321
322 \membersection{wxCmdLineParser::DisableLongOptions}\label{wxcmdlineparserdisablelongoptions}
323
324 \func{void}{DisableLongOptions}{\void}
325
326 Ientical to \helpref{EnableLongOptions(FALSE)}{wxcmdlineparserenablelongoptions}.
327
328 \membersection{wxCmdLineParser::SetLogo}\label{wxcmdlineparsersetlogo}
329
330 \func{void}{SetLogo}{\param{const wxString\& }{logo}}
331
332 {\it logo} is some extra text which will be shown by
333 \helpref{Usage}{wxcmdlineparserusage} method.
334
335 \membersection{wxCmdLineParser::SetDesc}\label{wxcmdlineparsersetdesc}
336
337 \func{void}{SetDesc}{\param{const wxCmdLineEntryDesc* }{desc}}
338
339 Construct the command line description
340
341 Take the command line description from the wxCMD\_LINE\_NONE terminated table.
342
343 Example of usage:
344
345 \begin{verbatim}
346 static const wxCmdLineEntryDesc cmdLineDesc[] =
347 {
348 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
349 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
350
351 { wxCMD_LINE_OPTION, "o", "output", "output file" },
352 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
353 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
354 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
355
356 { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
357
358 { wxCMD_LINE_NONE }
359 };
360
361 wxCmdLineParser parser;
362
363 parser.SetDesc(cmdLineDesc);
364 \end{verbatim}
365
366 \membersection{wxCmdLineParser::AddSwitch}\label{wxcmdlineparseraddswitch}
367
368 \func{void}{AddSwitch}{\param{const wxString\& }{name}, \param{const wxString\& }{lng = wxEmptyString}, \param{const wxString\& }{desc = wxEmptyString}, \param{int }{flags = 0}}
369
370 Add a switch {\it name} with an optional long name {\it lng} (no long name if it
371 is empty, which is default), description {\it desc} and flags {\it flags} to the
372 command line description.
373
374 \membersection{wxCmdLineParser::AddOption}\label{wxcmdlineparseraddoption}
375
376 \func{void}{AddOption}{\param{const wxString\& }{name}, \param{const wxString\& }{lng = wxEmptyString}, \param{const wxString\& }{desc = wxEmptyString}, \param{wxCmdLineParamType }{type = wxCMD\_LINE\_VAL\_STRING}, \param{int }{flags = 0}}
377
378 Add an option {\it name} with an optional long name {\it lng} (no long name if
379 it is empty, which is default) taking a value of the given type (string by
380 default) to the command line description.
381
382 \membersection{wxCmdLineParser::AddParam}\label{wxcmdlineparseraddparam}
383
384 \func{void}{AddParam}{\param{const wxString\& }{desc = wxEmptyString}, \param{wxCmdLineParamType }{type = wxCMD\_LINE\_VAL\_STRING}, \param{int }{flags = 0}}
385
386 Add a parameter of the given {\it type} to the command line description.
387
388 \membersection{wxCmdLineParser::Parse}\label{wxcmdlineparserparse}
389
390 \func{int}{Parse}{\void}
391
392 Parse the command line, return $0$ if ok, $-1$ if {\tt "-h"} or {\tt "--help"}
393 option was encountered and the help message was given or a positive value if a
394 syntax error occured.
395
396 \membersection{wxCmdLineParser::Usage}\label{wxcmdlineparserusage}
397
398 \func{void}{Usage}{\void}
399
400 Give the standard usage message describing all program options. It will use the
401 options and parameters descriptions specified earlier, so the resulting message
402 will not be helpful to the user unless the descriptions were indeed specified.
403
404 \wxheading{See also}
405
406 \helpref{SetLogo}{wxcmdlineparsersetlogo}
407
408 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfoundswitch}
409
410 \constfunc{bool}{Found}{\param{const wxString\& }{name}}
411
412 Returns TRUE if the given switch was found, FALSE otherwise.
413
414 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfoundstringoption}
415
416 \constfunc{bool}{Found}{\param{const wxString\& }{name}, \param{wxString* }{value}}
417
418 Returns TRUE if an option taking a string value was found and stores the
419 value in the provided pointer (which should not be NULL).
420
421 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfoundintoption}
422
423 \constfunc{bool}{Found}{\param{const wxString\& }{name}, \param{long* }{value}}
424
425 Returns TRUE if an option taking an integer value was found and stores
426 the value in the provided pointer (which should not be NULL).
427
428 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfounddateoption}
429
430 \constfunc{bool}{Found}{\param{const wxString\& }{name}, \param{wxDateTime* }{value}}
431
432 Returns TRUE if an option taking a date value was found and stores the
433 value in the provided pointer (which should not be NULL).
434
435 \membersection{wxCmdLineParser::GetParamCount}\label{wxcmdlineparsergetparamcount}
436
437 \constfunc{size\_t}{GetParamCount}{\void}
438
439 Returns the number of parameters found. This function makes sense mostly if you
440 had used {\tt wxCMD\_LINE\_PARAM\_MULTIPLE} flag.
441
442 \membersection{wxCmdLineParser::GetParam}\label{wxcmdlineparsergetparam}
443
444 \constfunc{wxString}{GetParam}{\param{size\_t }{n = 0u}}
445
446 Returns the value of Nth parameter (as string only for now).
447
448 \wxheading{See also}
449
450 \helpref{GetParamCount}{wxcmdlineparsergetparamcount}
451