]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdline.h
Replace wxTINY_CAPTION_{HORIZ,VERT} with a single wxTINY_CAPTION.
[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 };
47
48 // an option value or parameter may be a string (the most common case), a
49 // number or a date
50 enum wxCmdLineParamType
51 {
52 wxCMD_LINE_VAL_STRING, // should be 0 (default)
53 wxCMD_LINE_VAL_NUMBER,
54 wxCMD_LINE_VAL_DATE,
55 wxCMD_LINE_VAL_DOUBLE,
56 wxCMD_LINE_VAL_NONE
57 };
58
59 // for constructing the cmd line description using Init()
60 enum wxCmdLineEntryType
61 {
62 wxCMD_LINE_SWITCH,
63 wxCMD_LINE_OPTION,
64 wxCMD_LINE_PARAM,
65 wxCMD_LINE_USAGE_TEXT,
66 wxCMD_LINE_NONE // to terminate the list
67 };
68
69 // ----------------------------------------------------------------------------
70 // wxCmdLineEntryDesc is a description of one command line
71 // switch/option/parameter
72 // ----------------------------------------------------------------------------
73
74 struct wxCmdLineEntryDesc
75 {
76 wxCmdLineEntryType kind;
77 const char *shortName;
78 const char *longName;
79 const char *description;
80 wxCmdLineParamType type;
81 int flags;
82 };
83
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 }
87
88 // ----------------------------------------------------------------------------
89 // wxCmdLineParser is a class for parsing command line.
90 //
91 // It has the following features:
92 //
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, ...)
97 //
98 // To use it you should:
99 //
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)
102 // 3. call Parse()
103 // 4. use GetXXX() to retrieve the parsed info
104 // ----------------------------------------------------------------------------
105
106 class WXDLLIMPEXP_BASE wxCmdLineParser
107 {
108 public:
109 // ctors and initializers
110 // ----------------------
111
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); }
115 #if wxUSE_UNICODE
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); }
121
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); }
128 #if wxUSE_UNICODE
129 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv)
130 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
131 wxCmdLineParser(const wxCmdLineEntryDesc *desc,
132 int argc,
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); }
138
139 // set cmd line to parse after using one of the ctors which don't do it
140 void SetCmdLine(int argc, char **argv);
141 #if wxUSE_UNICODE
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);
146
147 // not virtual, don't use this class polymorphically
148 ~wxCmdLineParser();
149
150 // set different parser options
151 // ----------------------------
152
153 // by default, '-' is switch char under Unix, '-' or '/' under Win:
154 // switchChars contains all characters with which an option or switch may
155 // start
156 void SetSwitchChars(const wxString& switchChars);
157
158 // long options are not POSIX-compliant, this option allows to disable them
159 void EnableLongOptions(bool enable = true);
160 void DisableLongOptions() { EnableLongOptions(false); }
161
162 bool AreLongOptionsEnabled() const;
163
164 // extra text may be shown by Usage() method if set by this function
165 void SetLogo(const wxString& logo);
166
167 // construct the cmd line description
168 // ----------------------------------
169
170 // take the cmd line description from the wxCMD_LINE_NONE terminated table
171 void SetDesc(const wxCmdLineEntryDesc *desc);
172
173 // a switch: i.e. an option without value
174 void AddSwitch(const wxString& name, const wxString& lng = wxEmptyString,
175 const wxString& desc = wxEmptyString,
176 int flags = 0);
177
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,
182 int flags = 0);
183
184 // a parameter
185 void AddParam(const wxString& desc = wxEmptyString,
186 wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
187 int flags = 0);
188
189 // add an explanatory text to be shown to the user in help
190 void AddUsageText(const wxString& text);
191
192 // actions
193 // -------
194
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
198 //
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);
202
203 // give the usage message describing all program options
204 void Usage() const;
205
206 // return the usage string, call Usage() to directly show it to the user
207 wxString GetUsageString() const;
208
209 // get the command line arguments
210 // ------------------------------
211
212 // returns true if the given switch was found
213 bool Found(const wxString& name) const;
214
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;
218
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;
222
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;
226
227 #if wxUSE_DATETIME
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
232
233 // gets the number of parameters found
234 size_t GetParamCount() const;
235
236 // gets the value of Nth parameter (as string only for now)
237 wxString GetParam(size_t n = 0u) const;
238
239 // Resets switches and options
240 void Reset();
241
242 // break down the command line in arguments
243 static wxArrayString
244 ConvertStringToArgs(const wxString& cmdline,
245 wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
246
247 private:
248 // common part of all ctors
249 void Init();
250
251 struct wxCmdLineParserData *m_data;
252
253 wxDECLARE_NO_COPY_CLASS(wxCmdLineParser);
254 };
255
256 #else // !wxUSE_CMDLINE_PARSER
257
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
261 {
262 public:
263 static wxArrayString
264 ConvertStringToArgs(const wxString& cmdline,
265 wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
266 };
267
268 #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
269
270 #endif // _WX_CMDLINE_H_