1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCmdLineParser and related classes for parsing the command
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_CMDLINE_H_
14 #define _WX_CMDLINE_H_
18 #include "wx/string.h"
19 #include "wx/arrstr.h"
20 #include "wx/cmdargs.h"
22 // determines ConvertStringToArgs() behaviour
23 enum wxCmdLineSplitType
29 #if wxUSE_CMDLINE_PARSER
31 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // by default, options are optional (sic) and each call to AddParam() allows
38 // one more parameter - this may be changed by giving non-default flags to it
41 wxCMD_LINE_OPTION_MANDATORY
= 0x01, // this option must be given
42 wxCMD_LINE_PARAM_OPTIONAL
= 0x02, // the parameter may be omitted
43 wxCMD_LINE_PARAM_MULTIPLE
= 0x04, // the parameter may be repeated
44 wxCMD_LINE_OPTION_HELP
= 0x08, // this option is a help request
45 wxCMD_LINE_NEEDS_SEPARATOR
= 0x10 // must have sep before the value
48 // an option value or parameter may be a string (the most common case), a
50 enum wxCmdLineParamType
52 wxCMD_LINE_VAL_STRING
, // should be 0 (default)
53 wxCMD_LINE_VAL_NUMBER
,
55 wxCMD_LINE_VAL_DOUBLE
,
59 // for constructing the cmd line description using Init()
60 enum wxCmdLineEntryType
65 wxCMD_LINE_USAGE_TEXT
,
66 wxCMD_LINE_NONE
// to terminate the list
69 // ----------------------------------------------------------------------------
70 // wxCmdLineEntryDesc is a description of one command line
71 // switch/option/parameter
72 // ----------------------------------------------------------------------------
74 struct wxCmdLineEntryDesc
76 wxCmdLineEntryType kind
;
77 const char *shortName
;
79 const char *description
;
80 wxCmdLineParamType type
;
84 // the list of wxCmdLineEntryDesc objects should be terminated with this one
85 #define wxCMD_LINE_DESC_END \
86 { wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0x0 }
88 // ----------------------------------------------------------------------------
89 // wxCmdLineParser is a class for parsing command line.
91 // It has the following features:
93 // 1. distinguishes options, switches and parameters; allows option grouping
94 // 2. allows both short and long options
95 // 3. automatically generates the usage message from the cmd line description
96 // 4. does type checks on the options values (number, date, ...)
98 // To use it you should:
100 // 1. construct it giving it the cmd line to parse and optionally its desc
101 // 2. construct the cmd line description using AddXXX() if not done in (1)
103 // 4. use GetXXX() to retrieve the parsed info
104 // ----------------------------------------------------------------------------
106 class WXDLLIMPEXP_BASE wxCmdLineParser
109 // ctors and initializers
110 // ----------------------
112 // default ctor or ctor giving the cmd line in either Unix or Win form
113 wxCmdLineParser() { Init(); }
114 wxCmdLineParser(int argc
, char **argv
) { Init(); SetCmdLine(argc
, argv
); }
116 wxCmdLineParser(int argc
, wxChar
**argv
) { Init(); SetCmdLine(argc
, argv
); }
117 wxCmdLineParser(int argc
, const wxCmdLineArgsArray
& argv
)
118 { Init(); SetCmdLine(argc
, argv
); }
119 #endif // wxUSE_UNICODE
120 wxCmdLineParser(const wxString
& cmdline
) { Init(); SetCmdLine(cmdline
); }
122 // the same as above, but also gives the cmd line description - otherwise,
123 // use AddXXX() later
124 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
)
125 { Init(); SetDesc(desc
); }
126 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, int argc
, char **argv
)
127 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
129 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, int argc
, wxChar
**argv
)
130 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
131 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
,
133 const wxCmdLineArgsArray
& argv
)
134 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
135 #endif // wxUSE_UNICODE
136 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, const wxString
& cmdline
)
137 { Init(); SetCmdLine(cmdline
); SetDesc(desc
); }
139 // set cmd line to parse after using one of the ctors which don't do it
140 void SetCmdLine(int argc
, char **argv
);
142 void SetCmdLine(int argc
, wxChar
**argv
);
143 void SetCmdLine(int argc
, const wxCmdLineArgsArray
& argv
);
144 #endif // wxUSE_UNICODE
145 void SetCmdLine(const wxString
& cmdline
);
147 // not virtual, don't use this class polymorphically
150 // set different parser options
151 // ----------------------------
153 // by default, '-' is switch char under Unix, '-' or '/' under Win:
154 // switchChars contains all characters with which an option or switch may
156 void SetSwitchChars(const wxString
& switchChars
);
158 // long options are not POSIX-compliant, this option allows to disable them
159 void EnableLongOptions(bool enable
= true);
160 void DisableLongOptions() { EnableLongOptions(false); }
162 bool AreLongOptionsEnabled() const;
164 // extra text may be shown by Usage() method if set by this function
165 void SetLogo(const wxString
& logo
);
167 // construct the cmd line description
168 // ----------------------------------
170 // take the cmd line description from the wxCMD_LINE_NONE terminated table
171 void SetDesc(const wxCmdLineEntryDesc
*desc
);
173 // a switch: i.e. an option without value
174 void AddSwitch(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
175 const wxString
& desc
= wxEmptyString
,
178 // an option taking a value of the given type
179 void AddOption(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
180 const wxString
& desc
= wxEmptyString
,
181 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
185 void AddParam(const wxString
& desc
= wxEmptyString
,
186 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
189 // add an explanatory text to be shown to the user in help
190 void AddUsageText(const wxString
& text
);
195 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
196 // was encountered and the help message was given or a positive value if a
197 // syntax error occurred
199 // if showUsage is true, Usage() is called in case of syntax error or if
200 // help was requested
201 int Parse(bool showUsage
= true);
203 // give the usage message describing all program options
206 // return the usage string, call Usage() to directly show it to the user
207 wxString
GetUsageString() const;
209 // get the command line arguments
210 // ------------------------------
212 // returns true if the given switch was found
213 bool Found(const wxString
& name
) const;
215 // returns true if an option taking a string value was found and stores the
216 // value in the provided pointer
217 bool Found(const wxString
& name
, wxString
*value
) const;
219 // returns true if an option taking an integer value was found and stores
220 // the value in the provided pointer
221 bool Found(const wxString
& name
, long *value
) const;
223 // returns true if an option taking a double value was found and stores
224 // the value in the provided pointer
225 bool Found(const wxString
& name
, double *value
) const;
228 // returns true if an option taking a date value was found and stores the
229 // value in the provided pointer
230 bool Found(const wxString
& name
, wxDateTime
*value
) const;
231 #endif // wxUSE_DATETIME
233 // gets the number of parameters found
234 size_t GetParamCount() const;
236 // gets the value of Nth parameter (as string only for now)
237 wxString
GetParam(size_t n
= 0u) const;
239 // Resets switches and options
242 // break down the command line in arguments
244 ConvertStringToArgs(const wxString
& cmdline
,
245 wxCmdLineSplitType type
= wxCMD_LINE_SPLIT_DOS
);
248 // common part of all ctors
251 struct wxCmdLineParserData
*m_data
;
253 DECLARE_NO_COPY_CLASS(wxCmdLineParser
)
256 #else // !wxUSE_CMDLINE_PARSER
258 // this function is always available (even if !wxUSE_CMDLINE_PARSER) because it
259 // is used by wxWin itself under Windows
260 class WXDLLIMPEXP_BASE wxCmdLineParser
264 ConvertStringToArgs(const wxString
& cmdline
,
265 wxCmdLineSplitType type
= wxCMD_LINE_SPLIT_DOS
);
268 #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
270 #endif // _WX_CMDLINE_H_