summaryrefslogtreecommitdiff
path: root/src/controller/cmdline_parser.h
blob: 55b1a387b3442bb79a4a45912dc91d5688a161ab (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//------------------------------------------------------------------------------
// Author: Pavel Karneliuk
// Description: Command-line arguments parser.
// Copyright (c) 2013 EPAM Systems
//------------------------------------------------------------------------------
/*
    This file is part of Nfstrace.

    Nfstrace is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 2 of the License.

    Nfstrace is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Nfstrace.  If not, see <http://www.gnu.org/licenses/>.
*/
//------------------------------------------------------------------------------
#ifndef CMDLINE_PARSER_H
#define CMDLINE_PARSER_H
//------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>

#include <getopt.h>

#include "utils/noncopyable.h"
//------------------------------------------------------------------------------
namespace NST
{
namespace controller
{
namespace cmdline
{
class CLIError final : public std::runtime_error
{
public:
    explicit CLIError(const std::string& msg)
        : std::runtime_error{msg}
    {
    }
};

struct Opt final
{
    // clang-format off
    class Value final : utils::noncopyable
    {
    public:
        explicit Value(const char* const v) : value{v}{}
        Value(Value&& that) : value{that.value}{}


        operator std::string() const { return std::string{value};           }
        const char*  to_cstr() const { return value;                        }
        int           to_int() const { return atoi(value);                  }
        bool         to_bool() const { return strcmp(value, "true") == 0;   }
        bool is(const char* s) const { return strcmp(value, s) == 0;        }

    private:
        const char*const value;
    };

    enum Type { NOA, REQ, MUL };

    const char short_opt;           // a character for short option, can be 0
    const char* const long_opt;     // a string long option, can be nullptr
    const Type type;
    const char* const deflt;        // default value
    const char* const description;
    const char* value_pattern;
    const char* value;
    bool passed;                    // is option parsed

    // clang-format on
};

template <typename CLI>
class CmdlineParser : utils::noncopyable
{
public:
    CmdlineParser()          = default;
    virtual ~CmdlineParser() = default;


    void parse(int argc, char** argv);
    void validate();

    static Opt::Value get(typename CLI::Names name)
    {
        return Opt::Value{CLI::options[name].value};
    }

    static bool is_passed(typename CLI::Names name)
    {
        return CLI::options[name].passed;
    }

    static bool is_default(typename CLI::Names name)
    {
        const Opt& a = CLI::options[name];
        return a.value == a.deflt; // compare pointers
    }

    static void print_usage(std::ostream& out, const char* executable);

private:
    virtual void set_multiple_value(int /*index*/, char* const /*v*/) {}
    void set_value(int index, char* const v)
    {
        Opt& a = CLI::options[index];
        // if option argument specified - set it otherwise
        // set valid default OR "true" for no-args options
        a.value  = v ? v : (a.deflt && a.type != Opt::Type::NOA ? a.deflt : "true");
        a.passed = true;

        if(a.type == Opt::Type::MUL)
        {
            set_multiple_value(index, v);
        }
    }

    static std::string build_name(char short_name, const std::string& long_name)
    {
        if(short_name)
        {
            return {'\'', '-', short_name, '\''};
        }
        return std::string{'\''} + long_name + '\'';
    }

    static int short_opt_index(const char c)
    {
        for(int i = 0; i < CLI::num; ++i)
        {
            if(CLI::options[i].short_opt == c)
            {
                return i;
            }
        }
        return -1;
    }
};

template <typename CLI>
void CmdlineParser<CLI>::parse(int argc, char** argv)
{
    // generate input data for getopt_long()
    option long_opts[CLI::num + 1]; // +1 for NULL-option
    char   short_opts[CLI::num * 2 + 2] = {0};

    short_opts[0] = ':';

    char* short_p = &short_opts[1];
    for(int i = 0; i < CLI::num; ++i)
    {
        const Opt& a = CLI::options[i];

        long_opts[i].name    = a.long_opt;
        long_opts[i].has_arg = (a.type == Opt::Type::NOA) ? no_argument : required_argument;
        long_opts[i].flag    = 0;
        long_opts[i].val     = 0;

        if(a.short_opt)
        {
            *short_p = a.short_opt;
            ++short_p;
            if(a.type != Opt::Type::NOA)
            {
                *short_p = ':'; // argument to option is required
                ++short_p;
            }
        }
    }

    // fill last element
    memset(&long_opts[CLI::num], 0, sizeof(long_opts[CLI::num]));

    // assuming that argc and argv are the same as those passed to program
    int opt_index = 0;

    while(true)
    {
        int opt = getopt_long(argc, argv, short_opts, long_opts, &opt_index);
        if(opt == -1)
        {
            break;
        }

        switch(opt)
        {
        case 0:
            // store long option
            set_value(opt_index, optarg);
            break;

        case '?':
        {
            std::string unkn{build_name(optopt, argv[optind - 1])};
            throw CLIError{std::string{"Unrecognized option: "} + unkn};
        }

        case ':':
        {
            std::string miss{build_name(optopt, argv[optind - 1])};
            throw CLIError{std::string{"Option requires an argument: "} + miss};
        }

        default:
        {
            // if short option found
            const int index = short_opt_index(opt);
            if(index != -1)
            {
                set_value(index, optarg);
            }
        }
        }
    }

    // if we get non-option element in args, throw exception
    if(optind != argc)
    {
        // quote non-option
        std::string name{build_name(0, argv[optind])};
        throw CLIError{std::string{"Unexpected operand on command line: "} + name};
    }

    // set default values
    for(Opt& o : CLI::options)
    {
        if(o.value == nullptr     // is value still uninitialized?
           && o.deflt != nullptr) // try to substitute by default value
        {
            o.value  = o.deflt;
            o.passed = false;
        }
    }
}

template <typename CLI>
void CmdlineParser<CLI>::validate()
{
    // validate Args::arguments[i].value. nullptr isn't valid!
    for(const Opt& o : CLI::options)
    {
        if(o.value == nullptr) // is value still uninitialized?
        {
            std::string lopt{o.long_opt ? std::string("--") + o.long_opt : ""};
            std::string name{build_name(o.short_opt, lopt)};
            throw CLIError{std::string{"Missing required option: "} + name};
        }
    }
}

template <typename CLI>
void CmdlineParser<CLI>::print_usage(std::ostream& out, const char* name)
{
    out << "Usage: " << name << " [OPTIONS]..." << std::endl;

    for(const Opt& o : CLI::options)
    {
        std::string s_opt;
        std::string l_opt;
        std::string text;

        if(o.short_opt) // print out short key
        {
            char tmp[]{'-', o.short_opt, ' ', '\0'};
            if(o.long_opt) tmp[2] = ',';
            s_opt                 = std::string{"   "} + tmp; //indentation
        }
        if(o.long_opt) // print out long key
        {
            l_opt = std::string{" --"} + o.long_opt;

            if(o.value_pattern)
            {
                l_opt += '=';
                l_opt += o.value_pattern;
            }
        }
        if(o.deflt) // has default value?
        {
            text = std::string{"(default:"} + o.deflt + ") ";
        }
        else
        {
            text = "(required) ";
        }

        if(o.description)
        {
            text += o.description;
        }

        out << std::setw(6) << s_opt
            << std::setiosflags(std::ios::left) << std::setw(35) << l_opt
            << text << std::endl;
    }
}

} // namespace cmdline
} // namespace controller
} // namespace NST
//------------------------------------------------------------------------------
#endif // CMDLINE_PARSER_H
//------------------------------------------------------------------------------