]> git.saurik.com Git - wxWidgets.git/blame - include/wx/cmdline.h
Compilation fix for non-MSW: don't use ProcessCommand().
[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
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
a4761b4c
VZ
22// determines ConvertStringToArgs() behaviour
23enum wxCmdLineSplitType
24{
25 wxCMD_LINE_SPLIT_DOS,
26 wxCMD_LINE_SPLIT_UNIX
27};
28
ee0f5531
WS
29#if wxUSE_CMDLINE_PARSER
30
b5dbe15d 31class WXDLLIMPEXP_FWD_BASE wxDateTime;
9f83044f
VZ
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
7bdb010f 39enum wxCmdLineEntryFlags
9f83044f
VZ
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
f6bcfd97 44 wxCMD_LINE_OPTION_HELP = 0x08, // this option is a help request
494a19d6 45 wxCMD_LINE_NEEDS_SEPARATOR = 0x10 // must have sep before the value
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
69// ----------------------------------------------------------------------------
70// wxCmdLineEntryDesc is a description of one command line
71// switch/option/parameter
72// ----------------------------------------------------------------------------
73
74struct wxCmdLineEntryDesc
75{
76 wxCmdLineEntryType kind;
0d5ab92f
VZ
77 const char *shortName;
78 const char *longName;
79 const char *description;
9f83044f
VZ
80 wxCmdLineParamType type;
81 int flags;
82};
83
0d5ab92f
VZ
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
9f83044f
VZ
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
bddd7a8d 106class WXDLLIMPEXP_BASE wxCmdLineParser
9f83044f
VZ
107{
108public:
109 // ctors and initializers
110 // ----------------------
111
112 // default ctor or ctor giving the cmd line in either Unix or Win form
113 wxCmdLineParser() { Init(); }
ff1ce997
VZ
114 wxCmdLineParser(int argc, char **argv) { Init(); SetCmdLine(argc, argv); }
115#if wxUSE_UNICODE
55b2b0d8 116 wxCmdLineParser(int argc, wxChar **argv) { Init(); SetCmdLine(argc, argv); }
541ea80f
VZ
117 wxCmdLineParser(int argc, const wxCmdLineArgsArray& argv)
118 { Init(); SetCmdLine(argc, argv); }
ff1ce997 119#endif // wxUSE_UNICODE
9f83044f
VZ
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); }
ff1ce997
VZ
126 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, char **argv)
127 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
128#if wxUSE_UNICODE
55b2b0d8 129 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv)
9f83044f 130 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
541ea80f
VZ
131 wxCmdLineParser(const wxCmdLineEntryDesc *desc,
132 int argc,
133 const wxCmdLineArgsArray& argv)
134 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
ff1ce997 135#endif // wxUSE_UNICODE
9f83044f
VZ
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
ff1ce997
VZ
140 void SetCmdLine(int argc, char **argv);
141#if wxUSE_UNICODE
55b2b0d8 142 void SetCmdLine(int argc, wxChar **argv);
541ea80f 143 void SetCmdLine(int argc, const wxCmdLineArgsArray& argv);
ff1ce997 144#endif // wxUSE_UNICODE
9f83044f
VZ
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
68379eaf
WS
159 void EnableLongOptions(bool enable = true);
160 void DisableLongOptions() { EnableLongOptions(false); }
9f83044f 161
779288b4 162 bool AreLongOptionsEnabled() const;
250b589f 163
e612f101
VZ
164 // extra text may be shown by Usage() method if set by this function
165 void SetLogo(const wxString& logo);
166
9f83044f
VZ
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
e559d790
VZ
189 // add an explanatory text to be shown to the user in help
190 void AddUsageText(const wxString& text);
191
9f83044f
VZ
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
3103e8a9 197 // syntax error occurred
be03c0ec
VZ
198 //
199 // if showUsage is true, Usage() is called in case of syntax error or if
200 // help was requested
68379eaf 201 int Parse(bool showUsage = true);
9f83044f
VZ
202
203 // give the usage message describing all program options
779288b4 204 void Usage() const;
9f83044f 205
16b627b0
VZ
206 // return the usage string, call Usage() to directly show it to the user
207 wxString GetUsageString() const;
208
9f83044f
VZ
209 // get the command line arguments
210 // ------------------------------
211
68379eaf 212 // returns true if the given switch was found
9f83044f
VZ
213 bool Found(const wxString& name) const;
214
68379eaf 215 // returns true if an option taking a string value was found and stores the
9f83044f
VZ
216 // value in the provided pointer
217 bool Found(const wxString& name, wxString *value) const;
218
68379eaf 219 // returns true if an option taking an integer value was found and stores
9f83044f
VZ
220 // the value in the provided pointer
221 bool Found(const wxString& name, long *value) const;
222
b1859b1a
VZ
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
e2b87f38 227#if wxUSE_DATETIME
68379eaf 228 // returns true if an option taking a date value was found and stores the
9f83044f
VZ
229 // value in the provided pointer
230 bool Found(const wxString& name, wxDateTime *value) const;
e2b87f38 231#endif // wxUSE_DATETIME
9f83044f
VZ
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)
e612f101 237 wxString GetParam(size_t n = 0u) const;
9f83044f 238
31f6de22
VZ
239 // Resets switches and options
240 void Reset();
241
242 // break down the command line in arguments
a4761b4c
VZ
243 static wxArrayString
244 ConvertStringToArgs(const wxString& cmdline,
245 wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
07d09af8 246
9f83044f
VZ
247private:
248 // common part of all ctors
249 void Init();
250
251 struct wxCmdLineParserData *m_data;
22f3361e 252
c0c133e1 253 wxDECLARE_NO_COPY_CLASS(wxCmdLineParser);
9f83044f
VZ
254};
255
31f6de22
VZ
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
bddd7a8d 260class WXDLLIMPEXP_BASE wxCmdLineParser
31f6de22
VZ
261{
262public:
a4761b4c
VZ
263 static wxArrayString
264 ConvertStringToArgs(const wxString& cmdline,
265 wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
31f6de22
VZ
266};
267
268#endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
2407b388 269
9f83044f 270#endif // _WX_CMDLINE_H_