-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOption.cc
More file actions
206 lines (196 loc) · 8.28 KB
/
Option.cc
File metadata and controls
206 lines (196 loc) · 8.28 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Option.h"
#include "misc.h"
#include <cstdlib>
//#ifndef report
//#define report printf
//#endif
// OptionParser works like getopt. That is, the second argument to the
// constructor specifies the valid one-character options. A character
// followed by a colon indicates that it has a required argument. A
// character followed by two colons indicates that it has an optional
// argument. The special string '--' terminates option processing.
// Example:
// OptionParser opt("/window -w25 -h15 -x0 -y0 -b Joe", "w:h:x:y:bfW::")
// indicates the options -w,-h,-x,-y must have arguments (specified either
// like "-w25" or "-w 25"), -b,-f are "boolean", and W can take an optional
// argument.
// Find -- or = to terminate options (must have spaces on both sides).
// Alternatively:
// while(s =~ /\s*(?:-([A-Za-z]))?([\'\"])?(\w+)\2/) {
// if($1) { $options{$1} = $3?$3:1; }
// }
// Ok, this turned out much uglier than I anticipated.
OptionParser::OptionParser(const string& _s, const string& _options)
: s(_s), optionStr(_options), isvalid(true)
{
unsigned int start = 0;
unsigned int pos = 0;
unsigned int endoptpos = 0; // last option
unsigned int argstart = 0;
int numbs = 0;
bool newarg = true; // looking for a new argument
bool inq = false; // in single quotes (')
bool inqq = false; // in double quotes (")
bool lookforopt = true; // haven't found a -- or = yet.
bool needarg = false; // need mandatory argument
bool needoptarg = false;// need optional argument
bool isoptchar = false; // this character is an option char
bool lastwasopt = false;// was the last agument an option?
char optchar = '\0';
while(pos < s.length()) {
char thisc = _s[pos];
if(thisc == '\\') { // FIXME what if the string starts with \?
numbs++;
} else {
if(thisc == '\n' || thisc == '\r' || thisc == '\t' || thisc == ' ') {
if(!(inq || inqq || numbs%2 == 1 || newarg)) {
if(needarg || needoptarg) {
options[optchar] = debackslashify(s.substr(start,pos-start));
lastwasopt = true;
needarg = needoptarg = false;
newarg = true;
}
else if(!(isoptchar || newarg)) {
nonOptions.push_back(debackslashify(s.substr(start,pos-start)));
lastwasopt = false;
newarg = true;
}
if(isoptchar) { newarg = true; isoptchar = false; lastwasopt = true; }
}
} else {
if(newarg) {
newarg = false;
start = pos;
if(lastwasopt) endoptpos = start;
if(!argstart && pos) argstart = pos; // grab the first argument
}
if(thisc == '\'') {
if(numbs%2 == 0 && !inqq) {
if(needarg || needoptarg) {
if(inq) {
options[optchar] = debackslashify(s.substr(start+1, pos-start-1)); // strip ""
lastwasopt = true;
needarg = needoptarg = false;
newarg = true;
} else {
start = pos;
}
} else if(inq) {
nonOptions.push_back(debackslashify(s.substr(start+1,pos-start-1)));
lastwasopt = false;
newarg = true;
}
inq = !inq;
}
isoptchar = false;
} else if(thisc == '\"') {
if(numbs%2 == 0 && !inq) {
if(needarg || needoptarg) {
if(inqq) {
options[optchar] = debackslashify(s.substr(start+1, pos-start-1)); // strip ''
lastwasopt = true;
needarg = needoptarg = false;
newarg = true;
} else {
start = pos;
}
} else if(inqq) {
nonOptions.push_back(debackslashify(s.substr(start+1,pos-start-1)));
lastwasopt = false;
newarg = true;
}
inqq = !inqq;
}
isoptchar = false;
} else if(thisc == '-') {
if(!(inq || inqq || numbs%2 == 1) && (start == pos) && lookforopt) {
if(isoptchar) {
lookforopt = false; // found --
isoptchar = false;
newarg = true;
} else if(!needarg) { // - can be part of option. i.e. -x -41
isoptchar = true;
newarg = false;
} // next char is option char.
}
} else if(thisc == '=') { // Terminates option processing.
if(!(inq || inqq || numbs%2 == 1) && lookforopt) {
start = pos;
lookforopt = false;
newarg = false;
}
} else { // thisc is a regular 'ol character.
if(newarg) {
newarg = false;
start = pos;
}
if(!(inq || inqq) && isoptchar) {
optchar = thisc;
size_t optloc = optionStr.find_first_of(optchar);
if(optloc == string::npos) {
report_err("Unknown option found for command %s -%c\n", nonOptions[0].c_str(), optchar);
isvalid = false;
} else if(optionStr[optloc+1] == ':') {
if(optionStr[optloc+2] == ':') {
needoptarg = true;
isoptchar = false;
} else {
needarg = true;
isoptchar = false;
}
newarg = true;
} else {
options[optchar] = "1";
}
}
}
}
numbs = 0;
}
pos++;
} // Scan to next argument
if(inq || inqq) {
report_err("%s Syntax Error: unmatched %c.\n", nonOptions[0].c_str(), inq?'\'':'"');
isvalid = false;
}
if(s.length() && !(inq || inqq || numbs)) {
if(!(inq || inqq || numbs%2 == 1 || newarg)) {
if(needarg || needoptarg) {
options[optchar] = debackslashify(s.substr(start,pos-start));
lastwasopt = true;
needarg = needoptarg = false;
newarg = true;
}
else if(!(isoptchar || newarg)) {
nonOptions.push_back(debackslashify(s.substr(start,pos-start)));
lastwasopt = false;
newarg = true;
}
if(isoptchar) { newarg = true; isoptchar = false; }
}
}
numbs = 0;
if(needarg) {
report_err("%s: Mandatory value for option -%c not found\n", nonOptions[0].c_str(), optchar);
isvalid = false;
}
if(!endoptpos) endoptpos = argstart;
s_nonopt = s.substr(endoptpos, s.length()-endoptpos);
if(!isvalid) {
report_err("Command with errors was:\n\t%s", s.c_str());
}
}
// Returns the non-arguments part of the string, EXCLUDING argv[0]
string OptionParser::restStr()
{
// nonOptions[0] is the /command.
if(nonOptions.size() == 2) return nonOptions[1];
if(nonOptions.size() == 1) return "";
else return s_nonopt;
}
// collapse escaped things.
string OptionParser::arg(unsigned int i)
{
if(i < nonOptions.size()) return nonOptions[i];
else return "";
}