1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCmdLineParser and related classes for parsing the command
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_CMDLINE_H_
14 #define _WX_CMDLINE_H_
18 #include "wx/string.h"
19 #include "wx/arrstr.h"
20 #include "wx/cmdargs.h"
22 // determines ConvertStringToArgs() behaviour
23 enum wxCmdLineSplitType
29 #if wxUSE_CMDLINE_PARSER
31 class WXDLLIMPEXP_FWD_BASE wxDateTime
;
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // by default, options are optional (sic) and each call to AddParam() allows
38 // one more parameter - this may be changed by giving non-default flags to it
39 enum wxCmdLineEntryFlags
41 wxCMD_LINE_OPTION_MANDATORY
= 0x01, // this option must be given
42 wxCMD_LINE_PARAM_OPTIONAL
= 0x02, // the parameter may be omitted
43 wxCMD_LINE_PARAM_MULTIPLE
= 0x04, // the parameter may be repeated
44 wxCMD_LINE_OPTION_HELP
= 0x08, // this option is a help request
45 wxCMD_LINE_NEEDS_SEPARATOR
= 0x10, // must have sep before the value
46 wxCMD_LINE_SWITCH_NEGATABLE
= 0x20 // this switch can be negated (e.g. /S-)
49 // an option value or parameter may be a string (the most common case), a
51 enum wxCmdLineParamType
53 wxCMD_LINE_VAL_STRING
, // should be 0 (default)
54 wxCMD_LINE_VAL_NUMBER
,
56 wxCMD_LINE_VAL_DOUBLE
,
60 // for constructing the cmd line description using Init()
61 enum wxCmdLineEntryType
66 wxCMD_LINE_USAGE_TEXT
,
67 wxCMD_LINE_NONE
// to terminate the list
70 // Possible return values of wxCmdLineParser::FoundSwitch()
71 enum wxCmdLineSwitchState
73 wxCMD_SWITCH_OFF
= -1, // Found but turned off/negated.
74 wxCMD_SWITCH_NOT_FOUND
, // Not found at all.
75 wxCMD_SWITCH_ON
// Found in normal state.
78 // ----------------------------------------------------------------------------
79 // wxCmdLineEntryDesc is a description of one command line
80 // switch/option/parameter
81 // ----------------------------------------------------------------------------
83 struct wxCmdLineEntryDesc
85 wxCmdLineEntryType kind
;
86 const char *shortName
;
88 const char *description
;
89 wxCmdLineParamType type
;
93 // the list of wxCmdLineEntryDesc objects should be terminated with this one
94 #define wxCMD_LINE_DESC_END \
95 { wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0x0 }
97 // ----------------------------------------------------------------------------
98 // wxCmdLineParser is a class for parsing command line.
100 // It has the following features:
102 // 1. distinguishes options, switches and parameters; allows option grouping
103 // 2. allows both short and long options
104 // 3. automatically generates the usage message from the cmd line description
105 // 4. does type checks on the options values (number, date, ...)
107 // To use it you should:
109 // 1. construct it giving it the cmd line to parse and optionally its desc
110 // 2. construct the cmd line description using AddXXX() if not done in (1)
112 // 4. use GetXXX() to retrieve the parsed info
113 // ----------------------------------------------------------------------------
115 class WXDLLIMPEXP_BASE wxCmdLineParser
118 // ctors and initializers
119 // ----------------------
121 // default ctor or ctor giving the cmd line in either Unix or Win form
122 wxCmdLineParser() { Init(); }
123 wxCmdLineParser(int argc
, char **argv
) { Init(); SetCmdLine(argc
, argv
); }
125 wxCmdLineParser(int argc
, wxChar
**argv
) { Init(); SetCmdLine(argc
, argv
); }
126 wxCmdLineParser(int argc
, const wxCmdLineArgsArray
& argv
)
127 { Init(); SetCmdLine(argc
, argv
); }
128 #endif // wxUSE_UNICODE
129 wxCmdLineParser(const wxString
& cmdline
) { Init(); SetCmdLine(cmdline
); }
131 // the same as above, but also gives the cmd line description - otherwise,
132 // use AddXXX() later
133 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
)
134 { Init(); SetDesc(desc
); }
135 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, int argc
, char **argv
)
136 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
138 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, int argc
, wxChar
**argv
)
139 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
140 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
,
142 const wxCmdLineArgsArray
& argv
)
143 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
144 #endif // wxUSE_UNICODE
145 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, const wxString
& cmdline
)
146 { Init(); SetCmdLine(cmdline
); SetDesc(desc
); }
148 // set cmd line to parse after using one of the ctors which don't do it
149 void SetCmdLine(int argc
, char **argv
);
151 void SetCmdLine(int argc
, wxChar
**argv
);
152 void SetCmdLine(int argc
, const wxCmdLineArgsArray
& argv
);
153 #endif // wxUSE_UNICODE
154 void SetCmdLine(const wxString
& cmdline
);
156 // not virtual, don't use this class polymorphically
159 // set different parser options
160 // ----------------------------
162 // by default, '-' is switch char under Unix, '-' or '/' under Win:
163 // switchChars contains all characters with which an option or switch may
165 void SetSwitchChars(const wxString
& switchChars
);
167 // long options are not POSIX-compliant, this option allows to disable them
168 void EnableLongOptions(bool enable
= true);
169 void DisableLongOptions() { EnableLongOptions(false); }
171 bool AreLongOptionsEnabled() const;
173 // extra text may be shown by Usage() method if set by this function
174 void SetLogo(const wxString
& logo
);
176 // construct the cmd line description
177 // ----------------------------------
179 // take the cmd line description from the wxCMD_LINE_NONE terminated table
180 void SetDesc(const wxCmdLineEntryDesc
*desc
);
182 // a switch: i.e. an option without value
183 void AddSwitch(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
184 const wxString
& desc
= wxEmptyString
,
186 void AddLongSwitch(const wxString
& lng
,
187 const wxString
& desc
= wxEmptyString
,
190 AddSwitch(wxString(), lng
, desc
, flags
);
193 // an option taking a value of the given type
194 void AddOption(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
195 const wxString
& desc
= wxEmptyString
,
196 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
198 void AddLongOption(const wxString
& lng
,
199 const wxString
& desc
= wxEmptyString
,
200 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
203 AddOption(wxString(), lng
, desc
, type
, flags
);
207 void AddParam(const wxString
& desc
= wxEmptyString
,
208 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
211 // add an explanatory text to be shown to the user in help
212 void AddUsageText(const wxString
& text
);
217 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
218 // was encountered and the help message was given or a positive value if a
219 // syntax error occurred
221 // if showUsage is true, Usage() is called in case of syntax error or if
222 // help was requested
223 int Parse(bool showUsage
= true);
225 // give the usage message describing all program options
228 // return the usage string, call Usage() to directly show it to the user
229 wxString
GetUsageString() const;
231 // get the command line arguments
232 // ------------------------------
234 // returns true if the given switch was found
235 bool Found(const wxString
& name
) const;
237 // Returns wxCMD_SWITCH_NOT_FOUND if the switch was not found at all,
238 // wxCMD_SWITCH_ON if it was found in normal state and wxCMD_SWITCH_OFF if
239 // it was found but negated (i.e. followed by "-", this can only happen for
240 // the switches with wxCMD_LINE_SWITCH_NEGATABLE flag).
241 wxCmdLineSwitchState
FoundSwitch(const wxString
& name
) const;
243 // returns true if an option taking a string value was found and stores the
244 // value in the provided pointer
245 bool Found(const wxString
& name
, wxString
*value
) const;
247 // returns true if an option taking an integer value was found and stores
248 // the value in the provided pointer
249 bool Found(const wxString
& name
, long *value
) const;
251 // returns true if an option taking a double value was found and stores
252 // the value in the provided pointer
253 bool Found(const wxString
& name
, double *value
) const;
256 // returns true if an option taking a date value was found and stores the
257 // value in the provided pointer
258 bool Found(const wxString
& name
, wxDateTime
*value
) const;
259 #endif // wxUSE_DATETIME
261 // gets the number of parameters found
262 size_t GetParamCount() const;
264 // gets the value of Nth parameter (as string only for now)
265 wxString
GetParam(size_t n
= 0u) const;
267 // Resets switches and options
270 // break down the command line in arguments
272 ConvertStringToArgs(const wxString
& cmdline
,
273 wxCmdLineSplitType type
= wxCMD_LINE_SPLIT_DOS
);
276 // common part of all ctors
279 struct wxCmdLineParserData
*m_data
;
281 wxDECLARE_NO_COPY_CLASS(wxCmdLineParser
);
284 #else // !wxUSE_CMDLINE_PARSER
286 // this function is always available (even if !wxUSE_CMDLINE_PARSER) because it
287 // is used by wxWin itself under Windows
288 class WXDLLIMPEXP_BASE wxCmdLineParser
292 ConvertStringToArgs(const wxString
& cmdline
,
293 wxCmdLineSplitType type
= wxCMD_LINE_SPLIT_DOS
);
296 #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
298 #endif // _WX_CMDLINE_H_