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 // construct the cmd line description
130 // ----------------------------------
132 // take the cmd line description from the wxCMD_LINE_NONE terminated table
133 void SetDesc(const wxCmdLineEntryDesc
*desc
);
135 // a switch: i.e. an option without value
136 void AddSwitch(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
137 const wxString
& desc
= wxEmptyString
,
140 // an option taking a value of the given type
141 void AddOption(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
142 const wxString
& desc
= wxEmptyString
,
143 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
147 void AddParam(const wxString
& desc
= wxEmptyString
,
148 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
154 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
155 // was encountered and the help message was given or a positive value if a
156 // syntax error occured
159 // give the usage message describing all program options
162 // get the command line arguments
163 // ------------------------------
165 // returns TRUE if the given switch was found
166 bool Found(const wxString
& name
) const;
168 // returns TRUE if an option taking a string value was found and stores the
169 // value in the provided pointer
170 bool Found(const wxString
& name
, wxString
*value
) const;
172 // returns TRUE if an option taking an integer value was found and stores
173 // the value in the provided pointer
174 bool Found(const wxString
& name
, long *value
) const;
176 // returns TRUE if an option taking a date value was found and stores the
177 // value in the provided pointer
178 bool Found(const wxString
& name
, wxDateTime
*value
) const;
180 // gets the number of parameters found
181 size_t GetParamCount() const;
183 // gets the value of Nth parameter (as string only for now)
184 wxString
GetParam(size_t n
) const;
187 // common part of all ctors
190 struct wxCmdLineParserData
*m_data
;
193 #endif // _WX_CMDLINE_H_