-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgParser.h
More file actions
191 lines (165 loc) · 4.41 KB
/
ArgParser.h
File metadata and controls
191 lines (165 loc) · 4.41 KB
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
191
#ifndef ARGPARSER
#define ARGPARSER
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include <map>
#include <vector>
#include <iterator>
#include <assert.h>
#include <limits>
// using namespace std;
class BadConversion : public std::runtime_error
{
public:
BadConversion (const std::string &s) : std::runtime_error(s) {}
};
template<typename NumType>
inline NumType convert_to_num (const std::string &s)
{
if (s == "inf")
{
return std::numeric_limits<NumType>::infinity();
}
else if (s == "nan")
{
return std::numeric_limits<NumType>::quiet_NaN();
}
else
{
std::istringstream ss(s);
NumType x;
if (!(ss>>x)) {throw BadConversion("convert_to_numeric(\""+s+"\")");}
return x;
}
}
void find_and_replace (std::string& source, std::string const& find, std::string const& replace)
{
for(std::string::size_type i = 0; (i = source.find(find, i)) != std::string::npos;)
{
source.replace(i, find.length(), replace);
i += replace.length() - find.length() + 1;
}
}
class ArgParser
{
typedef std::string string;
public:
ArgParser (int argc_input, char** argv_input);
string operator [] (string keyword) {return dictionary[keyword];}; // brackets [] = string
int operator () (string keyword) {return get<int>(keyword);}; // parentheses () = int
template<typename Scalar> Scalar get (string keyword, Scalar default_val=Scalar());
template<typename Scalar> std::vector<Scalar> get_list (string keyword, std::vector<Scalar> default_val={Scalar()});
bool test (string key) const;
string info() const;
string str() const;
private:
std::map<string,string> dictionary;
};
ArgParser::
ArgParser (int argc_input, char** argv_input)
{
std::vector<string> args(argv_input+1, argv_input+argc_input);
for (int i=0; i<args.size(); ++i)
{
if (args[i].substr(0,1) == "-")
{
args[i].erase(0,1);
std::size_t found_eq = args[i].find("=");
// form "-arg=x"
if (found_eq != std::string::npos)
{
string repl_eq = args[i].replace(found_eq, 1, " ");
std::istringstream iss(repl_eq);
std::vector<string> split_eq{std::istream_iterator<string>{iss}, std::istream_iterator<string>{}};
assert(split_eq.size() == 2);
dictionary.insert(std::pair<string,string>(split_eq[0],split_eq[1]));
}
// form "-arg x" or "-arg"
else
{
if (i!=args.size()-1) // in the middle of arglist
{
// form "-arg x"
if (args[i+1].substr(0,1) != "-")
{
dictionary.insert(std::pair<string,string>(args[i],args[i+1])); // store -arg val
}
// form "-arg" is interpreted as "arg=1"
else
{
dictionary.insert(std::pair<string,string>(args[i],"1")); // store -arg 1 (true)
}
}
else // at the end of arglist
{
dictionary.insert(std::pair<string,string>(args[i],"1")); // store -arg 1 (true)
}
}
}
}
}
std::string ArgParser::
info() const
{
std::stringstream ss;
ss << "ArgParser: ";
for (auto it=dictionary.begin(); it!=dictionary.end(); ++it)
{
ss << "-" << it->first << "=" << it->second;
if (it!=--dictionary.end()) {ss << " ";}
}
return ss.str();
}
std::string ArgParser::
str() const
{
std::stringstream ss;
for (auto it=dictionary.begin(); it!=dictionary.end(); ++it)
{
ss << it->first << "=" << it->second;
if (it!=--dictionary.end()) {ss << "_";}
}
return ss.str();
}
bool ArgParser::
test (string key) const
{
return (dictionary.find(key) != dictionary.end())? true : false;
}
template<typename Scalar>
Scalar ArgParser::
get (string keyword, Scalar default_val)
{
return (dictionary.find(keyword)!=dictionary.end()) ? convert_to_num<Scalar>(dictionary[keyword]) : static_cast<Scalar>(default_val);
// If not in dictionary, return 0 (false) by default
}
template<typename Scalar>
std::vector<Scalar> ArgParser::
get_list (string keyword, std::vector<Scalar> default_val)
{
if (dictionary.find(keyword)!=dictionary.end())
{
std::vector<string> Vstr;
std::vector<Scalar> Vnum;
string arglist = dictionary[keyword]; // arglist="1,2,3"
find_and_replace(arglist,","," ");
std::istringstream ss(arglist);
copy(std::istream_iterator<string>(ss), std::istream_iterator<string>(), std::back_inserter< std::vector<string> >(Vstr)); // Vout[0]="1", Vout[1]="2", Vout[2]="3"
// convert to Scalar
for (auto it=Vstr.begin(); it!=Vstr.end(); ++it)
{
std::istringstream iss(*it);
Scalar temp;
iss >> temp;
Vnum.push_back(temp);
}
return Vnum;
}
else
{
return default_val;
}
}
#endif