1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Purpose: wxCmdLineParser documentation
4 %% Author: Vadim Zeitlin
8 %% Copyright: (c) Vadim Zeitlin
9 %% License: wxWindows license
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 \section{\class{wxCmdLineParser
}}\label{wxcmdlineparser
}
14 wxCmdLineParser is a class for parsing command line.
16 It has the following features:
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$).
25 To use it you should follow these steps:
27 \begin{enumerate
}\itemsep=
0pt
28 \item \helpref{construct
}{wxcmdlineparserconstruction
} an object of this class
29 giving 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
35 In the documentation below the following terminology is used:
37 \begin{twocollist
}\itemsep=
0pt
38 \twocolitem{switch
}{This is a boolean option which can be given or not, but
39 which doesn't have any value. We use the word switch to distinguish such boolean
40 options 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
43 unlike a switch. For example,
{\tt -o:filename
} might be an option which allows
44 to specify the name of the output file.
}
45 \twocolitem{parameter
}{This is a required program argument.
}
48 \wxheading{Derived from
}
52 \wxheading{Include files
}
58 The structure wxCmdLineEntryDesc is used to describe the one command
59 line switch, option or parameter. An array of such structures should be passed
60 to
\helpref{SetDesc()
}{wxcmdlineparsersetdesc
}. Also, the meanings of parameters
61 of the
{\tt AddXXX()
} functions are the same as of the corresponding fields in
65 struct wxCmdLineEntryDesc
67 wxCmdLineEntryType kind;
68 const wxChar *shortName;
69 const wxChar *longName;
70 const wxChar *description;
71 wxCmdLineParamType type;
76 The type of a command line entity is in the
{\tt kind
} field and may be one of
77 the following constants:
81 enum wxCmdLineEntryType
86 wxCMD
\_LINE\_NONE // use this to terminate the list
91 The 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
93 name. Both of these fields are unused for the parameters. Both the short and
94 long option names can contain only letters, digits and the underscores.
96 {\tt description
} is used by the
\helpref{Usage()
}{wxcmdlineparserusage
} method
97 to construct a help message explaining the syntax of the program.
99 The possible values of
{\tt type
} which specifies the type of the value accepted
100 by an option or parameter are:
104 enum wxCmdLineParamType
106 wxCMD
\_LINE\_VAL\_STRING, // default
107 wxCMD
\_LINE\_VAL\_NUMBER,
108 wxCMD
\_LINE\_VAL\_DATE,
109 wxCMD
\_LINE\_VAL\_NONE
114 Finally, the
{\tt flags
} field is a combination of the following bit masks:
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
129 Notice that by default (i.e. if flags are just $
0$), options are optional (sic)
130 and each call to
\helpref{AddParam()
}{wxcmdlineparseraddparam
} allows one more
131 parameter - 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
135 variable number of parameters - but it only can be given for the last parameter
136 in the command line description. If you use this flag, you will probably need to
137 use
\helpref{GetParamCount
}{wxcmdlineparsergetparamcount
} to retrieve the number
138 of parameters effectively specified after calling
139 \helpref{Parse
}{wxcmdlineparserparse
}.
141 The last flag
{\tt wxCMD
\_LINE\_NEEDS\_SEPARATOR} can be specified to require a
142 separator (either a colon, an equal sign or white space) between the option
143 name and its value. By default, no separator is required.
147 \helpref{wxApp::argc
}{wxappargc
} and
\helpref{wxApp::argv
}{wxappargv
}\\
150 %%%%%%%%%%%%% Methods by group %%%%%%%%%%%%%
151 \latexignore{\rtfignore{\wxheading{Function groups
}}}
153 \membersection{Construction
}\label{wxcmdlineparserconstruction
}
155 Before
\helpref{Parse
}{wxcmdlineparserparse
} can be called, the command line
156 parser object must have the command line to parse and also the rules saying
157 which switches, options and parameters are valid - this is called command line
158 description in what follows.
160 You have complete freedom of choice as to when specify the required information,
161 the only restriction is that it must be done before calling
162 \helpref{Parse
}{wxcmdlineparserparse
}.
164 To specify the command line to parse you may use either one of constructors
165 accepting it (
\helpref{wxCmdLineParser(argc, argv)
}{wxcmdlineparserwxcmdlineparserargc
} or
166 \helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserdescargc
} usually) or,
167 if you use
\helpref{the default constructor
}{wxcmdlineparserwxcmdlineparserdef
},
168 you can do it later by calling
169 \helpref{SetCmdLine
}{wxcmdlineparsersetcmdlineargc
}.
171 The same holds for command line description: it can be specified either in
172 the constructor (
\helpref{without command line
}{wxcmdlineparserwxcmdlineparserdesc
} or
173 \helpref{together with it
}{wxcmdlineparserwxcmdlineparserdescargc
}) or
174 constructed later using either
\helpref{SetDesc
}{wxcmdlineparsersetdesc
} or
175 combination of
\helpref{AddSwitch
}{wxcmdlineparseraddswitch
},
176 \helpref{AddOption
}{wxcmdlineparseraddoption
} and
177 \helpref{AddParam
}{wxcmdlineparseraddparam
} methods.
179 Using constructors or
\helpref{SetDesc
}{wxcmdlineparsersetdesc
} uses a (usually
180 {\tt const static
}) table containing the command line description. If you want
181 to decide which options to acccept during the run-time, using one of the
182 {\tt AddXXX()
} functions above might be preferable.
184 \membersection{Customization
}\label{wxcmdlineparsercustomization
}
186 wxCmdLineParser has several global options which may be changed by the
187 application. All of the functions described in this section should be called
188 before
\helpref{Parse
}{wxcmdlineparserparse
}.
190 First global option is the support for long (also known as GNU-style) options.
191 The long options are the ones which start with two dashes (
{\tt "--"
}) and look
192 like this:
{\tt --verbose
}, i.e. they generally are complete words and not some
193 abbreviations of them. As long options are used by more and more applications,
194 they are enabled by default, but may be disabled with
195 \helpref{DisableLongOptions
}{wxcmdlineparserdisablelongoptions
}.
197 Another global option is the set of characters which may be used to start an
198 option (otherwise, the word on the command line is assumed to be a parameter).
199 Under Unix,
{\tt '-'
} is always used, but Windows has at least two common
200 choices for this:
{\tt '-'
} and
{\tt '/'
}. Some programs also use
{\tt '+'
}.
201 The default is to use what suits most the current platform, but may be changed
202 with
\helpref{SetSwitchChars
}{wxcmdlineparsersetswitchchars
} method.
204 Finally,
\helpref{SetLogo
}{wxcmdlineparsersetlogo
} can be used to show some
205 application-specific text before the explanation given by
206 \helpref{Usage
}{wxcmdlineparserusage
} function.
208 \membersection{Parsing command line
}\label{wxcmdlineparserparsing
}
210 After the command line description was constructed and the desired options were
211 set, you can finally call
\helpref{Parse
}{wxcmdlineparserparse
} method.
212 It returns $
0$ if the command line was correct and was parsed, $-
1$ if the help
213 option was specified (this is a separate case as, normally, the program will
214 terminate after this) or a positive number if there was an error during the
215 command line parsing.
217 In the latter case, the appropriate error message and usage information are
218 logged by wxCmdLineParser itself using the standard wxWindows logging functions.
220 \membersection{Getting results
}\label{wxcmdlineparsergettingresults
}
222 After calling
\helpref{Parse
}{wxcmdlineparserparse
} (and if it returned $
0$),
223 you may access the results of parsing using one of overloaded
{\tt Found()
}
226 For a simple switch, you will simply call
227 \helpref{Found
}{wxcmdlineparserfoundswitch
} to determine if the switch was given
228 or not, for an option or a parameter, you will call a version of
{\tt Found()
}
229 which also returns the associated value in the provided variable. All
230 {\tt Found()
} functions return TRUE if the switch or option were found in the
231 command line or FALSE if they were not specified.
233 %%%%%%%%%%%%% Methods in alphabetic order %%%%%%%%%%%%%
234 \helponly{\insertatlevel{2}{
240 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdef
}
242 \func{}{wxCmdLineParser
}{\void}
244 Default constructor. You must use
245 \helpref{SetCmdLine
}{wxcmdlineparsersetcmdlineargc
} later.
247 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserargc
}
249 \func{}{wxCmdLineParser
}{\param{int
}{argc
},
\param{char**
}{argv
}}
251 Constructor specifies the command line to parse. This is the traditional
252 (Unix) command line format. The parameters
{\it argc
} and
{\it argv
} have the
253 same meaning as for
{\tt main()
} function.
255 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserstr
}
257 \func{}{wxCmdLineParser
}{\param{const wxString\&
}{cmdline
}}
259 Constructor specifies the command line to parse in Windows format. The parameter
260 {\it cmdline
} has the same meaning as the corresponding parameter of
263 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdesc
}
265 \func{}{wxCmdLineParser
}{\param{const wxCmdLineEntryDesc*
}{desc
}}
267 Same as
\helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserdef
}, but also
268 specifies the
\helpref{command line description
}{wxcmdlineparsersetdesc
}.
270 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdescargc
}
272 \func{}{wxCmdLineParser
}{\param{const wxCmdLineEntryDesc*
}{desc
},
\param{int
}{argc
},
\param{char**
}{argv
}}
274 Same as
\helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserargc
}, but also
275 specifies the
\helpref{command line description
}{wxcmdlineparsersetdesc
}.
277 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdescstr
}
279 \func{}{wxCmdLineParser
}{\param{const wxCmdLineEntryDesc*
}{desc
},
\param{const wxString\&
}{cmdline
}}
281 Same as
\helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserstr
}, but also
282 specifies the
\helpref{command line description
}{wxcmdlineparsersetdesc
}.
284 \membersection{wxCmdLineParser::SetCmdLine
}\label{wxcmdlineparsersetcmdlineargc
}
286 \func{void
}{SetCmdLine
}{\param{int
}{argc
},
\param{char**
}{argv
}}
288 Set command line to parse after using one of the constructors which don't do it.
292 \helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserargc
}
294 \membersection{wxCmdLineParser::SetCmdLine
}\label{wxcmdlineparsersetcmdlinestr
}
296 \func{void
}{SetCmdLine
}{\param{const wxString\&
}{cmdline
}}
298 Set command line to parse after using one of the constructors which don't do it.
302 \helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserstr
}
304 \membersection{wxCmdLineParser::
\destruct{wxCmdLineParser
}}\label{wxcmdlineparserdtor
}
306 \func{}{\destruct{wxCmdLineParser
}}{\void}
308 Frees resources allocated by the object.
310 {\bf NB:
} destructor is not virtual, don't use this class polymorphically.
312 \membersection{wxCmdLineParser::SetSwitchChars
}\label{wxcmdlineparsersetswitchchars
}
314 \func{void
}{SetSwitchChars
}{\param{const wxString\&
}{switchChars
}}
316 {\it switchChars
} contains all characters with which an option or switch may
317 start. Default is
{\tt "-"
} for Unix,
{\tt "-/"
} for Windows.
319 \membersection{wxCmdLineParser::EnableLongOptions
}\label{wxcmdlineparserenablelongoptions
}
321 \func{void
}{EnableLongOptions
}{\param{bool
}{enable = TRUE
}}
323 Enable or disable support for the long options.
325 As long options are not (yet) POSIX-compliant, this option allows to disable
330 \helpref{Customization
}{wxcmdlineparsercustomization
}
332 \membersection{wxCmdLineParser::DisableLongOptions
}\label{wxcmdlineparserdisablelongoptions
}
334 \func{void
}{DisableLongOptions
}{\void}
336 Ientical to
\helpref{EnableLongOptions(FALSE)
}{wxcmdlineparserenablelongoptions
}.
338 \membersection{wxCmdLineParser::SetLogo
}\label{wxcmdlineparsersetlogo
}
340 \func{void
}{SetLogo
}{\param{const wxString\&
}{logo
}}
342 {\it logo
} is some extra text which will be shown by
343 \helpref{Usage
}{wxcmdlineparserusage
} method.
345 \membersection{wxCmdLineParser::SetDesc
}\label{wxcmdlineparsersetdesc
}
347 \func{void
}{SetDesc
}{\param{const wxCmdLineEntryDesc*
}{desc
}}
349 Construct the command line description
351 Take the command line description from the wxCMD
\_LINE\_NONE terminated table.
356 static const wxCmdLineEntryDesc cmdLineDesc
[] =
358 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose"
},
359 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet"
},
361 { wxCMD_LINE_OPTION, "o", "output", "output file"
},
362 { wxCMD_LINE_OPTION, "i", "input", "input dir"
},
363 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER
},
364 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE
},
366 { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE
},
371 wxCmdLineParser parser;
373 parser.SetDesc(cmdLineDesc);
376 \membersection{wxCmdLineParser::AddSwitch
}\label{wxcmdlineparseraddswitch
}
378 \func{void
}{AddSwitch
}{\param{const wxString\&
}{name
},
\param{const wxString\&
}{lng = wxEmptyString
},
\param{const wxString\&
}{desc = wxEmptyString
},
\param{int
}{flags =
0}}
380 Add a switch
{\it name
} with an optional long name
{\it lng
} (no long name if it
381 is empty, which is default), description
{\it desc
} and flags
{\it flags
} to the
382 command line description.
384 \membersection{wxCmdLineParser::AddOption
}\label{wxcmdlineparseraddoption
}
386 \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}}
388 Add an option
{\it name
} with an optional long name
{\it lng
} (no long name if
389 it is empty, which is default) taking a value of the given type (string by
390 default) to the command line description.
392 \membersection{wxCmdLineParser::AddParam
}\label{wxcmdlineparseraddparam
}
394 \func{void
}{AddParam
}{\param{const wxString\&
}{desc = wxEmptyString
},
\param{wxCmdLineParamType
}{type = wxCMD
\_LINE\_VAL\_STRING},
\param{int
}{flags =
0}}
396 Add a parameter of the given
{\it type
} to the command line description.
398 \membersection{wxCmdLineParser::Parse
}\label{wxcmdlineparserparse
}
400 \func{int
}{Parse
}{\void}
402 Parse the command line, return $
0$ if ok, $-
1$ if
{\tt "-h"
} or
{\tt "--help"
}
403 option was encountered and the help message was given or a positive value if a
404 syntax error occured.
406 \membersection{wxCmdLineParser::Usage
}\label{wxcmdlineparserusage
}
408 \func{void
}{Usage
}{\void}
410 Give the standard usage message describing all program options. It will use the
411 options and parameters descriptions specified earlier, so the resulting message
412 will not be helpful to the user unless the descriptions were indeed specified.
416 \helpref{SetLogo
}{wxcmdlineparsersetlogo
}
418 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfoundswitch
}
420 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
}}
422 Returns TRUE if the given switch was found, FALSE otherwise.
424 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfoundstringoption
}
426 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
},
\param{wxString*
}{value
}}
428 Returns TRUE if an option taking a string value was found and stores the
429 value in the provided pointer (which should not be NULL).
431 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfoundintoption
}
433 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
},
\param{long*
}{value
}}
435 Returns TRUE if an option taking an integer value was found and stores
436 the value in the provided pointer (which should not be NULL).
438 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfounddateoption
}
440 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
},
\param{wxDateTime*
}{value
}}
442 Returns TRUE if an option taking a date value was found and stores the
443 value in the provided pointer (which should not be NULL).
445 \membersection{wxCmdLineParser::GetParamCount
}\label{wxcmdlineparsergetparamcount
}
447 \constfunc{size
\_t}{GetParamCount
}{\void}
449 Returns the number of parameters found. This function makes sense mostly if you
450 had used
{\tt wxCMD
\_LINE\_PARAM\_MULTIPLE} flag.
452 \membersection{wxCmdLineParser::GetParam
}\label{wxcmdlineparsergetparam
}
454 \constfunc{wxString
}{GetParam
}{\param{size
\_t }{n =
0u}}
456 Returns the value of Nth parameter (as string only for now).
460 \helpref{GetParamCount
}{wxcmdlineparsergetparamcount
}