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