]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cmdline.h
added cmdline.h/.cpp to the makefiles
[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 };
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, char **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, char **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, char **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 // construct the cmd line description
130 // ----------------------------------
131
132 // take the cmd line description from the wxCMD_LINE_NONE terminated table
133 void SetDesc(const wxCmdLineEntryDesc *desc);
134
135 // a switch: i.e. an option without value
136 void AddSwitch(const wxString& name, const wxString& lng = wxEmptyString,
137 const wxString& desc = wxEmptyString,
138 int flags = 0);
139
140 // an option taking a value of the given type
141 void AddOption(const wxString& name, const wxString& lng = wxEmptyString,
142 const wxString& desc = wxEmptyString,
143 wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
144 int flags = 0);
145
146 // a parameter
147 void AddParam(const wxString& desc = wxEmptyString,
148 wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
149 int flags = 0);
150
151 // actions
152 // -------
153
154 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
155 // was encountered and the help message was given or a positive value if a
156 // syntax error occured
157 int Parse();
158
159 // give the usage message describing all program options
160 void Usage();
161
162 // get the command line arguments
163 // ------------------------------
164
165 // returns TRUE if the given switch was found
166 bool Found(const wxString& name) const;
167
168 // returns TRUE if an option taking a string value was found and stores the
169 // value in the provided pointer
170 bool Found(const wxString& name, wxString *value) const;
171
172 // returns TRUE if an option taking an integer value was found and stores
173 // the value in the provided pointer
174 bool Found(const wxString& name, long *value) const;
175
176 // returns TRUE if an option taking a date value was found and stores the
177 // value in the provided pointer
178 bool Found(const wxString& name, wxDateTime *value) const;
179
180 // gets the number of parameters found
181 size_t GetParamCount() const;
182
183 // gets the value of Nth parameter (as string only for now)
184 wxString GetParam(size_t n) const;
185
186 private:
187 // common part of all ctors
188 void Init();
189
190 struct wxCmdLineParserData *m_data;
191 };
192
193 #endif // _WX_CMDLINE_H_