-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cc
175 lines (148 loc) · 6.02 KB
/
Parser.cc
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
#include "Parser.h"
#include <cstring>
#include <algorithm>
#include <stdexcept>
namespace op {
void Parser::parseCommandLine(int argc, const char* const argv[]) {
if(argc > 0)
if(strlen(argv[0]) == 0)
throw std::runtime_error("empty argv");
for(int i=1; i<argc; ++i){
// research option help
if( (strcmp(argv[i],"--help") == 0) || (strcmp(argv[i],"-h") == 0) ){
printHelp(std::cout);
exit(1);
}
size_t size = strlen(argv[i]);
if(size == 0) {
throw std::runtime_error("empty argv");
} else if(argv[i][0] == '-') {
//option
if(size == 1){
throw std::runtime_error("empty option");
} else {
int start = 1;
if(argv[i][1] == '-') {
start = 2;
}
std::string argv_str = argv[i];
std::string option_name = argv_str.substr(start, size-start);
/* Search option, check if value expected */
if(!isOption(option_name)){
throw std::runtime_error("option not valid");
} else {
Option &option = Parser::getOption(option_name);
option.parsed();
if(option.expectValue()){
if( (i+1 >= argc) || (strlen(argv[i+1]) == 0) ){
throw std::runtime_error("value of option not found");
}
std::string value = argv[i+1];
std::cout << "value of option : " << value << std::endl;
option = value;
++i;
}
}
}
} else {
//not an option and not his value, so positional argument
Parser::positionalArguments.emplace_back(argv[i]);
}
}
// Check if mandatory option not read
for(const Option& opt : Parser::options){
if( (!opt) && (opt.isMandatory()) ){
throw std::runtime_error("mandatory option not read");
}
}
}
void Parser::printHelp(std::ostream& stream) const {
std::string result;
for (const auto& current_option : options) {
auto vec = current_option.getNames();
bool mandatory = current_option.isMandatory();
bool expect = current_option.expectValue();
std::string defaultval;
if (current_option.hasDefault()) {
defaultval = current_option.getValue();
}
std::string ret;
auto size = vec.size();
for (size_t j = 0; j < size; ++j) {
ret = "";
if (j == size-1 && j == 0) { // case : without any alias
std::string complement;
if (expect)
complement.append(" value");
if (mandatory)
complement.append(" MANDATORY");
if (!defaultval.empty())
complement.append (" (default value : <" + defaultval + ">)");
ret.append("\t --" + vec[j] + complement +" \n");
} else { // case : with alias
if (j == 0) { // case : first name
ret.append("\t --" + vec[j] + " |");
} else {
if (j == size-1) { //case : last alias
std::string complement;
if (expect)
complement.append(" value");
if (mandatory)
complement.append(" MANDATORY");
if (!defaultval.empty())
complement.append(" (default value : <" + defaultval + ">)");
ret.append(" -" + vec[j] + complement + " \n");
} else { // case : middle alias
ret.append(" -" + vec[j] + " |");
}
}
}
}
result.append(ret);
}
stream << "Usage :\n" << result;
}
std::size_t Parser::getPositionalArgumentCount() const {
return positionalArguments.size();
}
Option& Parser::operator()(const std::string& name) {
if (name == "help" || name == "h") {
throw std::runtime_error("help and h cannot be redefined");
}
for(auto& option : Parser::options){
std::vector<std::string> names = option.getNames();
if (std::find(names.begin(), names.end(), name) != names.end()){
return option;
}
}
// Option not stored -> add
op::Option opt(name);
Parser::options.push_back(opt);
return Parser::options[options.size()-1];
}
const std::string& Parser::operator[](std::size_t i) const {
auto count = Parser::getPositionalArgumentCount();
if(i >= count){
throw std::out_of_range("operator[]");
}
return positionalArguments[i];
}
bool Parser::isOption(const std::string& name){
for(auto& option : Parser::options){
std::vector<std::string> names = option.getNames();
if (std::find(names.begin(), names.end(), name) != names.end()){
return true;
}
}
return false;
}
Option& Parser::getOption(const std::string& name){
for(auto& option : Parser::options){
std::vector<std::string> names = option.getNames();
if (std::find(names.begin(), names.end(), name) != names.end()){
return option;
}
}
throw std::runtime_error("no option with this name");
}
}