-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.h
172 lines (152 loc) · 5.72 KB
/
args.h
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
// Argument parser. Not the fastest, but its very easy to use.
// made by bain on 10.01.2021
#include <string>
#include <cstring>
#include <stdexcept>
#include <utility>
#include <vector>
#include <algorithm>
#include <iostream>
#ifndef VISTE_ARGS_H
#define VISTE_ARGS_H
namespace Args {
namespace Exceptions {
class ArgumentParsingError: public std::exception
{
std::string msg;
public:
explicit ArgumentParsingError(std::string msg) {
this->msg = std::move(msg);
}
[[nodiscard]] const char *what() const noexcept override
{
return msg.data();
}
};
}
struct ArgOpt {
int position = -1;
std::string long_name;
char short_name;
};
class Parser {
int argc;
char** argv;
private:
std::vector<int> not_positional_args;
int get_string(const ArgOpt& options, const bool& is_bool) {
int positional_arg = 0;
for (int i = 0; i < argc; i++) {
char* argument = argv[i];
int size_of_argument = strlen(argv[i]);
// check if keyword argument
int t = (argument[0] == '-' && size_of_argument > 1);
t += (argument[1] == '-');
switch (t) {
case 0:
// The current string is a positional argument.
// Check if argument is not positional
if (std::find(not_positional_args.begin(), not_positional_args.end(), i) != not_positional_args.end())
continue;
if (positional_arg == options.position) return i;
positional_arg++;
break;
case 1:
// The current string is a keyword argument with a short name.
if (!is_bool && options.short_name == argument[size_of_argument - 1]) {
if (argc > i+1) {
not_positional_args.push_back(++i);
return i;
}
else throw Exceptions::ArgumentParsingError((std::string)"Argument " + argument + " needs a value. Got none.");
} else {
for (int j = 0; j < size_of_argument; j++)
if (argument[j] == options.short_name) return i;
}
break;
case 2:
// The current string is a keyword argument with a long name.
if ("--"+options.long_name == argument) {
if (!is_bool) {
if (argc > i+1) {
not_positional_args.push_back(++i);
return i;
}
else
throw Exceptions::ArgumentParsingError(
(std::string) "Argument " + argument + " needs a value. Got none.");
}
else return i;
}
break;
default:
break;
}
}
return -1;
}
public:
explicit Parser(int argc, char **argv) {
this->argc = argc;
this->argv = argv;
}
template<typename ValueType>
ValueType get_arg(const ArgOpt& options) {
ValueType output;
bool val_type_is_bool = std::is_same_v<ValueType, bool>;
int position = get_string(options, val_type_is_bool);
if (position == -1) {
std::string s;
if (options.long_name.empty()) s+=options.short_name;
else s+= options.long_name;
throw Exceptions::ArgumentParsingError(s+" is a required argument.");
} else {
if (val_type_is_bool) return true;
return convert_arg<ValueType>(argv[position]);
}
}
template<typename ValueType>
ValueType get_arg(const ArgOpt& options, const ValueType& default_) {
ValueType v;
try {
v = get_arg<ValueType>(options);
}
catch (Exceptions::ArgumentParsingError& e) {
return default_;
}
return v;
}
template<typename ValueType>
ValueType convert_arg(char *arg) {
if (std::is_floating_point_v<ValueType>) {
return (ValueType)(convert_arg<double>(arg));
} else if (std::is_arithmetic_v<ValueType>) {
return (ValueType)(convert_arg<long>(arg));
}
throw Exceptions::ArgumentParsingError((std::string)"Cannot convert \""+arg+"\"");
}
};
}
// template specializations must be in namespace scope
template<>
std::string Args::Parser::get_arg<std::string>(const ArgOpt &options) {
int position = get_string(options, false);
if (position == -1) {
std::string s;
if (options.long_name.empty()) s+=options.short_name;
else s+= options.long_name;
throw Exceptions::ArgumentParsingError(s+" is a required argument.");
} else {
return std::string(argv[position]);
}
}
// converters, yaaay
template<>
long Args::Parser::convert_arg<long>(char *arg) {
return strtol(arg, nullptr, 10);
}
template<>
double Args::Parser::convert_arg<double>(char *arg) {
return strtod(arg, nullptr);
}
#endif