]> git.saurik.com Git - wxWidgets.git/blame - include/wx/cmdline.h
cleanup mac
[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
ee0f5531
WS
22#if wxUSE_CMDLINE_PARSER
23
b5dbe15d 24class 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
32enum
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
43enum 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()
52enum 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
65struct wxCmdLineEntryDesc
66{
67 wxCmdLineEntryType kind;
0d5ab92f
VZ
68 const char *shortName;
69 const char *longName;
70 const char *description;
9f83044f
VZ
71 wxCmdLineParamType type;
72 int flags;
73};
74
0d5ab92f
VZ
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
9f83044f
VZ
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
bddd7a8d 97class WXDLLIMPEXP_BASE wxCmdLineParser
9f83044f
VZ
98{
99public:
100 // ctors and initializers
101 // ----------------------
102
103 // default ctor or ctor giving the cmd line in either Unix or Win form
104 wxCmdLineParser() { Init(); }
ff1ce997
VZ
105 wxCmdLineParser(int argc, char **argv) { Init(); SetCmdLine(argc, argv); }
106#if wxUSE_UNICODE
55b2b0d8 107 wxCmdLineParser(int argc, wxChar **argv) { Init(); SetCmdLine(argc, argv); }
541ea80f
VZ
108 wxCmdLineParser(int argc, const wxCmdLineArgsArray& argv)
109 { Init(); SetCmdLine(argc, argv); }
ff1ce997 110#endif // wxUSE_UNICODE
9f83044f
VZ
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); }
ff1ce997
VZ
117 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, char **argv)
118 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
119#if wxUSE_UNICODE
55b2b0d8 120 wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv)
9f83044f 121 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
541ea80f
VZ
122 wxCmdLineParser(const wxCmdLineEntryDesc *desc,
123 int argc,
124 const wxCmdLineArgsArray& argv)
125 { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
ff1ce997 126#endif // wxUSE_UNICODE
9f83044f
VZ
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
ff1ce997
VZ
131 void SetCmdLine(int argc, char **argv);
132#if wxUSE_UNICODE
55b2b0d8 133 void SetCmdLine(int argc, wxChar **argv);
541ea80f 134 void SetCmdLine(int argc, const wxCmdLineArgsArray& argv);
ff1ce997 135#endif // wxUSE_UNICODE
9f83044f
VZ
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
68379eaf
WS
150 void EnableLongOptions(bool enable = true);
151 void DisableLongOptions() { EnableLongOptions(false); }
9f83044f 152
250b589f
JS
153 bool AreLongOptionsEnabled();
154
e612f101
VZ
155 // extra text may be shown by Usage() method if set by this function
156 void SetLogo(const wxString& logo);
157
9f83044f
VZ
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
3103e8a9 185 // syntax error occurred
be03c0ec
VZ
186 //
187 // if showUsage is true, Usage() is called in case of syntax error or if
188 // help was requested
68379eaf 189 int Parse(bool showUsage = true);
9f83044f
VZ
190
191 // give the usage message describing all program options
192 void Usage();
193
194 // get the command line arguments
195 // ------------------------------
196
68379eaf 197 // returns true if the given switch was found
9f83044f
VZ
198 bool Found(const wxString& name) const;
199
68379eaf 200 // returns true if an option taking a string value was found and stores the
9f83044f
VZ
201 // value in the provided pointer
202 bool Found(const wxString& name, wxString *value) const;
203
68379eaf 204 // returns true if an option taking an integer value was found and stores
9f83044f
VZ
205 // the value in the provided pointer
206 bool Found(const wxString& name, long *value) const;
207
e2b87f38 208#if wxUSE_DATETIME
68379eaf 209 // returns true if an option taking a date value was found and stores the
9f83044f
VZ
210 // value in the provided pointer
211 bool Found(const wxString& name, wxDateTime *value) const;
e2b87f38 212#endif // wxUSE_DATETIME
9f83044f
VZ
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)
e612f101 218 wxString GetParam(size_t n = 0u) const;
9f83044f 219
31f6de22
VZ
220 // Resets switches and options
221 void Reset();
222
223 // break down the command line in arguments
86501081 224 static wxArrayString ConvertStringToArgs(const wxString& cmdline);
07d09af8 225
9f83044f 226private:
74698d3a
MB
227 // get usage string
228 wxString GetUsageString();
229
9f83044f
VZ
230 // common part of all ctors
231 void Init();
232
233 struct wxCmdLineParserData *m_data;
22f3361e
VZ
234
235 DECLARE_NO_COPY_CLASS(wxCmdLineParser)
9f83044f
VZ
236};
237
31f6de22
VZ
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
bddd7a8d 242class WXDLLIMPEXP_BASE wxCmdLineParser
31f6de22
VZ
243{
244public:
86501081 245 static wxArrayString ConvertStringToArgs(const wxString& cmdline);
31f6de22
VZ
246};
247
248#endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
2407b388 249
9f83044f 250#endif // _WX_CMDLINE_H_
31f6de22 251