Coverage Report

Created: 2024-03-18 06:28

/src/wxwidgets/include/wx/cmdline.h
Line
Count
Source (jump to first uncovered line)
1
///////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/cmdline.h
3
// Purpose:     wxCmdLineParser and related classes for parsing the command
4
//              line options
5
// Author:      Vadim Zeitlin
6
// Created:     04.01.00
7
// Copyright:   (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8
// Licence:     wxWindows licence
9
///////////////////////////////////////////////////////////////////////////////
10
11
#ifndef _WX_CMDLINE_H_
12
#define _WX_CMDLINE_H_
13
14
#include "wx/defs.h"
15
16
#include "wx/string.h"
17
#include "wx/arrstr.h"
18
#include "wx/cmdargs.h"
19
20
// determines ConvertStringToArgs() behaviour
21
enum wxCmdLineSplitType
22
{
23
    wxCMD_LINE_SPLIT_DOS,
24
    wxCMD_LINE_SPLIT_UNIX
25
};
26
27
#if wxUSE_CMDLINE_PARSER
28
29
class WXDLLIMPEXP_FWD_BASE wxCmdLineParser;
30
class WXDLLIMPEXP_FWD_BASE wxDateTime;
31
32
// ----------------------------------------------------------------------------
33
// constants
34
// ----------------------------------------------------------------------------
35
36
// by default, options are optional (sic) and each call to AddParam() allows
37
// one more parameter - this may be changed by giving non-default flags to it
38
enum wxCmdLineEntryFlags
39
{
40
    wxCMD_LINE_OPTION_MANDATORY = 0x01, // this option must be given
41
    wxCMD_LINE_PARAM_OPTIONAL   = 0x02, // the parameter may be omitted
42
    wxCMD_LINE_PARAM_MULTIPLE   = 0x04, // the parameter may be repeated
43
    wxCMD_LINE_OPTION_HELP      = 0x08, // this option is a help request
44
    wxCMD_LINE_NEEDS_SEPARATOR  = 0x10, // must have sep before the value
45
    wxCMD_LINE_SWITCH_NEGATABLE = 0x20, // this switch can be negated (e.g. /S-)
46
    wxCMD_LINE_HIDDEN           = 0x40  // this switch is not listed by Usage()
47
};
48
49
// an option value or parameter may be a string (the most common case), a
50
// number or a date
51
enum wxCmdLineParamType
52
{
53
    wxCMD_LINE_VAL_STRING,  // should be 0 (default)
54
    wxCMD_LINE_VAL_NUMBER,
55
    wxCMD_LINE_VAL_DATE,
56
    wxCMD_LINE_VAL_DOUBLE,
57
    wxCMD_LINE_VAL_NONE
58
};
59
60
// for constructing the cmd line description using Init()
61
enum wxCmdLineEntryType
62
{
63
    wxCMD_LINE_SWITCH,
64
    wxCMD_LINE_OPTION,
65
    wxCMD_LINE_PARAM,
66
    wxCMD_LINE_USAGE_TEXT,
67
    wxCMD_LINE_NONE         // to terminate the list
68
};
69
70
// Possible return values of wxCmdLineParser::FoundSwitch()
71
enum wxCmdLineSwitchState
72
{
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.
76
};
77
78
// ----------------------------------------------------------------------------
79
// wxCmdLineEntryDesc is a description of one command line
80
// switch/option/parameter
81
// ----------------------------------------------------------------------------
82
83
struct wxCmdLineEntryDesc
84
{
85
    wxCmdLineEntryType kind;
86
    const char *shortName;
87
    const char *longName;
88
    const char *description;
89
    wxCmdLineParamType type;
90
    int flags;
91
};
92
93
// the list of wxCmdLineEntryDesc objects should be terminated with this one
94
#define wxCMD_LINE_DESC_END \
95
0
        { wxCMD_LINE_NONE, nullptr, nullptr, nullptr, wxCMD_LINE_VAL_NONE, 0x0 }
96
97
// ----------------------------------------------------------------------------
98
// wxCmdLineArg contains the value for one command line argument
99
// ----------------------------------------------------------------------------
100
101
class WXDLLIMPEXP_BASE wxCmdLineArg
102
{
103
public:
104
0
    virtual ~wxCmdLineArg() = default;
105
106
    virtual double GetDoubleVal() const = 0;
107
    virtual long GetLongVal() const = 0;
108
    virtual const wxString& GetStrVal() const = 0;
109
#if wxUSE_DATETIME
110
    virtual const wxDateTime& GetDateVal() const = 0;
111
#endif // wxUSE_DATETIME
112
113
    virtual bool IsNegated() const = 0;
114
115
    virtual wxCmdLineEntryType GetKind() const = 0;
116
    virtual wxString GetShortName() const = 0;
117
    virtual wxString GetLongName() const = 0;
118
    virtual wxCmdLineParamType GetType() const = 0;
119
};
120
121
// ----------------------------------------------------------------------------
122
// wxCmdLineArgs is a container of command line arguments actually parsed and
123
// allows enumerating them using the standard iterator-based approach.
124
// ----------------------------------------------------------------------------
125
126
class WXDLLIMPEXP_BASE wxCmdLineArgs
127
{
128
public:
129
    class WXDLLIMPEXP_BASE const_iterator
130
    {
131
    public:
132
        typedef int difference_type;
133
        typedef wxCmdLineArg value_type;
134
        typedef const wxCmdLineArg* pointer;
135
        typedef const wxCmdLineArg& reference;
136
        typedef std::bidirectional_iterator_tag iterator_category;
137
138
0
        const_iterator() : m_parser(nullptr), m_index(0) {}
139
        reference operator *() const;
140
        pointer operator ->() const;
141
        const_iterator &operator ++ ();
142
        const_iterator operator ++ (int);
143
        const_iterator &operator -- ();
144
        const_iterator operator -- (int);
145
146
0
        bool operator == (const const_iterator &other) const {
147
0
            return m_parser==other.m_parser && m_index==other.m_index;
148
0
        }
149
0
        bool operator != (const const_iterator &other) const {
150
0
            return !operator==(other);
151
0
        }
152
153
    private:
154
        const_iterator (const wxCmdLineParser& parser, size_t index)
155
0
            : m_parser(&parser), m_index(index) {
156
0
        }
157
158
        const wxCmdLineParser* m_parser;
159
        size_t m_index;
160
161
        friend class wxCmdLineArgs;
162
    };
163
164
0
    wxCmdLineArgs (const wxCmdLineParser& parser) : m_parser(parser) {}
165
166
0
    const_iterator begin() const { return const_iterator(m_parser, 0); }
167
0
    const_iterator end() const { return const_iterator(m_parser, size()); }
168
169
    size_t size() const;
170
171
private:
172
    const wxCmdLineParser& m_parser;
173
    wxDECLARE_NO_ASSIGN_DEF_COPY(wxCmdLineArgs);
174
};
175
176
// ----------------------------------------------------------------------------
177
// wxCmdLineParser is a class for parsing command line.
178
//
179
// It has the following features:
180
//
181
// 1. distinguishes options, switches and parameters; allows option grouping
182
// 2. allows both short and long options
183
// 3. automatically generates the usage message from the cmd line description
184
// 4. does type checks on the options values (number, date, ...)
185
//
186
// To use it you should:
187
//
188
// 1. construct it giving it the cmd line to parse and optionally its desc
189
// 2. construct the cmd line description using AddXXX() if not done in (1)
190
// 3. call Parse()
191
// 4. use GetXXX() to retrieve the parsed info
192
// ----------------------------------------------------------------------------
193
194
class WXDLLIMPEXP_BASE wxCmdLineParser
195
{
196
public:
197
    // ctors and initializers
198
    // ----------------------
199
200
    // default ctor or ctor giving the cmd line in either Unix or Win form
201
0
    wxCmdLineParser() { Init(); }
202
0
    wxCmdLineParser(int argc, char **argv) { Init(); SetCmdLine(argc, argv); }
203
0
    wxCmdLineParser(int argc, wxChar **argv) { Init(); SetCmdLine(argc, argv); }
204
    wxCmdLineParser(int argc, const wxCmdLineArgsArray& argv)
205
0
        { Init(); SetCmdLine(argc, argv); }
206
0
    wxCmdLineParser(const wxString& cmdline) { Init(); SetCmdLine(cmdline); }
207
208
    // the same as above, but also gives the cmd line description - otherwise,
209
    // use AddXXX() later
210
    wxCmdLineParser(const wxCmdLineEntryDesc *desc)
211
0
        { Init(); SetDesc(desc); }
212
    wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, char **argv)
213
0
        { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
214
    wxCmdLineParser(const wxCmdLineEntryDesc *desc, int argc, wxChar **argv)
215
0
        { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
216
    wxCmdLineParser(const wxCmdLineEntryDesc *desc,
217
                    int argc,
218
                    const wxCmdLineArgsArray& argv)
219
0
        { Init(); SetCmdLine(argc, argv); SetDesc(desc); }
220
    wxCmdLineParser(const wxCmdLineEntryDesc *desc, const wxString& cmdline)
221
0
        { Init(); SetCmdLine(cmdline); SetDesc(desc); }
222
223
    // set cmd line to parse after using one of the ctors which don't do it
224
    void SetCmdLine(int argc, char **argv);
225
    void SetCmdLine(int argc, wxChar **argv);
226
    void SetCmdLine(int argc, const wxCmdLineArgsArray& argv);
227
    void SetCmdLine(const wxString& cmdline);
228
229
    // not virtual, don't use this class polymorphically
230
    ~wxCmdLineParser();
231
232
    // set different parser options
233
    // ----------------------------
234
235
    // by default, '-' is switch char under Unix, '-' or '/' under Win:
236
    // switchChars contains all characters with which an option or switch may
237
    // start
238
    void SetSwitchChars(const wxString& switchChars);
239
240
    // long options are not POSIX-compliant, this option allows to disable them
241
    void EnableLongOptions(bool enable = true);
242
0
    void DisableLongOptions() { EnableLongOptions(false); }
243
244
    bool AreLongOptionsEnabled() const;
245
246
    // extra text may be shown by Usage() method if set by this function
247
    void SetLogo(const wxString& logo);
248
249
    // construct the cmd line description
250
    // ----------------------------------
251
252
    // take the cmd line description from the wxCMD_LINE_NONE terminated table
253
    void SetDesc(const wxCmdLineEntryDesc *desc);
254
255
    // a switch: i.e. an option without value
256
    void AddSwitch(const wxString& name, const wxString& lng = wxEmptyString,
257
                   const wxString& desc = wxEmptyString,
258
                   int flags = 0);
259
    void AddLongSwitch(const wxString& lng,
260
                       const wxString& desc = wxEmptyString,
261
                       int flags = 0)
262
0
    {
263
0
        AddSwitch(wxString(), lng, desc, flags);
264
0
    }
265
266
    // an option taking a value of the given type
267
    void AddOption(const wxString& name, const wxString& lng = wxEmptyString,
268
                   const wxString& desc = wxEmptyString,
269
                   wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
270
                   int flags = 0);
271
    void AddLongOption(const wxString& lng,
272
                       const wxString& desc = wxEmptyString,
273
                       wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
274
                       int flags = 0)
275
0
    {
276
0
        AddOption(wxString(), lng, desc, type, flags);
277
0
    }
278
279
    // a parameter
280
    void AddParam(const wxString& desc = wxEmptyString,
281
                  wxCmdLineParamType type = wxCMD_LINE_VAL_STRING,
282
                  int flags = 0);
283
284
    // add an explanatory text to be shown to the user in help
285
    void AddUsageText(const wxString& text);
286
287
    // actions
288
    // -------
289
290
    // parse the command line, return 0 if ok, -1 if "-h" or "--help" option
291
    // was encountered and the help message was given or a positive value if a
292
    // syntax error occurred
293
    //
294
    // if showUsage is true, Usage() is called in case of syntax error or if
295
    // help was requested
296
    int Parse(bool showUsage = true);
297
298
    // give the usage message describing all program options
299
    void Usage() const;
300
301
    // return the usage string, call Usage() to directly show it to the user
302
    wxString GetUsageString() const;
303
304
    // get the command line arguments
305
    // ------------------------------
306
307
    // returns true if the given switch was found
308
    bool Found(const wxString& name) const;
309
310
    // Returns wxCMD_SWITCH_NOT_FOUND if the switch was not found at all,
311
    // wxCMD_SWITCH_ON if it was found in normal state and wxCMD_SWITCH_OFF if
312
    // it was found but negated (i.e. followed by "-", this can only happen for
313
    // the switches with wxCMD_LINE_SWITCH_NEGATABLE flag).
314
    wxCmdLineSwitchState FoundSwitch(const wxString& name) const;
315
316
    // returns true if an option taking a string value was found and stores the
317
    // value in the provided pointer
318
    bool Found(const wxString& name, wxString *value) const;
319
320
    // returns true if an option taking an integer value was found and stores
321
    // the value in the provided pointer
322
    bool Found(const wxString& name, long *value) const;
323
324
    // returns true if an option taking a double value was found and stores
325
    // the value in the provided pointer
326
    bool Found(const wxString& name, double *value) const;
327
328
#if wxUSE_DATETIME
329
    // returns true if an option taking a date value was found and stores the
330
    // value in the provided pointer
331
    bool Found(const wxString& name, wxDateTime *value) const;
332
#endif // wxUSE_DATETIME
333
334
    // gets the number of parameters found
335
    size_t GetParamCount() const;
336
337
    // gets the value of Nth parameter (as string only for now)
338
    wxString GetParam(size_t n = 0u) const;
339
340
    // returns a reference to the container of all command line arguments
341
0
    wxCmdLineArgs GetArguments() const { return wxCmdLineArgs(*this); }
342
343
    // Resets switches and options
344
    void Reset();
345
346
    // break down the command line in arguments
347
    static wxArrayString
348
    ConvertStringToArgs(const wxString& cmdline,
349
                        wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
350
351
private:
352
    // common part of all ctors
353
    void Init();
354
355
    struct wxCmdLineParserData *m_data;
356
357
    friend class wxCmdLineArgs;
358
    friend class wxCmdLineArgs::const_iterator;
359
    wxDECLARE_NO_COPY_CLASS(wxCmdLineParser);
360
};
361
362
#else // !wxUSE_CMDLINE_PARSER
363
364
// this function is always available (even if !wxUSE_CMDLINE_PARSER) because it
365
// is used by wxWin itself under Windows
366
class WXDLLIMPEXP_BASE wxCmdLineParser
367
{
368
public:
369
    static wxArrayString
370
    ConvertStringToArgs(const wxString& cmdline,
371
                        wxCmdLineSplitType type = wxCMD_LINE_SPLIT_DOS);
372
};
373
374
#endif // wxUSE_CMDLINE_PARSER/!wxUSE_CMDLINE_PARSER
375
376
#endif // _WX_CMDLINE_H_