forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.cpp
392 lines (332 loc) · 9.04 KB
/
cmdline.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "cmdline.h"
#include <util/edit_distance.h>
#include <util/exception_utils.h>
#include <util/invariant.h>
#include <util/string_utils.h>
cmdlinet::cmdlinet()
{
}
cmdlinet::~cmdlinet()
{
}
void cmdlinet::clear()
{
options.clear();
args.clear();
}
bool cmdlinet::isset(char option) const
{
auto i=getoptnr(option);
if(i.has_value())
return options[*i].isset;
else
return false;
}
bool cmdlinet::isset(const char *option) const
{
auto i=getoptnr(option);
if(i.has_value())
return options[*i].isset;
else
return false;
}
std::string cmdlinet::get_value(char option) const
{
auto i=getoptnr(option);
if(i.has_value() && !options[*i].values.empty())
return options[*i].values.front();
else
return "";
}
void cmdlinet::set(const std::string &option, bool value)
{
auto i=getoptnr(option);
if(i.has_value())
options[*i].isset = value;
else
{
throw invalid_command_line_argument_exceptiont(
"unknown command line option", option);
}
}
void cmdlinet::set(const std::string &option, const std::string &value)
{
auto i=getoptnr(option);
if(i.has_value())
{
options[*i].isset=true;
options[*i].values.push_back(value);
}
else
{
throw invalid_command_line_argument_exceptiont(
"unknown command line option", option);
}
}
static std::list<std::string> immutable_empty_list;
const std::list<std::string> &cmdlinet::get_values(char option) const
{
auto i=getoptnr(option);
if(i.has_value())
return options[*i].values;
else
return immutable_empty_list;
}
std::string cmdlinet::get_value(const char *option) const
{
auto i=getoptnr(option);
if(i.has_value() && !options[*i].values.empty())
return options[*i].values.front();
else
return "";
}
const std::list<std::string> &cmdlinet::get_values(
const std::string &option) const
{
auto i=getoptnr(option);
if(i.has_value())
return options[*i].values;
else
return immutable_empty_list;
}
std::list<std::string>
cmdlinet::get_comma_separated_values(const char *option) const
{
std::list<std::string> separated_values;
for(const auto &csv : get_values(option))
{
const auto values = split_string(csv, ',');
separated_values.insert(
separated_values.end(), values.begin(), values.end());
}
return separated_values;
}
optionalt<std::size_t> cmdlinet::getoptnr(char option) const
{
for(std::size_t i=0; i<options.size(); i++)
if(options[i].optchar==option)
return i;
return optionalt<std::size_t>();
}
optionalt<std::size_t> cmdlinet::getoptnr(const std::string &option) const
{
for(std::size_t i=0; i<options.size(); i++)
if(options[i].optstring==option)
return i;
return optionalt<std::size_t>();
}
bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
{
clear();
parse_optstring(optstring);
return parse_arguments(argc, argv);
}
cmdlinet::option_namest cmdlinet::option_names() const
{
return option_namest{*this};
}
void cmdlinet::parse_optstring(const char *optstring)
{
while(optstring[0] != 0)
{
optiont option;
DATA_INVARIANT(
optstring[0] != ':', "cmdlinet::parse: Invalid option string\n");
if(optstring[0] == '(')
{
option.islong = true;
option.optchar = 0;
option.isset = false;
option.optstring.clear();
for(optstring++; optstring[0] != ')' && optstring[0] != 0; optstring++)
option.optstring += optstring[0];
if(optstring[0] == ')')
optstring++;
}
else
{
option.islong = false;
option.optchar = optstring[0];
option.optstring.clear();
option.isset = false;
optstring++;
}
if(optstring[0] == ':')
{
option.hasval = true;
optstring++;
}
else
option.hasval = false;
options.push_back(option);
}
}
std::vector<std::string>
cmdlinet::get_argument_suggestions(const std::string &unknown_argument)
{
struct suggestiont
{
std::size_t distance;
std::string suggestion;
bool operator<(const suggestiont &other) const
{
return distance < other.distance;
}
};
auto argument_suggestions = std::vector<suggestiont>{};
// We allow 3 errors here. This can lead to the output being a bit chatty,
// which we mitigate by reducing suggestions to those with the minimum
// distance further down below
const auto argument_matcher = levenshtein_automatont{unknown_argument, 3};
for(const auto &option : options)
{
if(option.islong)
{
const auto long_name = "--" + option.optstring;
if(auto distance = argument_matcher.get_edit_distance(long_name))
{
argument_suggestions.push_back({distance.value(), long_name});
}
}
if(!option.islong)
{
const auto short_name = std::string{"-"} + option.optchar;
if(auto distance = argument_matcher.get_edit_distance(short_name))
{
argument_suggestions.push_back({distance.value(), short_name});
}
}
}
auto final_suggestions = std::vector<std::string>{};
if(!argument_suggestions.empty())
{
// we only want to keep suggestions with the minimum distance
// because otherwise they become quickly too noisy to be useful
auto min = std::min_element(
argument_suggestions.begin(), argument_suggestions.end());
INVARIANT(
min != argument_suggestions.end(),
"there is a minimum because it's not empty");
for(auto const &suggestion : argument_suggestions)
{
if(suggestion.distance == min->distance)
{
final_suggestions.push_back(suggestion.suggestion);
}
}
}
return final_suggestions;
}
bool cmdlinet::parse_arguments(int argc, const char **argv)
{
for(int i = 1; i < argc; i++)
{
if(argv[i][0] != '-')
args.push_back(argv[i]);
else
{
optionalt<std::size_t> optnr;
if(argv[i][1] != 0 && argv[i][2] == 0)
optnr = getoptnr(argv[i][1]); // single-letter option -X
else if(argv[i][1] == '-')
optnr = getoptnr(argv[i] + 2); // multi-letter option with --XXX
else
{
// Multi-letter option -XXX, or single-letter with argument -Xval
// We first try single-letter.
optnr = getoptnr(argv[i][1]);
if(!optnr.has_value()) // try multi-letter
optnr = getoptnr(argv[i] + 1);
}
if(!optnr.has_value())
{
unknown_arg = argv[i];
return true;
}
options[*optnr].isset = true;
if(options[*optnr].hasval)
{
if(argv[i][2] == 0 || options[*optnr].islong)
{
i++;
if(i == argc)
return true;
if(argv[i][0] == '-' && argv[i][1] != 0)
return true;
options[*optnr].values.push_back(argv[i]);
}
else
options[*optnr].values.push_back(argv[i] + 2);
}
}
}
return false;
}
cmdlinet::option_namest::option_names_iteratort::option_names_iteratort(
const cmdlinet *command_line,
std::size_t index)
: command_line(command_line), index(index)
{
goto_next_valid_index();
}
cmdlinet::option_namest::option_names_iteratort &
cmdlinet::option_namest::option_names_iteratort::operator++()
{
PRECONDITION(command_line != nullptr);
++index;
goto_next_valid_index();
return *this;
}
bool cmdlinet::option_namest::option_names_iteratort::is_valid_index() const
{
PRECONDITION(command_line != nullptr);
auto const &options = command_line->options;
return index < options.size() && options[index].isset &&
options[index].islong;
}
void cmdlinet::option_namest::option_names_iteratort::goto_next_valid_index()
{
PRECONDITION(command_line != nullptr);
while(index < command_line->options.size() && !is_valid_index())
{
++index;
}
}
const cmdlinet::option_namest::option_names_iteratort
cmdlinet::option_namest::option_names_iteratort::operator++(int dummy)
{
return ++option_names_iteratort(*this);
}
const std::string &cmdlinet::option_namest::option_names_iteratort::operator*()
{
PRECONDITION(command_line != nullptr);
return command_line->options.at(index).optstring;
}
bool cmdlinet::option_namest::option_names_iteratort::
operator==(const cmdlinet::option_namest::option_names_iteratort &other)
{
PRECONDITION(command_line != nullptr && command_line == other.command_line);
return index == other.index;
}
bool cmdlinet::option_namest::option_names_iteratort::
operator!=(const cmdlinet::option_namest::option_names_iteratort &other)
{
PRECONDITION(command_line != nullptr && command_line == other.command_line);
return index != other.index;
}
cmdlinet::option_namest::option_namest(const cmdlinet &command_line)
: command_line(command_line)
{
}
cmdlinet::option_namest::option_names_iteratort cmdlinet::option_namest::begin()
{
return option_names_iteratort(&command_line, 0);
}
cmdlinet::option_namest::option_names_iteratort cmdlinet::option_namest::end()
{
return option_names_iteratort(&command_line, command_line.options.size());
}