-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineArguments.cpp
More file actions
138 lines (120 loc) · 3.04 KB
/
CommandLineArguments.cpp
File metadata and controls
138 lines (120 loc) · 3.04 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
#include "CommandLineArguments.h"
void handleOptions(int argc, char* argv[], std::vector<Option*> opttable) {
if (argc <= 1) {
std::cout << "No arguments found!\n";
}
else {
bool errflag = true;
//stores argument that couldn't be parsed
std::vector <std::string> errtable;
//std::cout << argc << '\n';
//check each argument sequentially
for (int i = 1; i < argc; i++) {
//std::cout << i << " in main loop\n";
errflag = true;
//compare each argument with the list of options
for (auto j : opttable) {
//make sure the option hasn't already been set
if (!j->getOptFlag()) {
if (j->testString(argv[i])) {
errflag = false;
if (j->hasArg()) {
i++;
//std::cout << i << " in secondary loop\n";
//checks for arguments if the option has them
if (i < argc) {
//if the argument isn't an option
if (argv[i][0] != '-') {
j->setArgument(argv[i]);
}
else {
i--;
break;
}
}
else {
break;
}
}
break;
}
}
}
if (errflag) {
errtable.push_back(argv[i]);
}
}
if (errtable.size() != 0) {
std::cout << "The following arguments could not be parsed:\n";
for (auto i : errtable) {
std::cout << i << "\n";
}
}
}
}
//loads names for options
//'s' is the short option name
//'l' is the long option name
//the strings will be formatted automatically if no -- is included
//set 'a' to true to include an argument with the option
//pass a value to d to give the argument a default value
Option::Option(char s, std::string l, bool a, std::string odescription, std::string adescription, std::string d) {
std::stringstream ss;
ss << '-' << s;
ss >> shortopt;
ss.clear();
if (l.at(1) != '-') {
if (l.at(0) != '-') {
ss << "-";
}
ss << "-" << l;
ss >> longopt;
}
optdescription = odescription;
argdescription = adescription;
arg = d;
hasarg = a;
optflag = false;
}
//gives a short description of what the option does
void Option::describeOption() {
std::cout << shortopt << ", " << longopt << '\n';
std::cout << optdescription << '\n';
if (argdescription != "") {
std::cout << argdescription << "\n";
}
if (argIsDefined()) {
std::cout << "(Default argument: " << arg << ")\n";
}
std::cout << '\n';
}
//checks string for compatibility with command line argument
bool Option::testString(std::string compare) {
for (auto i = compare.begin(); i != compare.end(); i++) {
*i = tolower(*i);
}
optflag = (shortopt == compare || longopt == compare);
if (optflag) {
std::cout << "Match found!\n";
}
else {
std::cout << "Match not found!\n";
}
return optflag;
}
bool Option::getOptFlag() {
return optflag;
}
bool Option::hasArg() {
return hasarg;
}
void Option::setArgument(std::string a) {
arg = a;
argflag = true;
}
std::string Option::getArgument() {
return arg;
}
bool Option::argIsDefined() {
return (arg != "");
}