argument_parser.hpp 7.32 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
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
template <class T>
struct value_parser
{
    template <MIGRAPHX_REQUIRES(not std::is_enum<T>{})>
    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;
    }

    template <MIGRAPHX_REQUIRES(std::is_enum<T>{})>
    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);
    }
};

struct argument_parser
{
    struct argument
    {
        std::vector<std::string> flags;
Paul's avatar
Paul committed
64
        std::function<bool(argument_parser&, const std::vector<std::string>&)> action{};
Paul's avatar
Paul committed
65
66
67
        std::string type          = "";
        std::string help          = "";
        std::string metavar       = "";
Paul's avatar
Paul committed
68
        std::string default_value = "";
Paul's avatar
Paul committed
69
        unsigned nargs            = 1;
Paul's avatar
Paul committed
70
71
72
    };

    template <class T, class... Fs>
Paul's avatar
Paul committed
73
    void operator()(T& x, std::vector<std::string> flags, Fs... fs)
Paul's avatar
Paul committed
74
    {
Paul's avatar
Paul committed
75
        arguments.push_back({flags, [&](auto&&, const std::vector<std::string>& params) {
Paul's avatar
Paul committed
76
77
78
79
80
                                 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
81

Paul's avatar
Paul committed
82
83
        argument& arg     = arguments.back();
        arg.type          = migraphx::get_type_name<T>();
Paul's avatar
Paul committed
84
        arg.default_value = to_string(x);
Paul's avatar
Paul committed
85
86
87
        migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
    }

Paul's avatar
Paul committed
88
    template <class... Fs>
Paul's avatar
Paul committed
89
    void operator()(std::nullptr_t x, std::vector<std::string> flags, Fs... fs)
Paul's avatar
Paul committed
90
    {
Paul's avatar
Paul committed
91
        arguments.push_back({std::move(flags)});
Paul's avatar
Paul committed
92
93
94

        argument& arg = arguments.back();
        arg.type      = "";
Paul's avatar
Paul committed
95
        arg.nargs     = 0;
Paul's avatar
Paul committed
96
97
98
        migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
    }

Paul's avatar
Paul committed
99
    MIGRAPHX_DRIVER_STATIC auto nargs(unsigned n = 1)
Paul's avatar
Paul committed
100
    {
Paul's avatar
Paul committed
101
        return [=](auto&&, auto& arg) { arg.nargs = n; };
Paul's avatar
Paul committed
102
103
    }

Paul's avatar
Paul committed
104
    template <class F>
Paul's avatar
Paul committed
105
    MIGRAPHX_DRIVER_STATIC auto write_action(F f)
Paul's avatar
Paul committed
106
107
    {
        return [=](auto& x, auto& arg) {
Paul's avatar
Paul committed
108
            arg.action = [&, f](auto& self, const std::vector<std::string>& params) {
Paul's avatar
Paul committed
109
110
111
112
113
114
                f(self, x, params);
                return false;
            };
        };
    }

Paul's avatar
Paul committed
115
    template <class F>
Paul's avatar
Paul committed
116
    MIGRAPHX_DRIVER_STATIC auto do_action(F f)
Paul's avatar
Paul committed
117
118
    {
        return [=](auto&, auto& arg) {
Paul's avatar
Paul committed
119
            arg.nargs  = 0;
Paul's avatar
Paul committed
120
            arg.action = [&, f](auto& self, const std::vector<std::string>&) {
Paul's avatar
Paul committed
121
122
123
124
125
126
                f(self);
                return true;
            };
        };
    }

Paul's avatar
Paul committed
127
    MIGRAPHX_DRIVER_STATIC auto append()
Paul's avatar
Paul committed
128
    {
Paul's avatar
Paul committed
129
130
131
        return write_action([](auto&, auto& x, auto& params) {
            using type = typename decltype(params)::value_type;
            std::transform(params.begin(),
Paul's avatar
Paul committed
132
133
                           params.end(),
                           std::inserter(x, x.end()),
Paul's avatar
Paul committed
134
135
                           [](std::string y) { return value_parser<type>::apply(y); });
        });
Paul's avatar
Paul committed
136
137
    }

Paul's avatar
Paul committed
138
    MIGRAPHX_DRIVER_STATIC auto show_help(const std::string& msg = "")
Paul's avatar
Paul committed
139
    {
Paul's avatar
Paul committed
140
        return do_action([=](auto& self) {
Paul's avatar
Paul committed
141
            for(auto&& arg : self.arguments)
Paul's avatar
Paul committed
142
143
144
            {
                std::cout << std::endl;
                std::string prefix = "    ";
Paul's avatar
Paul committed
145
                if(arg.flags.empty())
Paul's avatar
Paul committed
146
147
148
149
                {
                    std::cout << prefix;
                    std::cout << arg.metavar;
                }
Paul's avatar
Paul committed
150
151
152
153
154
155
                for(const std::string& a : arg.flags)
                {
                    std::cout << prefix;
                    std::cout << a;
                    prefix = ", ";
                }
Paul's avatar
Paul committed
156
                if(not arg.type.empty())
Paul's avatar
Paul committed
157
                {
Paul's avatar
Paul committed
158
                    std::cout << " [" << arg.type << "]";
Paul's avatar
Paul committed
159
                    if(not arg.default_value.empty())
Paul's avatar
Paul committed
160
161
                        std::cout << " (Default: " << arg.default_value << ")";
                }
Paul's avatar
Paul committed
162
163
164
165
                std::cout << std::endl;
                std::cout << "        " << arg.help << std::endl;
            }
            std::cout << std::endl;
Paul's avatar
Paul committed
166
            if(not msg.empty())
Paul's avatar
Paul committed
167
                std::cout << msg << std::endl;
Paul's avatar
Paul committed
168
169
170
        });
    }

Paul's avatar
Paul committed
171
    MIGRAPHX_DRIVER_STATIC auto help(std::string help)
Paul's avatar
Paul committed
172
    {
Paul's avatar
Paul committed
173
        return [=](auto&, auto& arg) { arg.help = help; };
Paul's avatar
Paul committed
174
175
    }

Paul's avatar
Paul committed
176
    MIGRAPHX_DRIVER_STATIC auto metavar(std::string metavar)
Paul's avatar
Paul committed
177
178
179
180
    {
        return [=](auto&, auto& arg) { arg.metavar = metavar; };
    }

Paul's avatar
Paul committed
181
    template <class T>
Paul's avatar
Paul committed
182
    MIGRAPHX_DRIVER_STATIC auto set_value(T value)
Paul's avatar
Paul committed
183
184
    {
        return [=](auto& x, auto& arg) {
Paul's avatar
Paul committed
185
            arg.nargs  = 0;
Paul's avatar
Paul committed
186
            arg.type   = "";
Paul's avatar
Paul committed
187
            arg.action = [&, value](auto&, const std::vector<std::string>&) {
Paul's avatar
Paul committed
188
189
190
191
192
193
                x = value;
                return false;
            };
        };
    }

Paul's avatar
Paul committed
194
    bool parse(std::vector<std::string> args)
Paul's avatar
Paul committed
195
    {
Paul's avatar
Paul committed
196
        std::unordered_map<std::string, unsigned> keywords;
Paul's avatar
Paul committed
197
        for(auto&& arg : arguments)
Paul's avatar
Paul committed
198
        {
Paul's avatar
Paul committed
199
            for(auto&& flag : arg.flags)
Paul's avatar
Paul committed
200
                keywords[flag] = arg.nargs + 1;
Paul's avatar
Paul committed
201
        }
Paul's avatar
Paul committed
202
        auto arg_map = generic_parse(std::move(args), [&](std::string x) { return keywords[x]; });
Paul's avatar
Paul committed
203
        for(auto&& arg : arguments)
Paul's avatar
Paul committed
204
        {
Paul's avatar
Paul committed
205
            auto flags = arg.flags;
Paul's avatar
Paul committed
206
            if(flags.empty())
Paul's avatar
Paul committed
207
                flags = {""};
Paul's avatar
Paul committed
208
            for(auto&& flag : flags)
Paul's avatar
Paul committed
209
            {
Paul's avatar
Paul committed
210
                if(arg_map.count(flag) > 0)
Paul's avatar
Paul committed
211
                {
Paul's avatar
Paul committed
212
                    if(arg.action(*this, arg_map[flag]))
Paul's avatar
Paul committed
213
                        return true;
Paul's avatar
Paul committed
214
215
216
                }
            }
        }
Paul's avatar
Paul committed
217
        return false;
Paul's avatar
Paul committed
218
219
220
221
222
223
224
225
226
    }

    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
227
        bool clear = false;
Paul's avatar
Paul committed
228
229
        for(auto&& x : as)
        {
Paul's avatar
Paul committed
230
231
            auto k = is_keyword(x);
            if(k > 0)
Paul's avatar
Paul committed
232
233
234
            {
                flag = x;
                result[flag]; // Ensure the flag exists
Paul's avatar
Paul committed
235
                if(k == 1)
Paul's avatar
Paul committed
236
                    flag = "";
Paul's avatar
Paul committed
237
                else if(k == 2)
Paul's avatar
Paul committed
238
239
240
                    clear = true;
                else
                    clear = false;
Paul's avatar
Paul committed
241
242
243
244
            }
            else
            {
                result[flag].push_back(x);
Paul's avatar
Paul committed
245
                if(clear)
Paul's avatar
Paul committed
246
247
                    flag = "";
                clear = false;
Paul's avatar
Paul committed
248
249
250
251
            }
        }
        return result;
    }
Paul's avatar
Paul committed
252

Paul's avatar
Paul committed
253
    private:
Paul's avatar
Paul committed
254
    std::vector<argument> arguments;
Paul's avatar
Paul committed
255
256
};

Paul's avatar
Paul committed
257
258
259
260
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx

Paul's avatar
Paul committed
261
#endif