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