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 license
11 ///////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_CMDLINE_H_
14 #define _WX_CMDLINE_H_
17 #pragma interface "cmdline.h"
20 #include "wx/string.h"
22 class WXDLLEXPORT wxDateTime
;
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
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
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
38 // an option value or parameter may be a string (the most common case), a
40 enum wxCmdLineParamType
42 wxCMD_LINE_VAL_STRING
, // should be 0 (default)
43 wxCMD_LINE_VAL_NUMBER
,
48 // for constructing the cmd line description using Init()
49 enum wxCmdLineEntryType
54 wxCMD_LINE_NONE
// to terminate the list
57 // ----------------------------------------------------------------------------
58 // wxCmdLineEntryDesc is a description of one command line
59 // switch/option/parameter
60 // ----------------------------------------------------------------------------
62 struct wxCmdLineEntryDesc
64 wxCmdLineEntryType kind
;
65 const wxChar
*shortName
;
66 const wxChar
*longName
;
67 const wxChar
*description
;
68 wxCmdLineParamType type
;
72 // ----------------------------------------------------------------------------
73 // wxCmdLineParser is a class for parsing command line.
75 // It has the following features:
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, ...)
82 // To use it you should:
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)
87 // 4. use GetXXX() to retrieve the parsed info
88 // ----------------------------------------------------------------------------
90 class WXDLLEXPORT wxCmdLineParser
93 // ctors and initializers
94 // ----------------------
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
); }
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
); }
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
);
114 // not virtual, don't use this class polymorphically
117 // set different parser options
118 // ----------------------------
120 // by default, '-' is switch char under Unix, '-' or '/' under Win:
121 // switchChars contains all characters with which an option or switch may
123 void SetSwitchChars(const wxString
& switchChars
);
125 // long options are not POSIX-compliant, this option allows to disable them
126 void EnableLongOptions(bool enable
= TRUE
);
127 void DisableLongOptions() { EnableLongOptions(FALSE
); }
129 // extra text may be shown by Usage() method if set by this function
130 void SetLogo(const wxString
& logo
);
132 // construct the cmd line description
133 // ----------------------------------
135 // take the cmd line description from the wxCMD_LINE_NONE terminated table
136 void SetDesc(const wxCmdLineEntryDesc
*desc
);
138 // a switch: i.e. an option without value
139 void AddSwitch(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
140 const wxString
& desc
= wxEmptyString
,
143 // an option taking a value of the given type
144 void AddOption(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
145 const wxString
& desc
= wxEmptyString
,
146 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
150 void AddParam(const wxString
& desc
= wxEmptyString
,
151 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
157 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
158 // was encountered and the help message was given or a positive value if a
159 // syntax error occured
162 // give the usage message describing all program options
165 // get the command line arguments
166 // ------------------------------
168 // returns TRUE if the given switch was found
169 bool Found(const wxString
& name
) const;
171 // returns TRUE if an option taking a string value was found and stores the
172 // value in the provided pointer
173 bool Found(const wxString
& name
, wxString
*value
) const;
175 // returns TRUE if an option taking an integer value was found and stores
176 // the value in the provided pointer
177 bool Found(const wxString
& name
, long *value
) const;
179 // returns TRUE if an option taking a date value was found and stores the
180 // value in the provided pointer
181 bool Found(const wxString
& name
, wxDateTime
*value
) const;
183 // gets the number of parameters found
184 size_t GetParamCount() const;
186 // gets the value of Nth parameter (as string only for now)
187 wxString
GetParam(size_t n
= 0u) const;
190 // common part of all ctors
193 struct wxCmdLineParserData
*m_data
;
196 #endif // _WX_CMDLINE_H_