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 \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$).
24 To use it you should follow these steps:
26 \item \helpref{construct
}{wxcmdlineparserconstruction
} an object of this class
27 giving 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
33 In the documentation below the following terminology is used:
35 \begin{twocollist
}\itemsep=
0pt
36 \twocolitem{switch
}{This is a boolean option which can be given or not, but
37 which doesn't have any value. We use the word switch to distinguish such boolean
38 options 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
41 unlike a switch. For example,
{\tt -o:filename
} might be an option which allows
42 to specify the name of the output file.
}
43 \twocolitem{parameter
}{This is a required program argument.
}
46 \wxheading{Derived from
}
50 \wxheading{Include files
}
56 The structure wxCmdLineEntryDesc is used to describe the one command
57 line switch, option or parameter. An array of such structures should be passed
58 to
\helpref{SetDesc()
}{wxcmdlineparsersetdesc
}. Also, the meanings of parameters
59 of the
{\tt AddXXX()
} functions are the same as of the corresponding fields in
63 struct wxCmdLineEntryDesc
65 wxCmdLineEntryType kind;
66 const wxChar *shortName;
67 const wxChar *longName;
68 const wxChar *description;
69 wxCmdLineParamType type;
74 The type of a command line entity is in the
{\tt kind
} field and may be one of
75 the following constants:
76 {\small \begin{verbatim
}
77 enum wxCmdLineEntryType
82 wxCMD
\_LINE\_NONE // use this to terminate the list
86 The 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
88 name. Both of these fields are unused for the parameters. Both the short and
89 long option names can contain only letters, digits and the underscores.
91 {\tt description
} is used by the
\helpref{Usage()
}{wxcmdlineparserusage
} method
92 to construct a help message explaining the syntax of the program.
94 The possible values of
{\tt type
} which specifies the type of the value accepted
95 by an option or parameter are:
96 {\small \begin{verbatim
}
97 enum wxCmdLineParamType
99 wxCMD
\_LINE\_VAL\_STRING, // default
100 wxCMD
\_LINE\_VAL\_NUMBER,
101 wxCMD
\_LINE\_VAL\_DATE,
102 wxCMD
\_LINE\_VAL\_NONE
106 Finally, the
{\tt flags
} field is a combination of the following bit masks:
107 {\small \begin{verbatim
}
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
118 Notice that by default (i.e. if flags are just $
0$), options are optional (sic)
119 and each call to
\helpref{AddParam()
}{wxcmdlineparseraddparam
} allows one more
120 parameter - 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
124 variable number of parameters - but it only can be given for the last parameter
125 in the command line description. If you use this flag, you will probably need to
126 use
\helpref{GetParamCount
}{wxcmdlineparsergetparamcount
} to retrieve the number
127 of parameters effectively specified after calling
128 \helpref{Parse
}{wxcmdlineparserparse
}.
130 The last flag
{\tt wxCMD
\_LINE\_NEEDS\_SEPARATOR} can be specified to require a
131 separator (either a colon, an equal sign or white space) between the option
132 name and its value. By default, no separator is required.
136 \helpref{wxApp::argc
}{wxappargc
} and
\helpref{wxApp::argv
}{wxappargv
}\\
139 %%%%%%%%%%%%% Methods by group %%%%%%%%%%%%%
140 \latexignore{\rtfignore{\wxheading{Function groups
}}}
142 \membersection{Construction
}\label{wxcmdlineparserconstruction
}
144 Before
\helpref{Parse
}{wxcmdlineparserparse
} can be called, the command line
145 parser object must have the command line to parse and also the rules saying
146 which switches, options and parameters are valid - this is called command line
147 description in what follows.
149 You have complete freedom of choice as to when specify the required information,
150 the only restriction is that it must be done before calling
151 \helpref{Parse
}{wxcmdlineparserparse
}.
153 To specify the command line to parse you may use either one of constructors
154 accepting it (
\helpref{wxCmdLineParser(argc, argv)
}{wxcmdlineparserwxcmdlineparserargc
} or
155 \helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserdescargc
} usually) or,
156 if you use
\helpref{the default constructor
}{wxcmdlineparserwxcmdlineparserdef
},
157 you can do it later by calling
158 \helpref{SetCmdLine
}{wxcmdlineparsersetcmdlineargc
}.
160 The same holds for command line description: it can be specified either in
161 the constructor (
\helpref{without command line
}{wxcmdlineparserwxcmdlineparserdesc
} or
162 \helpref{together with it
}{wxcmdlineparserwxcmdlineparserdescargc
}) or
163 constructed later using either
\helpref{SetDesc
}{wxcmdlineparsersetdesc
} or
164 combination of
\helpref{AddSwitch
}{wxcmdlineparseraddswitch
},
165 \helpref{AddOption
}{wxcmdlineparseraddoption
} and
166 \helpref{AddParam
}{wxcmdlineparseraddparam
} methods.
168 Using constructors or
\helpref{SetDesc
}{wxcmdlineparsersetdesc
} uses a (usually
169 {\tt const static
}) table containing the command line description. If you want
170 to decide which options to acccept during the run-time, using one of the
171 {\tt AddXXX()
} functions above might be preferable.
173 \membersection{Customization
}\label{wxcmdlineparsercustomization
}
175 wxCmdLineParser has several global options which may be changed by the
176 application. All of the functions described in this section should be called
177 before
\helpref{Parse
}{wxcmdlineparserparse
}.
179 First global option is the support for long (also known as GNU-style) options.
180 The long options are the ones which start with two dashes (
{\tt "--"
}) and look
181 like this:
{\tt --verbose
}, i.e. they generally are complete words and not some
182 abbreviations of them. As long options are used by more and more applications,
183 they are enabled by default, but may be disabled with
184 \helpref{DisableLongOptions
}{wxcmdlineparserdisablelongoptions
}.
186 Another global option is the set of characters which may be used to start an
187 option (otherwise, the word on the command line is assumed to be a parameter).
188 Under Unix,
{\tt '-'
} is always used, but Windows has at least two common
189 choices for this:
{\tt '-'
} and
{\tt '/'
}. Some programs also use
{\tt '+'
}.
190 The default is to use what suits most the current platform, but may be changed
191 with
\helpref{SetSwitchChars
}{wxcmdlineparsersetswitchchars
} method.
193 Finally,
\helpref{SetLogo
}{wxcmdlineparsersetlogo
} can be used to show some
194 application-specific text before the explanation given by
195 \helpref{Usage
}{wxcmdlineparserusage
} function.
197 \membersection{Parsing command line
}\label{wxcmdlineparserparsing
}
199 After the command line description was constructed and the desiredoptions were
200 set, you can finally call
\helpref{Parse
}{wxcmdlineparserparse
} method.
201 It returns $
0$ if the command line was correct and was parsed, $-
1$ if the help
202 option was specified (this is a separate case as, normally, the program will
203 terminate after this) or a positive number if there was an error during the
204 command line parsing.
206 In the latter case, the appropriate error message and usage information are
207 logged by wxCmdLineParser itself using the standard wxWindows logging functions.
209 \membersection{Getting results
}\label{wxcmdlineparsergettingresults
}
211 After calling
\helpref{Parse
}{wxcmdlineparserparse
} (and if it returned $
0$),
212 you may access the results of parsing using one of overloaded
{\tt Found()
}
215 For a simple switch, you will simply call
216 \helpref{Found
}{wxcmdlineparserfoundswitch
} to determine if the switch was given
217 or not, for an option or a parameter, you will call a version of
{\tt Found()
}
218 which 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
220 command line or FALSE if they were not specified.
222 %%%%%%%%%%%%% Methods in alphabetic order %%%%%%%%%%%%%
223 \helponly{\insertatlevel{2}{
229 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdef
}
231 \func{}{wxCmdLineParser
}{\void}
233 Default constructor. You must use
234 \helpref{SetCmdLine
}{wxcmdlineparsersetcmdlineargc
} or
235 \helpref{SetCmdLinelater.
}{wxcmdlineparsersetcmdlinestr
} later.
237 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserargc
}
239 \func{}{wxCmdLineParser
}{\param{int
}{argc
},
\param{char**
}{argv
}}
241 Constructor specifies the command line to parse. This is the traditional
242 (Unix) command line format. The parameters
{\it argc
} and
{\it argv
} have the
243 same meaning as for
{\tt main()
} function.
245 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserstr
}
247 \func{}{wxCmdLineParser
}{\param{const wxString\&
}{cmdline
}}
249 Constructor specifies the command line to parse in Windows format. The parameter
250 {\it cmdline
} has the same meaning as the corresponding parameter of
253 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdesc
}
255 \func{}{wxCmdLineParser
}{\param{const wxCmdLineEntryDesc*
}{desc
}}
257 Same as
\helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserdef
}, but also
258 specifies the
\helpref{command line description
}{wxcmdlineparsersetdesc
}.
260 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdescargc
}
262 \func{}{wxCmdLineParser
}{\param{const wxCmdLineEntryDesc*
}{desc
},
\param{int
}{argc
},
\param{char**
}{argv
}}
264 Same as
\helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserargc
}, but also
265 specifies the
\helpref{command line description
}{wxcmdlineparsersetdesc
}.
267 \membersection{wxCmdLineParser::wxCmdLineParser
}\label{wxcmdlineparserwxcmdlineparserdescstr
}
269 \func{}{wxCmdLineParser
}{\param{const wxCmdLineEntryDesc*
}{desc
},
\param{const wxString\&
}{cmdline
}}
271 Same as
\helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserstr
}, but also
272 specifies the
\helpref{command line description
}{wxcmdlineparsersetdesc
}.
274 \membersection{wxCmdLineParser::SetCmdLine
}\label{wxcmdlineparsersetcmdlineargc
}
276 \func{void
}{SetCmdLine
}{\param{int
}{argc
},
\param{char**
}{argv
}}
278 Set command line to parse after using one of the constructors which don't do it.
282 \helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserargc
}
284 \membersection{wxCmdLineParser::SetCmdLine
}\label{wxcmdlineparsersetcmdlinestr
}
286 \func{void
}{SetCmdLine
}{\param{const wxString\&
}{cmdline
}}
288 Set command line to parse after using one of the constructors which don't do it.
292 \helpref{wxCmdLineParser
}{wxcmdlineparserwxcmdlineparserstr
}
294 \membersection{wxCmdLineParser::
\destruct{wxCmdLineParser
}}\label{wxcmdlineparserdtor
}
296 \func{}{\destruct{wxCmdLineParser
}}{\void}
298 Frees resources allocated by the object.
300 {\bf NB:
} destructor is not virtual, don't use this class polymorphically.
302 \membersection{wxCmdLineParser::SetSwitchChars
}\label{wxcmdlineparsersetswitchchars
}
304 \func{void
}{SetSwitchChars
}{\param{const wxString\&
}{switchChars
}}
306 {\it switchChars
} contains all characters with which an option or switch may
307 start. Default is
{\tt "-"
} for Unix,
{\tt "-/"
} for Windows.
309 \membersection{wxCmdLineParser::EnableLongOptions
}\label{wxcmdlineparserenablelongoptions
}
311 \func{void
}{EnableLongOptions
}{\param{bool
}{enable = TRUE
}}
313 Enable or disable support for the long options.
315 As long options are not (yet) POSIX-compliant, this option allows to disable
320 \helpref{Customization
}{wxcmdlineparsercustomization
}
322 \membersection{wxCmdLineParser::DisableLongOptions
}\label{wxcmdlineparserdisablelongoptions
}
324 \func{void
}{DisableLongOptions
}{\void}
326 Ientical to
\helpref{EnableLongOptions(FALSE)
}{wxcmdlineparserenablelongoptions
}.
328 \membersection{wxCmdLineParser::SetLogo
}\label{wxcmdlineparsersetlogo
}
330 \func{void
}{SetLogo
}{\param{const wxString\&
}{logo
}}
332 {\it logo
} is some extra text which will be shown by
333 \helpref{Usage
}{wxcmdlineparserusage
} method.
335 \membersection{wxCmdLineParser::SetDesc
}\label{wxcmdlineparsersetdesc
}
337 \func{void
}{SetDesc
}{\param{const wxCmdLineEntryDesc*
}{desc
}}
339 Construct the command line description
341 Take the command line description from the wxCMD
\_LINE\_NONE terminated table.
346 static const wxCmdLineEntryDesc cmdLineDesc
[] =
348 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose"
},
349 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet"
},
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
},
356 { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE
},
361 wxCmdLineParser parser;
363 parser.SetDesc(cmdLineDesc);
366 \membersection{wxCmdLineParser::AddSwitch
}\label{wxcmdlineparseraddswitch
}
368 \func{void
}{AddSwitch
}{\param{const wxString\&
}{name
},
\param{const wxString\&
}{lng = wxEmptyString
},
\param{const wxString\&
}{desc = wxEmptyString
},
\param{int
}{flags =
0}}
370 Add a switch
{\it name
} with an optional long name
{\it lng
} (no long name if it
371 is empty, which is default), description
{\it desc
} and flags
{\it flags
} to the
372 command line description.
374 \membersection{wxCmdLineParser::AddOption
}\label{wxcmdlineparseraddoption
}
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}}
378 Add an option
{\it name
} with an optional long name
{\it lng
} (no long name if
379 it is empty, which is default) taking a value of the given type (string by
380 default) to the command line description.
382 \membersection{wxCmdLineParser::AddParam
}\label{wxcmdlineparseraddparam
}
384 \func{void
}{AddParam
}{\param{const wxString\&
}{desc = wxEmptyString
},
\param{wxCmdLineParamType
}{type = wxCMD
\_LINE\_VAL\_STRING},
\param{int
}{flags =
0}}
386 Add a parameter of the given
{\it type
} to the command line description.
388 \membersection{wxCmdLineParser::Parse
}\label{wxcmdlineparserparse
}
390 \func{int
}{Parse
}{\void}
392 Parse the command line, return $
0$ if ok, $-
1$ if
{\tt "-h"
} or
{\tt "--help"
}
393 option was encountered and the help message was given or a positive value if a
394 syntax error occured.
396 \membersection{wxCmdLineParser::Usage
}\label{wxcmdlineparserusage
}
398 \func{void
}{Usage
}{\void}
400 Give the standard usage message describing all program options. It will use the
401 options and parameters descriptions specified earlier, so the resulting message
402 will not be helpful to the user unless the descriptions were indeed specified.
406 \helpref{SetLogo
}{wxcmdlineparsersetlogo
}
408 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfoundswitch
}
410 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
}}
412 Returns TRUE if the given switch was found, FALSE otherwise.
414 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfoundstringoption
}
416 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
},
\param{wxString*
}{value
}}
418 Returns TRUE if an option taking a string value was found and stores the
419 value in the provided pointer (which should not be NULL).
421 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfoundintoption
}
423 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
},
\param{long*
}{value
}}
425 Returns TRUE if an option taking an integer value was found and stores
426 the value in the provided pointer (which should not be NULL).
428 \membersection{wxCmdLineParser::Found
}\label{wxcmdlineparserfounddateoption
}
430 \constfunc{bool
}{Found
}{\param{const wxString\&
}{name
},
\param{wxDateTime*
}{value
}}
432 Returns TRUE if an option taking a date value was found and stores the
433 value in the provided pointer (which should not be NULL).
435 \membersection{wxCmdLineParser::GetParamCount
}\label{wxcmdlineparsergetparamcount
}
437 \constfunc{size
\_t}{GetParamCount
}{\void}
439 Returns the number of parameters found. This function makes sense mostly if you
440 had used
{\tt wxCMD
\_LINE\_PARAM\_MULTIPLE} flag.
442 \membersection{wxCmdLineParser::GetParam
}\label{wxcmdlineparsergetparam
}
444 \constfunc{wxString
}{GetParam
}{\param{size
\_t }{n =
0u}}
446 Returns the value of Nth parameter (as string only for now).
450 \helpref{GetParamCount
}{wxcmdlineparsergetparamcount
}