]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/cmdlpars.tex
Fixed missing Curley Brace
[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 desired options 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} later.
235
236 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserargc}
237
238 \func{}{wxCmdLineParser}{\param{int }{argc}, \param{char** }{argv}}
239
240 Constructor specifies the command line to parse. This is the traditional
241 (Unix) command line format. The parameters {\it argc} and {\it argv} have the
242 same meaning as for {\tt main()} function.
243
244 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserstr}
245
246 \func{}{wxCmdLineParser}{\param{const wxString\& }{cmdline}}
247
248 Constructor specifies the command line to parse in Windows format. The parameter
249 {\it cmdline} has the same meaning as the corresponding parameter of
250 {\tt WinMain()}.
251
252 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdesc}
253
254 \func{}{wxCmdLineParser}{\param{const wxCmdLineEntryDesc* }{desc}}
255
256 Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserdef}, but also
257 specifies the \helpref{command line description}{wxcmdlineparsersetdesc}.
258
259 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdescargc}
260
261 \func{}{wxCmdLineParser}{\param{const wxCmdLineEntryDesc* }{desc}, \param{int }{argc}, \param{char** }{argv}}
262
263 Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserargc}, but also
264 specifies the \helpref{command line description}{wxcmdlineparsersetdesc}.
265
266 \membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserdescstr}
267
268 \func{}{wxCmdLineParser}{\param{const wxCmdLineEntryDesc* }{desc}, \param{const wxString\& }{cmdline}}
269
270 Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserstr}, but also
271 specifies the \helpref{command line description}{wxcmdlineparsersetdesc}.
272
273 \membersection{wxCmdLineParser::SetCmdLine}\label{wxcmdlineparsersetcmdlineargc}
274
275 \func{void}{SetCmdLine}{\param{int }{argc}, \param{char** }{argv}}
276
277 Set command line to parse after using one of the constructors which don't do it.
278
279 \wxheading{See also}
280
281 \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserargc}
282
283 \membersection{wxCmdLineParser::SetCmdLine}\label{wxcmdlineparsersetcmdlinestr}
284
285 \func{void}{SetCmdLine}{\param{const wxString\& }{cmdline}}
286
287 Set command line to parse after using one of the constructors which don't do it.
288
289 \wxheading{See also}
290
291 \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserstr}
292
293 \membersection{wxCmdLineParser::\destruct{wxCmdLineParser}}\label{wxcmdlineparserdtor}
294
295 \func{}{\destruct{wxCmdLineParser}}{\void}
296
297 Frees resources allocated by the object.
298
299 {\bf NB:} destructor is not virtual, don't use this class polymorphically.
300
301 \membersection{wxCmdLineParser::SetSwitchChars}\label{wxcmdlineparsersetswitchchars}
302
303 \func{void}{SetSwitchChars}{\param{const wxString\& }{switchChars}}
304
305 {\it switchChars} contains all characters with which an option or switch may
306 start. Default is {\tt "-"} for Unix, {\tt "-/"} for Windows.
307
308 \membersection{wxCmdLineParser::EnableLongOptions}\label{wxcmdlineparserenablelongoptions}
309
310 \func{void}{EnableLongOptions}{\param{bool }{enable = TRUE}}
311
312 Enable or disable support for the long options.
313
314 As long options are not (yet) POSIX-compliant, this option allows to disable
315 them.
316
317 \wxheading{See also}
318
319 \helpref{Customization}{wxcmdlineparsercustomization}
320
321 \membersection{wxCmdLineParser::DisableLongOptions}\label{wxcmdlineparserdisablelongoptions}
322
323 \func{void}{DisableLongOptions}{\void}
324
325 Ientical to \helpref{EnableLongOptions(FALSE)}{wxcmdlineparserenablelongoptions}.
326
327 \membersection{wxCmdLineParser::SetLogo}\label{wxcmdlineparsersetlogo}
328
329 \func{void}{SetLogo}{\param{const wxString\& }{logo}}
330
331 {\it logo} is some extra text which will be shown by
332 \helpref{Usage}{wxcmdlineparserusage} method.
333
334 \membersection{wxCmdLineParser::SetDesc}\label{wxcmdlineparsersetdesc}
335
336 \func{void}{SetDesc}{\param{const wxCmdLineEntryDesc* }{desc}}
337
338 Construct the command line description
339
340 Take the command line description from the wxCMD\_LINE\_NONE terminated table.
341
342 Example of usage:
343
344 \begin{verbatim}
345 static const wxCmdLineEntryDesc cmdLineDesc[] =
346 {
347 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
348 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
349
350 { wxCMD_LINE_OPTION, "o", "output", "output file" },
351 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
352 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
353 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
354
355 { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
356
357 { wxCMD_LINE_NONE }
358 };
359
360 wxCmdLineParser parser;
361
362 parser.SetDesc(cmdLineDesc);
363 \end{verbatim}
364
365 \membersection{wxCmdLineParser::AddSwitch}\label{wxcmdlineparseraddswitch}
366
367 \func{void}{AddSwitch}{\param{const wxString\& }{name}, \param{const wxString\& }{lng = wxEmptyString}, \param{const wxString\& }{desc = wxEmptyString}, \param{int }{flags = 0}}
368
369 Add a switch {\it name} with an optional long name {\it lng} (no long name if it
370 is empty, which is default), description {\it desc} and flags {\it flags} to the
371 command line description.
372
373 \membersection{wxCmdLineParser::AddOption}\label{wxcmdlineparseraddoption}
374
375 \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}}
376
377 Add an option {\it name} with an optional long name {\it lng} (no long name if
378 it is empty, which is default) taking a value of the given type (string by
379 default) to the command line description.
380
381 \membersection{wxCmdLineParser::AddParam}\label{wxcmdlineparseraddparam}
382
383 \func{void}{AddParam}{\param{const wxString\& }{desc = wxEmptyString}, \param{wxCmdLineParamType }{type = wxCMD\_LINE\_VAL\_STRING}, \param{int }{flags = 0}}
384
385 Add a parameter of the given {\it type} to the command line description.
386
387 \membersection{wxCmdLineParser::Parse}\label{wxcmdlineparserparse}
388
389 \func{int}{Parse}{\void}
390
391 Parse the command line, return $0$ if ok, $-1$ if {\tt "-h"} or {\tt "--help"}
392 option was encountered and the help message was given or a positive value if a
393 syntax error occured.
394
395 \membersection{wxCmdLineParser::Usage}\label{wxcmdlineparserusage}
396
397 \func{void}{Usage}{\void}
398
399 Give the standard usage message describing all program options. It will use the
400 options and parameters descriptions specified earlier, so the resulting message
401 will not be helpful to the user unless the descriptions were indeed specified.
402
403 \wxheading{See also}
404
405 \helpref{SetLogo}{wxcmdlineparsersetlogo}
406
407 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfoundswitch}
408
409 \constfunc{bool}{Found}{\param{const wxString\& }{name}}
410
411 Returns TRUE if the given switch was found, FALSE otherwise.
412
413 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfoundstringoption}
414
415 \constfunc{bool}{Found}{\param{const wxString\& }{name}, \param{wxString* }{value}}
416
417 Returns TRUE if an option taking a string value was found and stores the
418 value in the provided pointer (which should not be NULL).
419
420 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfoundintoption}
421
422 \constfunc{bool}{Found}{\param{const wxString\& }{name}, \param{long* }{value}}
423
424 Returns TRUE if an option taking an integer value was found and stores
425 the value in the provided pointer (which should not be NULL).
426
427 \membersection{wxCmdLineParser::Found}\label{wxcmdlineparserfounddateoption}
428
429 \constfunc{bool}{Found}{\param{const wxString\& }{name}, \param{wxDateTime* }{value}}
430
431 Returns TRUE if an option taking a date value was found and stores the
432 value in the provided pointer (which should not be NULL).
433
434 \membersection{wxCmdLineParser::GetParamCount}\label{wxcmdlineparsergetparamcount}
435
436 \constfunc{size\_t}{GetParamCount}{\void}
437
438 Returns the number of parameters found. This function makes sense mostly if you
439 had used {\tt wxCMD\_LINE\_PARAM\_MULTIPLE} flag.
440
441 \membersection{wxCmdLineParser::GetParam}\label{wxcmdlineparsergetparam}
442
443 \constfunc{wxString}{GetParam}{\param{size\_t }{n = 0u}}
444
445 Returns the value of Nth parameter (as string only for now).
446
447 \wxheading{See also}
448
449 \helpref{GetParamCount}{wxcmdlineparsergetparamcount}
450