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