summaryrefslogtreecommitdiffstats
path: root/js/src/shell/jsoptparse.h
blob: 90c4c71bdbe5b75b401c589c9ecf2eeb3e955cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef shell_jsoptparse_h
#define shell_jsoptparse_h

#include <stdio.h>

#include "jsalloc.h"
#include "jsutil.h"

#include "js/Vector.h"

namespace js {
namespace cli {

namespace detail {

struct BoolOption;
struct MultiStringOption;
struct ValuedOption;
struct StringOption;
struct IntOption;

enum OptionKind
{
    OptionKindBool,
    OptionKindString,
    OptionKindInt,
    OptionKindMultiString,
    OptionKindInvalid
};

struct Option
{
    const char* longflag;
    const char* help;
    OptionKind  kind;
    char        shortflag;
    bool        terminatesOptions;

    Option(OptionKind kind, char shortflag, const char* longflag, const char* help)
      : longflag(longflag), help(help), kind(kind), shortflag(shortflag), terminatesOptions(false)
    {}

    virtual ~Option() = 0;

    void setTerminatesOptions(bool enabled) { terminatesOptions = enabled; }
    bool getTerminatesOptions() const { return terminatesOptions; }

    virtual bool isValued() const { return false; }

    /* Only some valued options are variadic (like MultiStringOptions). */
    virtual bool isVariadic() const { return false; }

    /*
     * For arguments, the shortflag field is used to indicate whether the
     * argument is optional.
     */
    bool isOptional() { return shortflag; }

    void setFlagInfo(char shortflag, const char* longflag, const char* help) {
        this->shortflag = shortflag;
        this->longflag = longflag;
        this->help = help;
    }

    ValuedOption* asValued();
    const ValuedOption* asValued() const;

#define OPTION_CONVERT_DECL(__cls) \
    bool is##__cls##Option() const; \
    __cls##Option* as##__cls##Option(); \
    const __cls##Option* as##__cls##Option() const;

    OPTION_CONVERT_DECL(Bool)
    OPTION_CONVERT_DECL(String)
    OPTION_CONVERT_DECL(Int)
    OPTION_CONVERT_DECL(MultiString)
};

inline Option::~Option() {}

struct BoolOption : public Option
{
    size_t  argno;
    bool    value;

    BoolOption(char shortflag, const char* longflag, const char* help)
      : Option(OptionKindBool, shortflag, longflag, help), value(false)
    {}

    virtual ~BoolOption() {}
};

struct ValuedOption : public Option
{
    const char* metavar;

    ValuedOption(OptionKind kind, char shortflag, const char* longflag, const char* help,
                 const char* metavar)
      : Option(kind, shortflag, longflag, help), metavar(metavar)
    {}

    virtual ~ValuedOption() = 0;
    virtual bool isValued() const { return true; }
};

inline ValuedOption::~ValuedOption() {}

struct StringOption : public ValuedOption
{
    const char* value;

    StringOption(char shortflag, const char* longflag, const char* help, const char* metavar)
      : ValuedOption(OptionKindString, shortflag, longflag, help, metavar), value(nullptr)
    {}

    virtual ~StringOption() {}
};

struct IntOption : public ValuedOption
{
    int value;

    IntOption(char shortflag, const char* longflag, const char* help, const char* metavar,
              int defaultValue)
      : ValuedOption(OptionKindInt, shortflag, longflag, help, metavar), value(defaultValue)
    {}

    virtual ~IntOption() {}
};

struct StringArg
{
    char*   value;
    size_t  argno;

    StringArg(char* value, size_t argno) : value(value), argno(argno) {}
};

struct MultiStringOption : public ValuedOption
{
    Vector<StringArg, 0, SystemAllocPolicy> strings;

    MultiStringOption(char shortflag, const char* longflag, const char* help, const char* metavar)
      : ValuedOption(OptionKindMultiString, shortflag, longflag, help, metavar)
    {}

    virtual ~MultiStringOption() {}

    virtual bool isVariadic() const { return true; }
};

} /* namespace detail */

class MultiStringRange
{
    typedef detail::StringArg StringArg;
    const StringArg* cur;
    const StringArg* end;

  public:
    explicit MultiStringRange(const StringArg* cur, const StringArg* end)
      : cur(cur), end(end) {
        MOZ_ASSERT(end - cur >= 0);
    }

    bool empty() const { return cur == end; }
    void popFront() { MOZ_ASSERT(!empty()); ++cur; }
    char* front() const { MOZ_ASSERT(!empty()); return cur->value; }
    size_t argno() const { MOZ_ASSERT(!empty()); return cur->argno; }
};

/*
 * Builder for describing a command line interface and parsing the resulting
 * specification.
 *
 * - A multi-option is an option that can appear multiple times and still
 *   parse as valid command line arguments.
 * - An "optional argument" is supported for backwards compatibility with prior
 *   command line interface usage. Once one optional argument has been added,
 *   *only* optional arguments may be added.
 */
class OptionParser
{
  public:
    enum Result
    {
        Okay = 0,
        Fail,       /* As in, allocation fail. */
        ParseError, /* Successfully parsed but with an error. */
        EarlyExit   /* Successfully parsed but exits the program,
                     * for example with --help and --version. */
    };

  private:
    typedef Vector<detail::Option*, 0, SystemAllocPolicy> Options;
    typedef detail::Option Option;
    typedef detail::BoolOption BoolOption;

    Options     options;
    Options     arguments;
    BoolOption  helpOption;
    BoolOption  versionOption;
    const char* usage;
    const char* version;
    const char* descr;
    size_t      descrWidth;
    size_t      helpWidth;
    size_t      nextArgument;

    // If '--' is passed, all remaining arguments should be interpreted as the
    // argument at index 'restArgument'. Defaults to the next unassigned
    // argument.
    int         restArgument;

    static const char prognameMeta[];

    Option* findOption(char shortflag);
    const Option* findOption(char shortflag) const;
    Option* findOption(const char* longflag);
    const Option* findOption(const char* longflag) const;
    int findArgumentIndex(const char* name) const;
    Option* findArgument(const char* name);
    const Option* findArgument(const char* name) const;

    Result error(const char* fmt, ...) MOZ_FORMAT_PRINTF(2, 3);
    Result extractValue(size_t argc, char** argv, size_t* i, char** value);
    Result handleArg(size_t argc, char** argv, size_t* i, bool* optsAllowed);
    Result handleOption(Option* opt, size_t argc, char** argv, size_t* i, bool* optsAllowed);

  public:
    explicit OptionParser(const char* usage)
      : helpOption('h', "help", "Display help information"),
        versionOption('v', "version", "Display version information and exit"),
        usage(usage), version(nullptr), descr(nullptr), descrWidth(80), helpWidth(80),
        nextArgument(0), restArgument(-1)
    {}

    ~OptionParser();

    Result parseArgs(int argc, char** argv);
    Result printHelp(const char* progname);
    Result printVersion();

    /* Metadata */

    void setVersion(const char* v) { version = v; }
    void setHelpWidth(size_t width) { helpWidth = width; }
    void setDescriptionWidth(size_t width) { descrWidth = width; }
    void setDescription(const char* description) { descr = description; }
    void setHelpOption(char shortflag, const char* longflag, const char* help);
    void setArgTerminatesOptions(const char* name, bool enabled);
    void setArgCapturesRest(const char* name);

    /* Arguments: no further arguments may be added after a variadic argument. */

    bool addOptionalStringArg(const char* name, const char* help);
    bool addOptionalMultiStringArg(const char* name, const char* help);

    const char* getStringArg(const char* name) const;
    MultiStringRange getMultiStringArg(const char* name) const;

    /* Options */

    bool addBoolOption(char shortflag, const char* longflag, const char* help);
    bool addStringOption(char shortflag, const char* longflag, const char* help,
                         const char* metavar);
    bool addIntOption(char shortflag, const char* longflag, const char* help,
                      const char* metavar, int defaultValue);
    bool addMultiStringOption(char shortflag, const char* longflag, const char* help,
                              const char* metavar);
    bool addOptionalVariadicArg(const char* name);

    int getIntOption(char shortflag) const;
    int getIntOption(const char* longflag) const;
    const char* getStringOption(char shortflag) const;
    const char* getStringOption(const char* longflag) const;
    bool getBoolOption(char shortflag) const;
    bool getBoolOption(const char* longflag) const;
    MultiStringRange getMultiStringOption(char shortflag) const;
    MultiStringRange getMultiStringOption(const char* longflag) const;

    /*
     * Return whether the help option was present (and thus help was already
     * displayed during parse_args).
     */
    bool getHelpOption() const;
};

} /* namespace cli */
} /* namespace js */

#endif /* shell_jsoptparse_h */