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_
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "cmdline.h"
22 #if wxUSE_CMDLINE_PARSER
24 #include "wx/string.h"
25 #include "wx/arrstr.h"
27 class WXDLLIMPEXP_BASE wxDateTime
;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
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
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
40 wxCMD_LINE_OPTION_HELP
= 0x08, // this option is a help request
41 wxCMD_LINE_NEEDS_SEPARATOR
= 0x10 // must have sep before the value
44 // an option value or parameter may be a string (the most common case), a
46 enum wxCmdLineParamType
48 wxCMD_LINE_VAL_STRING
, // should be 0 (default)
49 wxCMD_LINE_VAL_NUMBER
,
54 // for constructing the cmd line description using Init()
55 enum wxCmdLineEntryType
60 wxCMD_LINE_NONE
// to terminate the list
63 // ----------------------------------------------------------------------------
64 // wxCmdLineEntryDesc is a description of one command line
65 // switch/option/parameter
66 // ----------------------------------------------------------------------------
68 struct wxCmdLineEntryDesc
70 wxCmdLineEntryType kind
;
71 const wxChar
*shortName
;
72 const wxChar
*longName
;
73 const wxChar
*description
;
74 wxCmdLineParamType type
;
78 // ----------------------------------------------------------------------------
79 // wxCmdLineParser is a class for parsing command line.
81 // It has the following features:
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, ...)
88 // To use it you should:
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)
93 // 4. use GetXXX() to retrieve the parsed info
94 // ----------------------------------------------------------------------------
96 class WXDLLIMPEXP_BASE wxCmdLineParser
99 // ctors and initializers
100 // ----------------------
102 // default ctor or ctor giving the cmd line in either Unix or Win form
103 wxCmdLineParser() { Init(); }
104 wxCmdLineParser(int argc
, wxChar
**argv
) { Init(); SetCmdLine(argc
, argv
); }
105 wxCmdLineParser(const wxString
& cmdline
) { Init(); SetCmdLine(cmdline
); }
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
); }
111 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, int argc
, wxChar
**argv
)
112 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
113 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, const wxString
& cmdline
)
114 { Init(); SetCmdLine(cmdline
); SetDesc(desc
); }
116 // set cmd line to parse after using one of the ctors which don't do it
117 void SetCmdLine(int argc
, wxChar
**argv
);
118 void SetCmdLine(const wxString
& cmdline
);
120 // not virtual, don't use this class polymorphically
123 // set different parser options
124 // ----------------------------
126 // by default, '-' is switch char under Unix, '-' or '/' under Win:
127 // switchChars contains all characters with which an option or switch may
129 void SetSwitchChars(const wxString
& switchChars
);
131 // long options are not POSIX-compliant, this option allows to disable them
132 void EnableLongOptions(bool enable
= TRUE
);
133 void DisableLongOptions() { EnableLongOptions(FALSE
); }
135 bool AreLongOptionsEnabled();
137 // extra text may be shown by Usage() method if set by this function
138 void SetLogo(const wxString
& logo
);
140 // construct the cmd line description
141 // ----------------------------------
143 // take the cmd line description from the wxCMD_LINE_NONE terminated table
144 void SetDesc(const wxCmdLineEntryDesc
*desc
);
146 // a switch: i.e. an option without value
147 void AddSwitch(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
148 const wxString
& desc
= wxEmptyString
,
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
,
158 void AddParam(const wxString
& desc
= wxEmptyString
,
159 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
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
169 // if showUsage is true, Usage() is called in case of syntax error or if
170 // help was requested
171 int Parse(bool showUsage
= TRUE
);
173 // give the usage message describing all program options
176 // get the command line arguments
177 // ------------------------------
179 // returns TRUE if the given switch was found
180 bool Found(const wxString
& name
) const;
182 // returns TRUE if an option taking a string value was found and stores the
183 // value in the provided pointer
184 bool Found(const wxString
& name
, wxString
*value
) const;
186 // returns TRUE if an option taking an integer value was found and stores
187 // the value in the provided pointer
188 bool Found(const wxString
& name
, long *value
) const;
191 // returns TRUE if an option taking a date value was found and stores the
192 // value in the provided pointer
193 bool Found(const wxString
& name
, wxDateTime
*value
) const;
194 #endif // wxUSE_DATETIME
196 // gets the number of parameters found
197 size_t GetParamCount() const;
199 // gets the value of Nth parameter (as string only for now)
200 wxString
GetParam(size_t n
= 0u) const;
202 // Resets switches and options
205 // break down the command line in arguments
206 static wxArrayString
ConvertStringToArgs(const wxChar
*cmdline
);
210 wxString
GetUsageString();
212 // common part of all ctors
215 struct wxCmdLineParserData
*m_data
;
217 DECLARE_NO_COPY_CLASS(wxCmdLineParser
)
220 #else // !wxUSE_CMDLINE_PARSER
222 // this function is always available (even if !wxUSE_CMDLINE_PARSER) because it
223 // is used by wxWin itself under Windows
224 class WXDLLIMPEXP_BASE wxCmdLineParser
227 static wxArrayString
ConvertStringToArgs(const wxChar
*cmdline
);
230 #endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
232 #endif // _WX_CMDLINE_H_