]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdline.h
Block new frames being created in wxWebViewIE, bringing it into line with the other...
[wxWidgets.git] / include / wx / cmdline.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/cmdline.h
3 // Purpose: wxCmdLineParser and related classes for parsing the command
4 // line options
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 04.01.00
8 // RCS-ID: $Id$
9 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_CMDLINE_H_
14 #define _WX_CMDLINE_H_
15
16 #include "wx/defs.h"
17
18 #include "wx/string.h"
19 #include "wx/arrstr.h"
20 #include "wx/cmdargs.h"
21
22 // determines ConvertStringToArgs() behaviour
23 enum wxCmdLineSplitType
24 {
25 wxCMD_LINE_SPLIT_DOS,
26 wxCMD_LINE_SPLIT_UNIX
27 };
28
29 #if wxUSE_CMDLINE_PARSER
30
31 class WXDLLIMPEXP_FWD_BASE wxDateTime;
32
33 // ----------------------------------------------------------------------------
34 // constants
35 // ----------------------------------------------------------------------------
36
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
39 enum wxCmdLineEntryFlags
40 {
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
46 wxCMD_LINE_SWITCH_NEGATABLE = 0x20 // this switch can be negated (e.g. /S-)
47 };
48
49 // an option value or parameter may be a string (the most common case), a
50 // number or a date
51 enum wxCmdLineParamType
52 {
53 wxCMD_LINE_VAL_STRING, // should be 0 (default)
54 wxCMD_LINE_VAL_NUMBER,
55 wxCMD_LINE_VAL_DATE,
56 wxCMD_LINE_VAL_DOUBLE,
57 wxCMD_LINE_VAL_NONE
58 };
59
60 // for constructing the cmd line description using Init()
61 enum wxCmdLineEntryType
62 {
63 wxCMD_LINE_SWITCH,
64 wxCMD_LINE_OPTION,
65 wxCMD_LINE_PARAM,
66 wxCMD_LINE_USAGE_TEXT,
67 wxCMD_LINE_NONE // to terminate the list
68 };
69
70 // Possible return values of wxCmdLineParser::FoundSwitch()
71 enum wxCmdLineSwitchState
72 {
73 wxCMD_SWITCH_OFF = -1, // Found but turned off/negated.
74 wxCMD_SWITCH_NOT_FOUND, // Not found at all.
75 wxCMD_SWITCH_ON // Found in normal state.
76 };
77
78 // ----------------------------------------------------------------------------
79 // wxCmdLineEntryDesc is a description of one command line
80 // switch/option/parameter
81 // ----------------------------------------------------------------------------
82
83 struct wxCmdLineEntryDesc
84 {
85 wxCmdLineEntryType kind;
86 const char *shortName;
87 const char *longName;
88 const char *description;
89 wxCmdLineParamType type;
90 int flags;
91 };
92
93 // the list of wxCmdLineEntryDesc objects should be terminated with this one
94 #define wxCMD_LINE_DESC_END \
95 { wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0x0 }
96
97 // ----------------------------------------------------------------------------
98 // wxCmdLineParser is a class for parsing command line.
99 //
100 // It has the following features:
101 //
102 // 1. distinguishes options, switches and parameters; allows option grouping
103 // 2. allows both short and long options
104 // 3. automatically generates the usage message from the cmd line description
105 // 4. does type checks on the options values (number, date, ...)
106 //
107 // To use it you should:
108 //
109 // 1. construct it giving it the cmd line to parse and optionally its desc
110 // 2. construct the cmd line description using AddXXX() if not done in (1)
111 // 3. call Parse()
112 // 4. use GetXXX() to retrieve the parsed info
113 // ----------------------------------------------------------------------------
114
115 class WXDLLIMPEXP_BASE wxCmdLineParser
116 {
117 public:
118 // ctors and initializers
119 // ----------------------
120
121 // default ctor or ctor giving the cmd line in either Unix or Win form
122 wxCmdLineParser() { Init(); }
123 wxCmdLineParser(int argc, char **argv) { Init(); SetCmdLine(argc, argv); }
124 #if wxUSE_UNICODE
125 wxCmdLineParser(int argc, wxChar **argv) { Init(); SetCmdLine(argc, argv); }
126 wxCmdLineParser(int argc, const wxCmdLineArgsArray& argv)
127 { Init(); SetCmdLine(argc, argv); }
128 #endif // wxUSE_UNICODE
129 wxCmdLineParser(const wxString& cmdline) { Init(); SetCmdLine(cmdline); }
130
131 // the same as above, but also gives the cmd line description - otherwise,
132 // use AddXXX() later
133 wxCmdLineParser(const wxCmdLineEntryDesc *desc)
134 { Init(); SetDesc(desc); }
135 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, char **argv)
136 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
137 #if wxUSE_UNICODE
138 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv)
139 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
140 wxCmdLineParser(const wxCmdLineEntryDesc *desc,
141 int argc,
142 const wxCmdLineArgsArray& argv)
143 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
144 #endif // wxUSE_UNICODE
145 wxCmdLineParser(const wxCmdLineEntryDesc *desc, const wxString& cmdline)
146 { Init(); SetCmdLine(cmdline); SetDesc(desc); }
147
148 // set cmd line to parse after using one of the ctors which don't do it
149 void SetCmdLine(int argc, char **argv);
150 #if wxUSE_UNICODE
151 void SetCmdLine(int argc, wxChar **argv);
152 void SetCmdLine(int argc, const wxCmdLineArgsArray& argv);
153 #endif // wxUSE_UNICODE
154 void SetCmdLine(const wxString& cmdline);
155
156 // not virtual, don't use this class polymorphically
157 ~wxCmdLineParser();
158
159 // set different parser options
160 // ----------------------------
161
162 // by default, '-' is switch char under Unix, '-' or '/' under Win:
163 // switchChars contains all characters with which an option or switch may
164 // start
165 void SetSwitchChars(const wxString& switchChars);
166
167 // long options are not POSIX-compliant, this option allows to disable them
168 void EnableLongOptions(bool enable = true);
169 void DisableLongOptions() { EnableLongOptions(false); }
170
171 bool AreLongOptionsEnabled() const;
172
173 // extra text may be shown by Usage() method if set by this function
174 void SetLogo(const wxString& logo);
175
176 // construct the cmd line description
177 // ----------------------------------
178
179 // take the cmd line description from the wxCMD_LINE_NONE terminated table
180 void SetDesc(const wxCmdLineEntryDesc *desc);
181
182 // a switch: i.e. an option without value
183 void AddSwitch(const wxString& name, const wxString& lng = wxEmptyString,
184 const wxString& desc = wxEmptyString,
185 int flags = 0);
186
187 // an option taking a value of the given type
188 void AddOption(const wxString& name, const wxString& lng = wxEmptyString,
189 const wxString& desc = wxEmptyString,
190 wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
191 int flags = 0);
192
193 // a parameter
194 void AddParam(const wxString& desc = wxEmptyString,
195 wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
196 int flags = 0);
197
198 // add an explanatory text to be shown to the user in help
199 void AddUsageText(const wxString& text);
200
201 // actions
202 // -------
203
204 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
205 // was encountered and the help message was given or a positive value if a
206 // syntax error occurred
207 //
208 // if showUsage is true, Usage() is called in case of syntax error or if
209 // help was requested
210 int Parse(bool showUsage = true);
211
212 // give the usage message describing all program options
213 void Usage() const;
214
215 // return the usage string, call Usage() to directly show it to the user
216 wxString GetUsageString() const;
217
218 // get the command line arguments
219 // ------------------------------
220
221 // returns true if the given switch was found
222 bool Found(const wxString& name) const;
223
224 // Returns wxCMD_SWITCH_NOT_FOUND if the switch was not found at all,
225 // wxCMD_SWITCH_ON if it was found in normal state and wxCMD_SWITCH_OFF if
226 // it was found but negated (i.e. followed by "-", this can only happen for
227 // the switches with wxCMD_LINE_SWITCH_NEGATABLE flag).
228 wxCmdLineSwitchState FoundSwitch(const wxString& name) const;
229
230 // returns true if an option taking a string value was found and stores the
231 // value in the provided pointer
232 bool Found(const wxString& name, wxString *value) const;
233
234 // returns true if an option taking an integer value was found and stores
235 // the value in the provided pointer
236 bool Found(const wxString& name, long *value) const;
237
238 // returns true if an option taking a double value was found and stores
239 // the value in the provided pointer
240 bool Found(const wxString& name, double *value) const;
241
242 #if wxUSE_DATETIME
243 // returns true if an option taking a date value was found and stores the
244 // value in the provided pointer
245 bool Found(const wxString& name, wxDateTime *value) const;
246 #endif // wxUSE_DATETIME
247
248 // gets the number of parameters found
249 size_t GetParamCount() const;
250
251 // gets the value of Nth parameter (as string only for now)
252 wxString GetParam(size_t n = 0u) const;
253
254 // Resets switches and options
255 void Reset();
256
257 // break down the command line in arguments
258 static wxArrayString
259 ConvertStringToArgs(const wxString& cmdline,
260 wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
261
262 private:
263 // common part of all ctors
264 void Init();
265
266 struct wxCmdLineParserData *m_data;
267
268 wxDECLARE_NO_COPY_CLASS(wxCmdLineParser);
269 };
270
271 #else // !wxUSE_CMDLINE_PARSER
272
273 // this function is always available (even if !wxUSE_CMDLINE_PARSER) because it
274 // is used by wxWin itself under Windows
275 class WXDLLIMPEXP_BASE wxCmdLineParser
276 {
277 public:
278 static wxArrayString
279 ConvertStringToArgs(const wxString& cmdline,
280 wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
281 };
282
283 #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
284
285 #endif // _WX_CMDLINE_H_