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