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