]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/cmdlpars.tex
v2.2.1 TAGGED copy ported over to main trunk of CVS
[wxWidgets.git] / docs / latex / wx / cmdlpars.tex
CommitLineData
f6bcfd97
BP
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
14wxCmdLineParser is a class for parsing command line.
15
16It 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
24To use it you should follow these steps:
25\begin{enumerate}
26\item \helpref{construct}{wxcmdlineparserconstruction} an object of this class
27giving 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
33In 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
37which doesn't have any value. We use the word switch to distinguish such boolean
38options 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
41unlike a switch. For example, {\tt -o:filename} might be an option which allows
42to 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
48No base class
49
50\wxheading{Include files}
51
52<wx/cmdline.h>
53
54\wxheading{Constants}
55
56The structure wxCmdLineEntryDesc is used to describe the one command
57line switch, option or parameter. An array of such structures should be passed
58to \helpref{SetDesc()}{wxcmdlineparsersetdesc}. Also, the meanings of parameters
59of the {\tt AddXXX()} functions are the same as of the corresponding fields in
60this structure:
61
62\begin{verbatim}
63struct 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
74The type of a command line entity is in the {\tt kind} field and may be one of
75the following constants:
76{\small \begin{verbatim}
77enum 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
86The 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
88name. Both of these fields are unused for the parameters. Both the short and
89long option names can contain only letters, digits and the underscores.
90
91{\tt description} is used by the \helpref{Usage()}{wxcmdlineparserusage} method
92to construct a help message explaining the syntax of the program.
93
94The possible values of {\tt type} which specifies the type of the value accepted
95by an option or parameter are:
96{\small \begin{verbatim}
97enum 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
106Finally, the {\tt flags} field is a combination of the following bit masks:
107{\small \begin{verbatim}
108enum
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
118Notice that by default (i.e. if flags are just $0$), options are optional (sic)
119and each call to \helpref{AddParam()}{wxcmdlineparseraddparam} allows one more
120parameter - 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
124variable number of parameters - but it only can be given for the last parameter
125in the command line description. If you use this flag, you will probably need to
126use \helpref{GetParamCount}{wxcmdlineparsergetparamcount} to retrieve the number
127of parameters effectively specified after calling
128\helpref{Parse}{wxcmdlineparserparse}.
129
130The last flag {\tt wxCMD\_LINE\_NEEDS\_SEPARATOR} can be specified to require a
131separator (either a colon, an equal sign or white space) between the option
132name 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}\\
137console sample
138
139%%%%%%%%%%%%% Methods by group %%%%%%%%%%%%%
140\latexignore{\rtfignore{\wxheading{Function groups}}}
141
142\membersection{Construction}\label{wxcmdlineparserconstruction}
143
144Before \helpref{Parse}{wxcmdlineparserparse} can be called, the command line
145parser object must have the command line to parse and also the rules saying
146which switches, options and parameters are valid - this is called command line
147description in what follows.
148
149You have complete freedom of choice as to when specify the required information,
150the only restriction is that it must be done before calling
151\helpref{Parse}{wxcmdlineparserparse}.
152
153To specify the command line to parse you may use either one of constructors
154accepting it (\helpref{wxCmdLineParser(argc, argv)}{wxcmdlineparserwxcmdlineparserargc} or
155\helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserdescargc} usually) or,
156if you use \helpref{the default constructor}{wxcmdlineparserwxcmdlineparserdef},
157you can do it later by calling
158\helpref{SetCmdLine}{wxcmdlineparsersetcmdlineargc}.
159
160The same holds for command line description: it can be specified either in
161the constructor (\helpref{without command line}{wxcmdlineparserwxcmdlineparserdesc} or
162\helpref{together with it}{wxcmdlineparserwxcmdlineparserdescargc}) or
163constructed later using either \helpref{SetDesc}{wxcmdlineparsersetdesc} or
164combination of \helpref{AddSwitch}{wxcmdlineparseraddswitch},
165\helpref{AddOption}{wxcmdlineparseraddoption} and
166\helpref{AddParam}{wxcmdlineparseraddparam} methods.
167
168Using constructors or \helpref{SetDesc}{wxcmdlineparsersetdesc} uses a (usually
169{\tt const static}) table containing the command line description. If you want
170to 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
175wxCmdLineParser has several global options which may be changed by the
176application. All of the functions described in this section should be called
177before \helpref{Parse}{wxcmdlineparserparse}.
178
179First global option is the support for long (also known as GNU-style) options.
180The long options are the ones which start with two dashes ({\tt "--"}) and look
181like this: {\tt --verbose}, i.e. they generally are complete words and not some
182abbreviations of them. As long options are used by more and more applications,
183they are enabled by default, but may be disabled with
184\helpref{DisableLongOptions}{wxcmdlineparserdisablelongoptions}.
185
186Another global option is the set of characters which may be used to start an
187option (otherwise, the word on the command line is assumed to be a parameter).
188Under Unix, {\tt '-'} is always used, but Windows has at least two common
189choices for this: {\tt '-'} and {\tt '/'}. Some programs also use {\tt '+'}.
190The default is to use what suits most the current platform, but may be changed
191with \helpref{SetSwitchChars}{wxcmdlineparsersetswitchchars} method.
192
193Finally, \helpref{SetLogo}{wxcmdlineparsersetlogo} can be used to show some
194application-specific text before the explanation given by
195\helpref{Usage}{wxcmdlineparserusage} function.
196
197\membersection{Parsing command line}\label{wxcmdlineparserparsing}
198
199After the command line description was constructed and the desiredoptions were
200set, you can finally call \helpref{Parse}{wxcmdlineparserparse} method.
201It returns $0$ if the command line was correct and was parsed, $-1$ if the help
202option was specified (this is a separate case as, normally, the program will
203terminate after this) or a positive number if there was an error during the
204command line parsing.
205
206In the latter case, the appropriate error message and usage information are
207logged by wxCmdLineParser itself using the standard wxWindows logging functions.
208
209\membersection{Getting results}\label{wxcmdlineparsergettingresults}
210
211After calling \helpref{Parse}{wxcmdlineparserparse} (and if it returned $0$),
212you may access the results of parsing using one of overloaded {\tt Found()}
213methods.
214
215For a simple switch, you will simply call
216\helpref{Found}{wxcmdlineparserfoundswitch} to determine if the switch was given
217or not, for an option or a parameter, you will call a version of {\tt Found()}
218which 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
220command 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
233Default 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
241Constructor specifies the command line to parse. This is the traditional
242(Unix) command line format. The parameters {\it argc} and {\it argv} have the
243same meaning as for {\tt main()} function.
244
245\membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserstr}
246
247\func{}{wxCmdLineParser}{\param{const wxString\& }{cmdline}}
248
249Constructor 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
257Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserdef}, but also
258specifies 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
264Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserargc}, but also
265specifies 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
271Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserstr}, but also
272specifies 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
278Set 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
288Set 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
298Frees 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
307start. 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
313Enable or disable support for the long options.
314
315As long options are not (yet) POSIX-compliant, this option allows to disable
316them.
317
318\wxheading{See also}
319
320\helpref{Customization}{wxcmdlineparsercustomization}
321
322\membersection{wxCmdLineParser::DisableLongOptions}\label{wxcmdlineparserdisablelongoptions}
323
324\func{void}{DisableLongOptions}{\void}
325
326Ientical 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
339Construct the command line description
340
341Take the command line description from the wxCMD\_LINE\_NONE terminated table.
342
343Example of usage:
344
345\begin{verbatim}
346static 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
361wxCmdLineParser parser;
362
363parser.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
370Add a switch {\it name} with an optional long name {\it lng} (no long name if it
371is empty, which is default), description {\it desc} and flags {\it flags} to the
372command 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
378Add an option {\it name} with an optional long name {\it lng} (no long name if
379it is empty, which is default) taking a value of the given type (string by
380default) 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
386Add 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
392Parse the command line, return $0$ if ok, $-1$ if {\tt "-h"} or {\tt "--help"}
393option was encountered and the help message was given or a positive value if a
394syntax error occured.
395
396\membersection{wxCmdLineParser::Usage}\label{wxcmdlineparserusage}
397
398\func{void}{Usage}{\void}
399
400Give the standard usage message describing all program options. It will use the
401options and parameters descriptions specified earlier, so the resulting message
402will 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
412Returns 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
418Returns TRUE if an option taking a string value was found and stores the
419value 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
425Returns TRUE if an option taking an integer value was found and stores
426the 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
432Returns TRUE if an option taking a date value was found and stores the
433value in the provided pointer (which should not be NULL).
434
435\membersection{wxCmdLineParser::GetParamCount}\label{wxcmdlineparsergetparamcount}
436
437\constfunc{size\_t}{GetParamCount}{\void}
438
439Returns the number of parameters found. This function makes sense mostly if you
440had 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
446Returns the value of Nth parameter (as string only for now).
447
448\wxheading{See also}
449
450\helpref{GetParamCount}{wxcmdlineparsergetparamcount}
451