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
|
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#pragma once
#include <functional>
#include <variant>
#include <optional>
#include <string>
#include <type_traits>
#include <iostream>
namespace arg
{
struct noarg {};
template<typename T>
struct Opt
{
std::string shortopt;
std::string longopt;
std::function<int(T)> cb;
std::string help;
T t{};
};
template<>
struct Opt<noarg>
{
std::string shortopt;
std::string longopt;
std::function<int()> cb;
std::string help;
noarg t{};
};
template<typename... Ts>
class Parser
{
public:
struct missing_arg{};
int parse(int argc, const char* const argv[])
{
if(argc < 1)
{
return 1;
}
for(int i = 1; i < argc; ++i) // skip argv[0] which is program name
{
bool was_handled{false};
for(const auto& option : options)
{
enum class state { handled, unhandled };
std::pair<int, state> ret =
std::visit([&](auto&& opt) -> std::pair<int, state>
{
if(opt.shortopt != argv[i] &&
opt.longopt != argv[i])
{
return {0, state::unhandled};
}
try
{
using T = std::decay_t<decltype(opt)>;
if constexpr (std::is_same_v<T, Opt<noarg>>)
{
return {opt.cb(), state::handled};
}
else
{
auto arg = convert(argc, argv, i, opt.t);
return {opt.cb(arg), state::handled};
}
}
catch(std::invalid_argument&)
{
std::cout << argv[0] <<
": invalid argument for option '" <<
opt.shortopt << "'\n";
std::cout << "Type '" << argv[0] <<
" -h' for more information.\n";
return {1, state::handled};
}
catch(missing_arg&)
{
std::cout << argv[0] <<
": option requires and argument '" <<
opt.shortopt << "'\n";
std::cout << "Type '" << argv[0] <<
" -h' for more information.\n";
std::cout << opt.help << "\n";
return {1, state::handled};
}
},
option);
if(ret.second == state::handled && ret.first != 0)
{
return ret.first;
}
was_handled |= ret.second == state::handled;
if(was_handled)
{
break;
}
}
if(!was_handled)
{
std::cout << argv[0] << ": invalid option '" << argv[i] << "'\n";
std::cout << "Type '" << argv[0] << " -h' for more information.\n";
return 1;
}
}
return 0;
}
template<typename T>
void add(const std::string& shortopt,
const std::string& longopt,
std::function<int(T)> cb,
const std::string& help)
{
options.emplace_back(Opt<T>{shortopt, longopt, cb, help});
}
void add(const std::string& shortopt,
const std::string& longopt,
std::function<int()> cb,
const std::string& help)
{
options.emplace_back(Opt<noarg>{shortopt, longopt, cb, help});
}
private:
template<typename T>
T convert(int argc, const char* const argv[], int& i, T)
{
auto opt = convert(argc, argv, i, std::optional<T>{});
if(!opt)
{
throw missing_arg{};
}
return *opt;
}
template<typename T>
std::optional<T> convert(int argc, const char* const argv[], int& i,
std::optional<T>)
{
std::string arg;
bool has_arg{false};
if(i+1 < argc)
{
arg = argv[i+1];
has_arg = !arg.starts_with("-");
if(has_arg)
{
++i;
}
}
if(!has_arg)
{
return {};
}
if constexpr (std::is_same_v<T, int>)
{
return std::stoi(arg);
}
else if constexpr (std::is_same_v<T, double>)
{
return std::stod(arg);
}
else if constexpr (std::is_same_v<T, std::string>)
{
return arg;
}
else
{
static_assert(std::is_same_v<T, void>, "missing");
}
return {};
}
using Opts = std::variant<Opt<noarg>, Opt<Ts>...>;
std::vector<Opts> options;
};
} // arg::
|