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
36 wxCMD_LINE_NEEDS_SEPARATOR
= 0x10 // must have sep before the value
39 // an option value or parameter may be a string (the most common case), a
41 enum wxCmdLineParamType
43 wxCMD_LINE_VAL_STRING
, // should be 0 (default)
44 wxCMD_LINE_VAL_NUMBER
,
49 // for constructing the cmd line description using Init()
50 enum wxCmdLineEntryType
55 wxCMD_LINE_NONE
// to terminate the list
58 // ----------------------------------------------------------------------------
59 // wxCmdLineEntryDesc is a description of one command line
60 // switch/option/parameter
61 // ----------------------------------------------------------------------------
63 struct wxCmdLineEntryDesc
65 wxCmdLineEntryType kind
;
66 const wxChar
*shortName
;
67 const wxChar
*longName
;
68 const wxChar
*description
;
69 wxCmdLineParamType type
;
73 // ----------------------------------------------------------------------------
74 // wxCmdLineParser is a class for parsing command line.
76 // It has the following features:
78 // 1. distinguishes options, switches and parameters; allows option grouping
79 // 2. allows both short and long options
80 // 3. automatically generates the usage message from the cmd line description
81 // 4. does type checks on the options values (number, date, ...)
83 // To use it you should:
85 // 1. construct it giving it the cmd line to parse and optionally its desc
86 // 2. construct the cmd line description using AddXXX() if not done in (1)
88 // 4. use GetXXX() to retrieve the parsed info
89 // ----------------------------------------------------------------------------
91 class WXDLLEXPORT wxCmdLineParser
94 // ctors and initializers
95 // ----------------------
97 // default ctor or ctor giving the cmd line in either Unix or Win form
98 wxCmdLineParser() { Init(); }
99 wxCmdLineParser(int argc
, char **argv
) { Init(); SetCmdLine(argc
, argv
); }
100 wxCmdLineParser(const wxString
& cmdline
) { Init(); SetCmdLine(cmdline
); }
102 // the same as above, but also gives the cmd line description - otherwise,
103 // use AddXXX() later
104 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
)
105 { Init(); SetDesc(desc
); }
106 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, int argc
, char **argv
)
107 { Init(); SetCmdLine(argc
, argv
); SetDesc(desc
); }
108 wxCmdLineParser(const wxCmdLineEntryDesc
*desc
, const wxString
& cmdline
)
109 { Init(); SetCmdLine(cmdline
); SetDesc(desc
); }
111 // set cmd line to parse after using one of the ctors which don't do it
112 void SetCmdLine(int argc
, char **argv
);
113 void SetCmdLine(const wxString
& cmdline
);
115 // not virtual, don't use this class polymorphically
118 // set different parser options
119 // ----------------------------
121 // by default, '-' is switch char under Unix, '-' or '/' under Win:
122 // switchChars contains all characters with which an option or switch may
124 void SetSwitchChars(const wxString
& switchChars
);
126 // long options are not POSIX-compliant, this option allows to disable them
127 void EnableLongOptions(bool enable
= TRUE
);
128 void DisableLongOptions() { EnableLongOptions(FALSE
); }
130 // extra text may be shown by Usage() method if set by this function
131 void SetLogo(const wxString
& logo
);
133 // construct the cmd line description
134 // ----------------------------------
136 // take the cmd line description from the wxCMD_LINE_NONE terminated table
137 void SetDesc(const wxCmdLineEntryDesc
*desc
);
139 // a switch: i.e. an option without value
140 void AddSwitch(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
141 const wxString
& desc
= wxEmptyString
,
144 // an option taking a value of the given type
145 void AddOption(const wxString
& name
, const wxString
& lng
= wxEmptyString
,
146 const wxString
& desc
= wxEmptyString
,
147 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
151 void AddParam(const wxString
& desc
= wxEmptyString
,
152 wxCmdLineParamType type
= wxCMD_LINE_VAL_STRING
,
158 // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
159 // was encountered and the help message was given or a positive value if a
160 // syntax error occured
163 // give the usage message describing all program options
166 // get the command line arguments
167 // ------------------------------
169 // returns TRUE if the given switch was found
170 bool Found(const wxString
& name
) const;
172 // returns TRUE if an option taking a string value was found and stores the
173 // value in the provided pointer
174 bool Found(const wxString
& name
, wxString
*value
) const;
176 // returns TRUE if an option taking an integer value was found and stores
177 // the value in the provided pointer
178 bool Found(const wxString
& name
, long *value
) const;
180 // returns TRUE if an option taking a date value was found and stores the
181 // value in the provided pointer
182 bool Found(const wxString
& name
, wxDateTime
*value
) const;
184 // gets the number of parameters found
185 size_t GetParamCount() const;
187 // gets the value of Nth parameter (as string only for now)
188 wxString
GetParam(size_t n
= 0u) const;
190 // Resets switches and options
194 // common part of all ctors
197 struct wxCmdLineParserData
*m_data
;
200 #endif // _WX_CMDLINE_H_