]>
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, | |
58 | wxCMD_LINE_NONE // to terminate the list | |
59 | }; | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // wxCmdLineEntryDesc is a description of one command line | |
63 | // switch/option/parameter | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
66 | struct wxCmdLineEntryDesc | |
67 | { | |
68 | wxCmdLineEntryType kind; | |
0d5ab92f VZ |
69 | const char *shortName; |
70 | const char *longName; | |
71 | const char *description; | |
9f83044f VZ |
72 | wxCmdLineParamType type; |
73 | int flags; | |
74 | }; | |
75 | ||
0d5ab92f VZ |
76 | // the list of wxCmdLineEntryDesc objects should be terminated with this one |
77 | #define wxCMD_LINE_DESC_END \ | |
78 | { wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0x0 } | |
79 | ||
9f83044f VZ |
80 | // ---------------------------------------------------------------------------- |
81 | // wxCmdLineParser is a class for parsing command line. | |
82 | // | |
83 | // It has the following features: | |
84 | // | |
85 | // 1. distinguishes options, switches and parameters; allows option grouping | |
86 | // 2. allows both short and long options | |
87 | // 3. automatically generates the usage message from the cmd line description | |
88 | // 4. does type checks on the options values (number, date, ...) | |
89 | // | |
90 | // To use it you should: | |
91 | // | |
92 | // 1. construct it giving it the cmd line to parse and optionally its desc | |
93 | // 2. construct the cmd line description using AddXXX() if not done in (1) | |
94 | // 3. call Parse() | |
95 | // 4. use GetXXX() to retrieve the parsed info | |
96 | // ---------------------------------------------------------------------------- | |
97 | ||
bddd7a8d | 98 | class WXDLLIMPEXP_BASE wxCmdLineParser |
9f83044f VZ |
99 | { |
100 | public: | |
101 | // ctors and initializers | |
102 | // ---------------------- | |
103 | ||
104 | // default ctor or ctor giving the cmd line in either Unix or Win form | |
105 | wxCmdLineParser() { Init(); } | |
ff1ce997 VZ |
106 | wxCmdLineParser(int argc, char **argv) { Init(); SetCmdLine(argc, argv); } |
107 | #if wxUSE_UNICODE | |
55b2b0d8 | 108 | wxCmdLineParser(int argc, wxChar **argv) { Init(); SetCmdLine(argc, argv); } |
541ea80f VZ |
109 | wxCmdLineParser(int argc, const wxCmdLineArgsArray& argv) |
110 | { Init(); SetCmdLine(argc, argv); } | |
ff1ce997 | 111 | #endif // wxUSE_UNICODE |
9f83044f VZ |
112 | wxCmdLineParser(const wxString& cmdline) { Init(); SetCmdLine(cmdline); } |
113 | ||
114 | // the same as above, but also gives the cmd line description - otherwise, | |
115 | // use AddXXX() later | |
116 | wxCmdLineParser(const wxCmdLineEntryDesc *desc) | |
117 | { Init(); SetDesc(desc); } | |
ff1ce997 VZ |
118 | wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, char **argv) |
119 | { Init(); SetCmdLine(argc, argv); SetDesc(desc); } | |
120 | #if wxUSE_UNICODE | |
55b2b0d8 | 121 | wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv) |
9f83044f | 122 | { Init(); SetCmdLine(argc, argv); SetDesc(desc); } |
541ea80f VZ |
123 | wxCmdLineParser(const wxCmdLineEntryDesc *desc, |
124 | int argc, | |
125 | const wxCmdLineArgsArray& argv) | |
126 | { Init(); SetCmdLine(argc, argv); SetDesc(desc); } | |
ff1ce997 | 127 | #endif // wxUSE_UNICODE |
9f83044f VZ |
128 | wxCmdLineParser(const wxCmdLineEntryDesc *desc, const wxString& cmdline) |
129 | { Init(); SetCmdLine(cmdline); SetDesc(desc); } | |
130 | ||
131 | // set cmd line to parse after using one of the ctors which don't do it | |
ff1ce997 VZ |
132 | void SetCmdLine(int argc, char **argv); |
133 | #if wxUSE_UNICODE | |
55b2b0d8 | 134 | void SetCmdLine(int argc, wxChar **argv); |
541ea80f | 135 | void SetCmdLine(int argc, const wxCmdLineArgsArray& argv); |
ff1ce997 | 136 | #endif // wxUSE_UNICODE |
9f83044f VZ |
137 | void SetCmdLine(const wxString& cmdline); |
138 | ||
139 | // not virtual, don't use this class polymorphically | |
140 | ~wxCmdLineParser(); | |
141 | ||
142 | // set different parser options | |
143 | // ---------------------------- | |
144 | ||
145 | // by default, '-' is switch char under Unix, '-' or '/' under Win: | |
146 | // switchChars contains all characters with which an option or switch may | |
147 | // start | |
148 | void SetSwitchChars(const wxString& switchChars); | |
149 | ||
150 | // long options are not POSIX-compliant, this option allows to disable them | |
68379eaf WS |
151 | void EnableLongOptions(bool enable = true); |
152 | void DisableLongOptions() { EnableLongOptions(false); } | |
9f83044f | 153 | |
779288b4 | 154 | bool AreLongOptionsEnabled() const; |
250b589f | 155 | |
e612f101 VZ |
156 | // extra text may be shown by Usage() method if set by this function |
157 | void SetLogo(const wxString& logo); | |
158 | ||
9f83044f VZ |
159 | // construct the cmd line description |
160 | // ---------------------------------- | |
161 | ||
162 | // take the cmd line description from the wxCMD_LINE_NONE terminated table | |
163 | void SetDesc(const wxCmdLineEntryDesc *desc); | |
164 | ||
165 | // a switch: i.e. an option without value | |
166 | void AddSwitch(const wxString& name, const wxString& lng = wxEmptyString, | |
167 | const wxString& desc = wxEmptyString, | |
168 | int flags = 0); | |
169 | ||
170 | // an option taking a value of the given type | |
171 | void AddOption(const wxString& name, const wxString& lng = wxEmptyString, | |
172 | const wxString& desc = wxEmptyString, | |
173 | wxCmdLineParamType type = wxCMD_LINE_VAL_STRING, | |
174 | int flags = 0); | |
175 | ||
176 | // a parameter | |
177 | void AddParam(const wxString& desc = wxEmptyString, | |
178 | wxCmdLineParamType type = wxCMD_LINE_VAL_STRING, | |
179 | int flags = 0); | |
180 | ||
181 | // actions | |
182 | // ------- | |
183 | ||
184 | // parse the command line, return 0 if ok, -1 if "-h" or "--help" option | |
185 | // was encountered and the help message was given or a positive value if a | |
3103e8a9 | 186 | // syntax error occurred |
be03c0ec VZ |
187 | // |
188 | // if showUsage is true, Usage() is called in case of syntax error or if | |
189 | // help was requested | |
68379eaf | 190 | int Parse(bool showUsage = true); |
9f83044f VZ |
191 | |
192 | // give the usage message describing all program options | |
779288b4 | 193 | void Usage() const; |
9f83044f VZ |
194 | |
195 | // get the command line arguments | |
196 | // ------------------------------ | |
197 | ||
68379eaf | 198 | // returns true if the given switch was found |
9f83044f VZ |
199 | bool Found(const wxString& name) const; |
200 | ||
68379eaf | 201 | // returns true if an option taking a string value was found and stores the |
9f83044f VZ |
202 | // value in the provided pointer |
203 | bool Found(const wxString& name, wxString *value) const; | |
204 | ||
68379eaf | 205 | // returns true if an option taking an integer value was found and stores |
9f83044f VZ |
206 | // the value in the provided pointer |
207 | bool Found(const wxString& name, long *value) const; | |
208 | ||
b1859b1a VZ |
209 | // returns true if an option taking a double value was found and stores |
210 | // the value in the provided pointer | |
211 | bool Found(const wxString& name, double *value) const; | |
212 | ||
e2b87f38 | 213 | #if wxUSE_DATETIME |
68379eaf | 214 | // returns true if an option taking a date value was found and stores the |
9f83044f VZ |
215 | // value in the provided pointer |
216 | bool Found(const wxString& name, wxDateTime *value) const; | |
e2b87f38 | 217 | #endif // wxUSE_DATETIME |
9f83044f VZ |
218 | |
219 | // gets the number of parameters found | |
220 | size_t GetParamCount() const; | |
221 | ||
222 | // gets the value of Nth parameter (as string only for now) | |
e612f101 | 223 | wxString GetParam(size_t n = 0u) const; |
9f83044f | 224 | |
31f6de22 VZ |
225 | // Resets switches and options |
226 | void Reset(); | |
227 | ||
228 | // break down the command line in arguments | |
86501081 | 229 | static wxArrayString ConvertStringToArgs(const wxString& cmdline); |
07d09af8 | 230 | |
9f83044f | 231 | private: |
74698d3a | 232 | // get usage string |
779288b4 | 233 | wxString GetUsageString() const; |
74698d3a | 234 | |
9f83044f VZ |
235 | // common part of all ctors |
236 | void Init(); | |
237 | ||
238 | struct wxCmdLineParserData *m_data; | |
22f3361e VZ |
239 | |
240 | DECLARE_NO_COPY_CLASS(wxCmdLineParser) | |
9f83044f VZ |
241 | }; |
242 | ||
31f6de22 VZ |
243 | #else // !wxUSE_CMDLINE_PARSER |
244 | ||
245 | // this function is always available (even if !wxUSE_CMDLINE_PARSER) because it | |
246 | // is used by wxWin itself under Windows | |
bddd7a8d | 247 | class WXDLLIMPEXP_BASE wxCmdLineParser |
31f6de22 VZ |
248 | { |
249 | public: | |
86501081 | 250 | static wxArrayString ConvertStringToArgs(const wxString& cmdline); |
31f6de22 VZ |
251 | }; |
252 | ||
253 | #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER | |
2407b388 | 254 | |
9f83044f | 255 | #endif // _WX_CMDLINE_H_ |
31f6de22 | 256 |