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