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