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