-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_line_parser.hpp
170 lines (157 loc) · 4.9 KB
/
command_line_parser.hpp
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
#pragma once
/*******************************************************************************
* commandline.hpp
*
* Simple tool to read command line parameters
*
* Part of my utils library utils_tm - https://github.com/TooBiased/utils_tm.git
*
* Copyright (C) 2019 Tobias Maier <[email protected]>
*
* All rights reserved. Published under the BSD-2 license in the LICENSE file.
******************************************************************************/
#include <clocale>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
namespace utils_tm
{
class command_line_parser
{
public:
command_line_parser(int argn, char** argc)
{
std::setlocale(LC_ALL, "en_US.UTF-8");
for (size_t i = 0; i < size_t(argn); ++i)
{
_param_vec.emplace_back(argc[i]);
_flag_vec.push_back(usage_flags::unused);
}
}
std::string str_arg(const std::string& name, const std::string def = "")
{
auto ind = find_name(name);
if (ind + 1 < _param_vec.size())
{
_flag_vec[ind + 1] = usage_flags::used;
return _param_vec[ind + 1];
}
else if (ind < _param_vec.size())
{
_flag_vec[ind] = usage_flags::error;
std::cout << "found argument \"" << name
<< "\" without following integer!" << std::endl;
}
return def;
}
int int_arg(const std::string& name, int def = 0)
{
auto ind = find_name(name);
if (ind + 1 < _param_vec.size())
{
_flag_vec[ind + 1] = usage_flags::used;
int r = 0;
try
{
r = std::stoi(_param_vec[ind + 1]);
}
catch (std::invalid_argument& e)
{
_flag_vec[ind + 1] = usage_flags::error;
r = def;
std::cout
<< "error reading int argument \"" << name
<< "\" from console, got \"invalid_argument exception\""
<< std::endl;
}
return r;
}
else if (ind < _param_vec.size())
{
_flag_vec[ind] = usage_flags::error;
std::cout << "found argument \"" << name
<< "\" without following integer!" << std::endl;
}
return def;
}
double double_arg(const std::string& name, double def = 0.)
{
std::setlocale(LC_ALL, "en_US.UTF-8");
auto ind = find_name(name);
if (ind + 1 < _param_vec.size())
{
_flag_vec[ind + 1] = usage_flags::used;
double r = 0;
try
{
r = std::stod(_param_vec[ind + 1]);
}
catch (std::invalid_argument& e)
{
_flag_vec[ind + 1] = usage_flags::error;
r = def;
std::cout
<< "error reading double argument \"" << name
<< "\" from console, got \"invalid-argument exception\"!"
<< std::endl;
}
return r;
}
else if (ind < _param_vec.size())
{
_flag_vec[ind] = usage_flags::error;
std::cout << "found argument \"" << name
<< "\" without following double!" << std::endl;
}
return def;
}
bool bool_arg(const std::string& name)
{
return (find_name(name)) < _param_vec.size();
}
bool report()
{
bool un = true;
for (size_t i = 1; i < _param_vec.size(); ++i)
{
if (_flag_vec[i] != usage_flags::used)
{
if (_flag_vec[i] == usage_flags::unused)
{
std::cout << "parameter " << i << " = \"" << _param_vec[i]
<< "\" was unused!" << std::endl;
}
else if (_flag_vec[i] == usage_flags::error)
{
std::cout << "error reading parameter " << i << " = \""
<< _param_vec[i] << "\"" << std::endl;
}
un = false;
}
}
return un;
}
private:
enum class usage_flags
{
unused,
used,
error
};
std::vector<std::string> _param_vec;
std::vector<usage_flags> _flag_vec;
size_t find_name(const std::string& name)
{
for (size_t i = 0; i < _param_vec.size(); ++i)
{
if (_param_vec[i] == name)
{
_flag_vec[i] = usage_flags::used;
return i;
}
}
return _param_vec.size();
}
};
} // namespace utils_tm