aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/programoptions.h
blob: be21e800673147ff20b85a85d8947cda3ad35f24 (plain) (blame)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class vespalib::ProgramOptions
 * \ingroup util
 *
 * \brief Utility class for easy parsing of program options.
 *
 * This class makes it easy to parse program options, and to write a decent
 * syntax page.
 *
 * Just call addOption to register options, and call parseOptions to do the
 * parsing. There's also a function for writing the syntax page, such that this
 * is automatically updated.
 *
 * Stuff to come later:
 *
 * Support for arguments (So you can do stuff like ./myprog file.txt, and not
 *                        only stuff like ./myprog -f file.txt)
 * Setting min and max values for numbers.
 * Support for multiargument options.
 * Automatic man page writing.
 */

#pragma once

#include <vespa/vespalib/util/exception.h>
#include <map>
#include <set>
#include <vector>
#include <memory>

namespace vespalib {

VESPA_DEFINE_EXCEPTION(InvalidCommandLineArgumentsException, Exception);

struct ProgramOptions {
    /** Utility class used by command line configurable utility. */
    class LifetimeToken {
        ProgramOptions& o;
    public:
        using UP = std::unique_ptr<LifetimeToken>;
        LifetimeToken(ProgramOptions& op) : o(op) {}
        ~LifetimeToken() { o.clear(); }
    };
    /**
     * Utility class used to deletage stuff to be configured into multiple
     * units.
     */
    struct Configurable {
        virtual ~Configurable() {}
        /**
         * Called on configurables to have it register its options.
         * Unit must hang onto lifetimetoken until command line parsing have
         * completed. Lifetimetoken should be deleted before stuff registered
         * to be configured is deleted.
         */
        virtual void registerCommandLineOptions(ProgramOptions&, LifetimeToken::UP) = 0;

        /**
         * Called after command line parsing is complete, in order for
         * components to ensure validity of options and throw exceptionse on
         * failures.
         */
        virtual void finalizeOptions() = 0;
    };

    struct OptionParser;

    int _argc;
    const char* const* _argv;
    std::vector<std::shared_ptr<OptionParser> > _options;
    std::map<std::string, std::shared_ptr<OptionParser> > _optionMap;
    std::set<std::shared_ptr<OptionParser> > _setOptions;
    std::vector<std::shared_ptr<OptionParser> > _arguments;
    std::string _syntaxMessage;
    uint32_t _maxLeftColumnSize;
    bool _defaultsSet;
    std::vector<Configurable*> _configurables;

    ProgramOptions(const ProgramOptions&);
    ProgramOptions& operator=(const ProgramOptions&);

public:
    /**
     * If using empty constructor, setCommandLineArguments() must be called
     * before parse() and writeSyntaxPage().
     */
    ProgramOptions();
    ProgramOptions(int argc, const char* const* argv);
    virtual ~ProgramOptions();

    void addConfigurable(Configurable& c) {
        _configurables.push_back(&c);
        c.registerCommandLineOptions(
                *this, LifetimeToken::UP(new LifetimeToken(*this)));
    }

    void setCommandLineArguments(int argc, const char* const* argv);

    /**
     * In bool case, add an optional option that will be true if used.
     * In all other cases, adds a required option as there are no default.
     * Parsing will fail if required option is not set.
     */
    template<typename Type>
    OptionParser& addOption(const std::string& optionNameList,
                   Type& value,
                   const std::string& description);

    /** Add an optional option. Default value will be used if not set. */
    template<typename Type>
    OptionParser& addOption(const std::string& optionNameList,
                   Type& value,
                   const Type& defaultValue,
                   const std::string& description);

    template<typename Type>
    OptionParser& addArgument(const std::string& optionNameList,
                              Type& value,
                              const std::string& description);

    template<typename Type>
    OptionParser& addArgument(const std::string& optionNameList,
                              Type& value,
                              const Type& defaultValue,
                              const std::string& description);

    template<typename Type>
    OptionParser& addListArgument(const std::string& optionNameList,
                                  std::vector<Type>& value,
                                  const std::string& description);

    OptionParser& getOptionParser(const std::string& id);
    OptionParser& getArgumentParser(uint32_t argIndex);

    void addHiddenIdentifiers(const std::string& optionNameList);
    void setArgumentTypeName(const std::string& type, uint32_t index = 0);

    void addOptionHeader(const std::string& description);

    void setSyntaxPageMaxLeftColumnSize(uint32_t cols)
        { _maxLeftColumnSize = cols; }

    /**
     * Parses the command line arguments. Enable vespa debug logging if you want
     * to see details.
     *
     * @throws InvalidCommandLineArgumentsException on any failures.
     */
    void parse();

    /** Writes a syntax page to fit an 80 column screen. */
    void writeSyntaxPage(std::ostream& out, bool showDefaults = true);

    /** Sets some textual description added to syntax page. */
    void setSyntaxMessage(const std::string& msg);

    /**
     * Can be used after having added all the options to initialize all the
     * parameters to default values. Useful if you want to set defaults first,
     * override defaults with config of some kind, and then parse, such that
     * command line parameters override config.
     */
    void setDefaults() { setDefaults(false); }

    /**
     * Useful to clear out all options before shutdown if this class outlives
     * a class defining options.
     */
    void clear();

private:
    void parseOption(const std::string& id, OptionParser&, uint32_t& argPos);
    void parseArgument(OptionParser& opt, uint32_t& pos);
    OptionParser& addOption(std::shared_ptr<OptionParser> && opt);
    OptionParser& addArgument(std::shared_ptr<OptionParser> arg);
    void setDefaults(bool failUnsetRequired);

    struct OptionHeader;
    template<typename Number> struct NumberOptionParser;
    struct BoolOptionParser;
    struct FlagOptionParser;
    struct StringOptionParser;
    struct MapOptionParser;
    template<typename T> struct ListOptionParser;

};

// ----------------------------------------------------------------------------

// Implementation of templates and inner classes
// Not a part of the public interface

template<typename T> const char* getTypeName();
template<> inline const char* getTypeName<int32_t>() { return "int"; }
template<> inline const char* getTypeName<uint32_t>() { return "uint"; }
template<> inline const char* getTypeName<int64_t>() { return "long"; }
template<> inline const char* getTypeName<uint64_t>() { return "ulong"; }
template<> inline const char* getTypeName<float>() { return "float"; }
template<> inline const char* getTypeName<double>() { return "double"; }

struct ProgramOptions::OptionParser {
    using UP = std::unique_ptr<OptionParser>;
    using SP = std::shared_ptr<OptionParser>;

    std::vector<std::string> _names;
    std::vector<std::string> _hiddenNames;
    uint32_t _argCount;
    std::vector<std::string> _argTypes;
    bool _hasDefault;
    bool _invalidDefault;
    std::string _defaultString;
    std::string _description;

    OptionParser(const std::string& nameList, uint32_t argCount,
                 const std::string& desc);
    OptionParser(const std::string& nameList, uint32_t argCount,
                 const std::string& defString, const std::string& desc);
    virtual ~OptionParser();

    virtual bool isRequired() const { return !_hasDefault; }
    virtual void set(const std::vector<std::string>& arguments) = 0;
    virtual void setDefault() = 0;
    virtual void setInvalidDefault();
    virtual std::string getArgType(uint32_t /* index */) const { return "val"; }
    std::string getOptSyntaxString() const;
    std::string getArgName() const {
        std::string name = _names[0];
        for (uint32_t i=1; i<_names.size(); ++i) name += " " + _names[i];
        return name;
    }
    virtual bool isHeader() const { return false; }
    virtual bool hideFromSyntaxPage() const
        { return !isHeader() && _names.empty(); }
};

struct ProgramOptions::OptionHeader : public OptionParser {
    OptionHeader(const std::string& desc) : OptionParser("", 0, desc) {}
    void set(const std::vector<std::string>&) override {}
    void setDefault() override {}
    bool isHeader() const override { return true; }
};

template<typename Number>
struct ProgramOptions::NumberOptionParser : public OptionParser {
    Number& _number;
    Number _defaultValue;

    std::string getStringValue(Number n);

    NumberOptionParser(const std::string& nameList, Number& number,
            const std::string& description)
        : OptionParser(nameList, 1, description),
          _number(number),
          _defaultValue(number)
    {
    }

    NumberOptionParser(const std::string& nameList, Number& number,
            const Number& defValue, const std::string& desc)
        : OptionParser(nameList, 1, getStringValue(defValue), desc),
          _number(number),
          _defaultValue(defValue)
    {}
    ~NumberOptionParser() override;

    void set(const std::vector<std::string>& arguments) override;
    void setDefault() override { _number = _defaultValue; }
    std::string getArgType(uint32_t /* index */) const override {
        return getTypeName<Number>();
    }
};

template<typename Number>
ProgramOptions::NumberOptionParser<Number>::~NumberOptionParser() = default;

struct ProgramOptions::BoolOptionParser : public OptionParser {
    bool& _value;
    bool _defaultValue;

    BoolOptionParser(const std::string& nameList, bool& value, const std::string& description);
    void set(const std::vector<std::string>&) override { _value = true; }
    void setDefault() override { _value = false; }
};

struct ProgramOptions::FlagOptionParser : public OptionParser {
    bool& _value;
    bool _unsetValue;

    FlagOptionParser(const std::string& nameList, bool& value, const std::string& description);
    FlagOptionParser(const std::string& nameList, bool& value, const bool& unsetValue, const std::string& description);
    ~FlagOptionParser() override;
    void set(const std::vector<std::string>&) override { _value = !_unsetValue; }
    void setDefault() override { _value = _unsetValue; }
};


struct ProgramOptions::StringOptionParser : public OptionParser {
    std::string& _value;
    std::string _defaultValue;

    StringOptionParser(const std::string& nameList, std::string& value, const std::string& description);
    StringOptionParser(const std::string& nameList, std::string& value,
                       const std::string& defVal, const std::string& desc);
    ~StringOptionParser() override;

    void set(const std::vector<std::string>& arguments) override { _value = arguments[0]; }
    void setDefault() override { _value = _defaultValue; }
    std::string getArgType(uint32_t /* index */) const override { return "string"; }
};

struct ProgramOptions::MapOptionParser : public OptionParser {
    using MapType = std::map<std::string, std::string>;
    std::map<std::string, std::string>& _value;

    MapOptionParser(const std::string& nameList,
                    std::map<std::string, std::string>& value,
                    const std::string& description);

    void set(const std::vector<std::string>& arguments) override {
        _value[arguments[0]] = arguments[1];
    }

    std::string getArgType(uint32_t /* index */) const override { return "string"; }

    // Default of map is just an empty map.
    void setDefault() override { _value.clear(); }
};

template<typename T>
struct ProgramOptions::ListOptionParser : public OptionParser {
    std::vector<T>& _value;
    T _singleValue;
    OptionParser::UP _entryParser;

    ListOptionParser(const std::string& nameList,
                     std::vector<T>& value,
                     const std::string& description)
        : OptionParser(nameList, 0, description),
          _value(value)
    {
    }

    T& getSingleValue() { return _singleValue; }

    void setEntryParser(OptionParser::UP entryParser) {
        _entryParser = std::move(entryParser);
    }
    bool isRequired() const override { return false; }
    void set(const std::vector<std::string>& arguments) override {
        for (uint32_t i=0; i<arguments.size(); ++i) {
            std::vector<std::string> v;
            v.push_back(arguments[i]);
            _entryParser->set(v);
            _value.push_back(_singleValue);
        }
    }
    void setDefault() override {
        _value.clear();
    }
    std::string getArgType(uint32_t index) const override {
        return _entryParser->getArgType(index) + "[]";
    }
};

} // vespalib