argument_parser.hpp 8.55 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#ifndef MIGRAPHX_GUARD_RTGLIB_ARGUMENT_PARSER_HPP
#define MIGRAPHX_GUARD_RTGLIB_ARGUMENT_PARSER_HPP

Paul's avatar
Paul committed
4
5
6
7
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
Paul's avatar
Paul committed
8
#include <string>
Paul's avatar
Paul committed
9
#include <sstream>
Paul's avatar
Paul committed
10
11
#include <type_traits>
#include <unordered_map>
Paul's avatar
Paul committed
12
#include <utility>
Paul's avatar
Paul committed
13
14
#include <vector>

Paul's avatar
Paul committed
15
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
16
17
#include <migraphx/requires.hpp>
#include <migraphx/type_name.hpp>
Paul's avatar
Paul committed
18
#include <migraphx/functional.hpp>
Paul's avatar
Paul committed
19
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
20
21
22
23

namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
24

Paul's avatar
Paul committed
25
26
27
28
29
30
#ifdef MIGRAPHX_USE_CLANG_TIDY
#define MIGRAPHX_DRIVER_STATIC
#else
#define MIGRAPHX_DRIVER_STATIC static
#endif

Paul's avatar
Paul committed
31
template <class T>
Paul's avatar
Paul committed
32
33
34
35
36
using bare = std::remove_cv_t<std::remove_reference_t<T>>;

namespace detail {

template <class T>
Paul's avatar
Paul committed
37
auto is_container(int, T&& x) -> decltype(x.insert(x.end(), *x.begin()), std::true_type{});
Paul's avatar
Paul committed
38
39
40
41
42
43
44
45
46
47
48
49
50

template <class T>
std::false_type is_container(float, T&&);

} // namespace detail

template <class T>
struct is_container : decltype(detail::is_container(int(0), std::declval<T>()))
{
};

template <class T>
using is_multi_value =
Paul's avatar
Paul committed
51
    std::integral_constant<bool, (is_container<T>{} and not std::is_convertible<T, std::string>{})>;
Paul's avatar
Paul committed
52

Paul's avatar
Paul committed
53
54
55
template <class T>
struct value_parser
{
Paul's avatar
Paul committed
56
    template <MIGRAPHX_REQUIRES(not std::is_enum<T>{} and not is_multi_value<T>{})>
Paul's avatar
Paul committed
57
58
59
60
61
62
63
64
65
66
67
    static T apply(const std::string& x)
    {
        T result;
        std::stringstream ss;
        ss.str(x);
        ss >> result;
        if(ss.fail())
            throw std::runtime_error("Failed to parse: " + x);
        return result;
    }

Paul's avatar
Paul committed
68
    template <MIGRAPHX_REQUIRES(std::is_enum<T>{} and not is_multi_value<T>{})>
Paul's avatar
Paul committed
69
70
71
72
73
74
75
76
77
78
    static T apply(const std::string& x)
    {
        std::ptrdiff_t i;
        std::stringstream ss;
        ss.str(x);
        ss >> i;
        if(ss.fail())
            throw std::runtime_error("Failed to parse: " + x);
        return static_cast<T>(i);
    }
Paul's avatar
Paul committed
79
80
81
82
83
84
85
86
87

    template <MIGRAPHX_REQUIRES(is_multi_value<T>{} and not std::is_enum<T>{})>
    static T apply(const std::string& x)
    {
        T result;
        using value_type = typename T::value_type;
        result.insert(result.end(), value_parser<value_type>::apply(x));
        return result;
    }
Paul's avatar
Paul committed
88
89
90
91
92
93
94
};

struct argument_parser
{
    struct argument
    {
        std::vector<std::string> flags;
Paul's avatar
Paul committed
95
        std::function<bool(argument_parser&, const std::vector<std::string>&)> action{};
Paul's avatar
Paul committed
96
97
98
        std::string type          = "";
        std::string help          = "";
        std::string metavar       = "";
Paul's avatar
Paul committed
99
        std::string default_value = "";
Paul's avatar
Paul committed
100
        unsigned nargs            = 1;
Paul's avatar
Paul committed
101
102
    };

Paul's avatar
Paul committed
103
    template <class T, MIGRAPHX_REQUIRES(is_multi_value<T>{})>
Paul's avatar
Paul committed
104
105
106
107
108
    std::string as_string_value(const T& x)
    {
        return to_string_range(x);
    }

Paul's avatar
Paul committed
109
    template <class T, MIGRAPHX_REQUIRES(not is_multi_value<T>{})>
Paul's avatar
Paul committed
110
111
112
113
114
    std::string as_string_value(const T& x)
    {
        return to_string(x);
    }

Paul's avatar
Paul committed
115
    template <class T, class... Fs>
Paul's avatar
Paul committed
116
    void operator()(T& x, const std::vector<std::string>& flags, Fs... fs)
Paul's avatar
Paul committed
117
    {
Paul's avatar
Paul committed
118
        arguments.push_back({flags, [&](auto&&, const std::vector<std::string>& params) {
Paul's avatar
Paul committed
119
120
121
122
123
                                 if(params.empty())
                                     throw std::runtime_error("Flag with no value.");
                                 x = value_parser<T>::apply(params.back());
                                 return false;
                             }});
Paul's avatar
Paul committed
124

Paul's avatar
Paul committed
125
126
        argument& arg     = arguments.back();
        arg.type          = migraphx::get_type_name<T>();
Paul's avatar
Paul committed
127
        arg.default_value = as_string_value(x);
Paul's avatar
Paul committed
128
129
130
        migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
    }

Paul's avatar
Paul committed
131
    template <class... Fs>
Paul's avatar
Paul committed
132
    void operator()(std::nullptr_t x, std::vector<std::string> flags, Fs... fs)
Paul's avatar
Paul committed
133
    {
Paul's avatar
Paul committed
134
        arguments.push_back({std::move(flags)});
Paul's avatar
Paul committed
135
136
137

        argument& arg = arguments.back();
        arg.type      = "";
Paul's avatar
Paul committed
138
        arg.nargs     = 0;
Paul's avatar
Paul committed
139
140
141
        migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
    }

Paul's avatar
Paul committed
142
    MIGRAPHX_DRIVER_STATIC auto nargs(unsigned n = 1)
Paul's avatar
Paul committed
143
    {
Paul's avatar
Paul committed
144
        return [=](auto&&, auto& arg) { arg.nargs = n; };
Paul's avatar
Paul committed
145
146
    }

Paul's avatar
Paul committed
147
    template <class F>
Paul's avatar
Paul committed
148
    MIGRAPHX_DRIVER_STATIC auto write_action(F f)
Paul's avatar
Paul committed
149
150
    {
        return [=](auto& x, auto& arg) {
Paul's avatar
Paul committed
151
            arg.action = [&, f](auto& self, const std::vector<std::string>& params) {
Paul's avatar
Paul committed
152
153
154
155
156
157
                f(self, x, params);
                return false;
            };
        };
    }

Paul's avatar
Paul committed
158
    template <class F>
Paul's avatar
Paul committed
159
    MIGRAPHX_DRIVER_STATIC auto do_action(F f)
Paul's avatar
Paul committed
160
161
    {
        return [=](auto&, auto& arg) {
Paul's avatar
Paul committed
162
            arg.nargs  = 0;
Paul's avatar
Paul committed
163
            arg.action = [&, f](auto& self, const std::vector<std::string>&) {
Paul's avatar
Paul committed
164
165
166
167
168
169
                f(self);
                return true;
            };
        };
    }

Paul's avatar
Paul committed
170
    MIGRAPHX_DRIVER_STATIC auto append()
Paul's avatar
Paul committed
171
    {
Paul's avatar
Paul committed
172
        return write_action([](auto&, auto& x, auto& params) {
Paul's avatar
Paul committed
173
            using type = typename bare<decltype(params)>::value_type;
Paul's avatar
Paul committed
174
            std::transform(params.begin(),
Paul's avatar
Paul committed
175
176
                           params.end(),
                           std::inserter(x, x.end()),
Paul's avatar
Paul committed
177
178
                           [](std::string y) { return value_parser<type>::apply(y); });
        });
Paul's avatar
Paul committed
179
180
    }

Paul's avatar
Paul committed
181
    MIGRAPHX_DRIVER_STATIC auto show_help(const std::string& msg = "")
Paul's avatar
Paul committed
182
    {
Paul's avatar
Paul committed
183
        return do_action([=](auto& self) {
Paul's avatar
Paul committed
184
            for(auto&& arg : self.arguments)
Paul's avatar
Paul committed
185
186
187
            {
                std::cout << std::endl;
                std::string prefix = "    ";
Paul's avatar
Paul committed
188
                if(arg.flags.empty())
Paul's avatar
Paul committed
189
190
191
192
                {
                    std::cout << prefix;
                    std::cout << arg.metavar;
                }
Paul's avatar
Paul committed
193
194
195
196
197
198
                for(const std::string& a : arg.flags)
                {
                    std::cout << prefix;
                    std::cout << a;
                    prefix = ", ";
                }
Paul's avatar
Paul committed
199
                if(not arg.type.empty())
Paul's avatar
Paul committed
200
                {
Paul's avatar
Paul committed
201
                    std::cout << " [" << arg.type << "]";
Paul's avatar
Paul committed
202
                    if(not arg.default_value.empty())
Paul's avatar
Paul committed
203
204
                        std::cout << " (Default: " << arg.default_value << ")";
                }
Paul's avatar
Paul committed
205
206
207
208
                std::cout << std::endl;
                std::cout << "        " << arg.help << std::endl;
            }
            std::cout << std::endl;
Paul's avatar
Paul committed
209
            if(not msg.empty())
Paul's avatar
Paul committed
210
                std::cout << msg << std::endl;
Paul's avatar
Paul committed
211
212
213
        });
    }

Paul's avatar
Paul committed
214
    MIGRAPHX_DRIVER_STATIC auto help(const std::string& help)
Paul's avatar
Paul committed
215
    {
Paul's avatar
Paul committed
216
        return [=](auto&, auto& arg) { arg.help = help; };
Paul's avatar
Paul committed
217
218
    }

Paul's avatar
Paul committed
219
    MIGRAPHX_DRIVER_STATIC auto metavar(const std::string& metavar)
Paul's avatar
Paul committed
220
221
222
223
    {
        return [=](auto&, auto& arg) { arg.metavar = metavar; };
    }

Paul's avatar
Paul committed
224
    template <class T>
Paul's avatar
Paul committed
225
    MIGRAPHX_DRIVER_STATIC auto set_value(T value)
Paul's avatar
Paul committed
226
227
    {
        return [=](auto& x, auto& arg) {
Paul's avatar
Paul committed
228
            arg.nargs  = 0;
Paul's avatar
Paul committed
229
            arg.type   = "";
Paul's avatar
Paul committed
230
            arg.action = [&, value](auto&, const std::vector<std::string>&) {
Paul's avatar
Paul committed
231
232
233
234
235
236
                x = value;
                return false;
            };
        };
    }

Paul's avatar
Paul committed
237
    bool parse(std::vector<std::string> args)
Paul's avatar
Paul committed
238
    {
Paul's avatar
Paul committed
239
        std::unordered_map<std::string, unsigned> keywords;
Paul's avatar
Paul committed
240
        for(auto&& arg : arguments)
Paul's avatar
Paul committed
241
        {
Paul's avatar
Paul committed
242
            for(auto&& flag : arg.flags)
Paul's avatar
Paul committed
243
                keywords[flag] = arg.nargs + 1;
Paul's avatar
Paul committed
244
        }
Paul's avatar
Paul committed
245
246
        auto arg_map =
            generic_parse(std::move(args), [&](const std::string& x) { return keywords[x]; });
Paul's avatar
Paul committed
247
        for(auto&& arg : arguments)
Paul's avatar
Paul committed
248
        {
Paul's avatar
Paul committed
249
            auto flags = arg.flags;
Paul's avatar
Paul committed
250
            if(flags.empty())
Paul's avatar
Paul committed
251
                flags = {""};
Paul's avatar
Paul committed
252
            for(auto&& flag : flags)
Paul's avatar
Paul committed
253
            {
Paul's avatar
Paul committed
254
                if(arg_map.count(flag) > 0)
Paul's avatar
Paul committed
255
                {
Paul's avatar
Paul committed
256
                    if(arg.action(*this, arg_map[flag]))
Paul's avatar
Paul committed
257
                        return true;
Paul's avatar
Paul committed
258
259
260
                }
            }
        }
Paul's avatar
Paul committed
261
        return false;
Paul's avatar
Paul committed
262
263
264
265
266
267
268
269
270
    }

    using string_map = std::unordered_map<std::string, std::vector<std::string>>;
    template <class IsKeyword>
    static string_map generic_parse(std::vector<std::string> as, IsKeyword is_keyword)
    {
        string_map result;

        std::string flag;
Paul's avatar
Paul committed
271
        bool clear = false;
Paul's avatar
Paul committed
272
273
        for(auto&& x : as)
        {
Paul's avatar
Paul committed
274
275
            auto k = is_keyword(x);
            if(k > 0)
Paul's avatar
Paul committed
276
277
278
            {
                flag = x;
                result[flag]; // Ensure the flag exists
Paul's avatar
Paul committed
279
                if(k == 1)
Paul's avatar
Paul committed
280
                    flag = "";
Paul's avatar
Paul committed
281
                else if(k == 2)
Paul's avatar
Paul committed
282
283
284
                    clear = true;
                else
                    clear = false;
Paul's avatar
Paul committed
285
286
287
288
            }
            else
            {
                result[flag].push_back(x);
Paul's avatar
Paul committed
289
                if(clear)
Paul's avatar
Paul committed
290
291
                    flag = "";
                clear = false;
Paul's avatar
Paul committed
292
293
294
295
            }
        }
        return result;
    }
Paul's avatar
Paul committed
296

Paul's avatar
Paul committed
297
    private:
Paul's avatar
Paul committed
298
    std::vector<argument> arguments;
Paul's avatar
Paul committed
299
300
};

Paul's avatar
Paul committed
301
302
303
304
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
305
#endif