]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/cmdlpars.tex
Doc corrections; added HelpGen project files
[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
97d59046 199After the command line description was constructed and the desired options were
f6bcfd97
BP
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
97d59046 234\helpref{SetCmdLine}{wxcmdlineparsersetcmdlineargc} later.
f6bcfd97
BP
235
236\membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserargc}
237
238\func{}{wxCmdLineParser}{\param{int }{argc}, \param{char** }{argv}}
239
240Constructor specifies the command line to parse. This is the traditional
241(Unix) command line format. The parameters {\it argc} and {\it argv} have the
242same meaning as for {\tt main()} function.
243
244\membersection{wxCmdLineParser::wxCmdLineParser}\label{wxcmdlineparserwxcmdlineparserstr}
245
246\func{}{wxCmdLineParser}{\param{const wxString\& }{cmdline}}
247
248Constructor 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
256Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserdef}, but also
257specifies 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
263Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserargc}, but also
264specifies 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
270Same as \helpref{wxCmdLineParser}{wxcmdlineparserwxcmdlineparserstr}, but also
271specifies 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
277Set 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
287Set 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
297Frees 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
306start. 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
312Enable or disable support for the long options.
313
314As long options are not (yet) POSIX-compliant, this option allows to disable
315them.
316
317\wxheading{See also}
318
319\helpref{Customization}{wxcmdlineparsercustomization}
320
321\membersection{wxCmdLineParser::DisableLongOptions}\label{wxcmdlineparserdisablelongoptions}
322
323\func{void}{DisableLongOptions}{\void}
324
325Ientical 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
338Construct the command line description
339
340Take the command line description from the wxCMD\_LINE\_NONE terminated table.
341
342Example of usage:
343
344\begin{verbatim}
345static 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
360wxCmdLineParser parser;
361
362parser.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
369Add a switch {\it name} with an optional long name {\it lng} (no long name if it
370is empty, which is default), description {\it desc} and flags {\it flags} to the
371command 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
377Add an option {\it name} with an optional long name {\it lng} (no long name if
378it is empty, which is default) taking a value of the given type (string by
379default) 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
385Add 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
391Parse the command line, return $0$ if ok, $-1$ if {\tt "-h"} or {\tt "--help"}
392option was encountered and the help message was given or a positive value if a
393syntax error occured.
394
395\membersection{wxCmdLineParser::Usage}\label{wxcmdlineparserusage}
396
397\func{void}{Usage}{\void}
398
399Give the standard usage message describing all program options. It will use the
400options and parameters descriptions specified earlier, so the resulting message
401will 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
411Returns 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
417Returns TRUE if an option taking a string value was found and stores the
418value 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
424Returns TRUE if an option taking an integer value was found and stores
425the 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
431Returns TRUE if an option taking a date value was found and stores the
432value in the provided pointer (which should not be NULL).
433
434\membersection{wxCmdLineParser::GetParamCount}\label{wxcmdlineparsergetparamcount}
435
436\constfunc{size\_t}{GetParamCount}{\void}
437
438Returns the number of parameters found. This function makes sense mostly if you
439had 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
445Returns the value of Nth parameter (as string only for now).
446
447\wxheading{See also}
448
449\helpref{GetParamCount}{wxcmdlineparsergetparamcount}
450