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