-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCommandLine.m
267 lines (233 loc) · 8.85 KB
/
CommandLine.m
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
#import "CommandLine.h" //See the header file for comments
@interface CommandLine (NonPublicMethods)
- (NSString *) cL_createCommandLineString: (int)argc andArgv: (char *[])argv;
- (BOOL) cL_parseOptionsAtOffset: (int)optionBegin endExclusive: (int)switchEnd;
- (void) cL_parseParameter: (int)optionBegin endExclusive: (int)switchEnd;
- (BOOL) cL_parsePostFlight;
- (void) cL_resultSet: (BOOL)success message: (NSString *)message;
- (void) cL_resultReset;
@end
@implementation CommandLine
- (CommandLine *)init {
self = [super init];
parsedCommandLine = [NSMutableDictionary dictionaryWithCapacity: CL_MAXOPTIONS];
[self cL_resultReset];
parameterCount = 0;
return self;
}
- (CommandLine *)initWithCommandLine: (NSString *)commandLineString {
self = [self init];
self.commandLine = commandLineString;
return self;
}
- (CommandLine *)initWithArgc: (int)argc andArgv: (char *[])argv {
self = [self init];
self.commandLine = [self cL_createCommandLineString: argc andArgv: argv];
return self;
}
- (void)cL_setOptionsMatrix: (NSDictionary *)matrix andMinNumberOfParams: (int) numberOfParams {
minNumberOfParams = numberOfParams;
self.optionsMatrix = matrix;
}
- (BOOL) cL_parse {
BOOL parseSuccess = YES;
char clChar;
int clPointer = 0;
int optionBegin = 0;
int parameterBegin = 0;
int state = CL_STATE_WHITE;
commandLine = [NSString stringWithFormat: @"%@ ", commandLine];
commandLineStr = commandLine;
int clLength = [commandLine length];
//NSLog(@"ParameterCount %d",clLength);
//NSLog(@"ParameterCount %@",commandLine);
while ((clPointer < clLength) && parseSuccess) {
clChar = [commandLine characterAtIndex: clPointer];
switch (clChar) {
case CL_BEGIN_OPTION:
if (state == CL_STATE_OPTION) {
[self cL_resultSet: FALSE message: [NSString stringWithFormat: @"%@ %c", CL_ERRORMESSAGE_SYNTAX_ERROR, clChar]];
parseSuccess = NO;
}
if (state == CL_STATE_WHITE) {
state = CL_STATE_OPTION;
clPointer++;
optionBegin = clPointer;
}
break;
case CL_SPACE:
case CL_T:
case CL_N:
if (state == CL_STATE_OPTION) {
parseSuccess = [self cL_parseOptionsAtOffset: optionBegin endExclusive: clPointer];
}
if (state == CL_STATE_PARAMETER) {
[self cL_parseParameter: parameterBegin endExclusive: clPointer];
}
state = CL_STATE_WHITE;
clPointer++;
break;
default:
if (state == CL_STATE_WHITE) {
state = CL_STATE_PARAMETER;
parameterBegin = clPointer;
}
clPointer++;
}
}
if (parseSuccess && (parameterCount < minNumberOfParams)) {
[self cL_resultSet: FALSE message: CL_ERRORMESSAGE_PARAMS_MISSING];
parseSuccess = NO;
}
if (parseSuccess) {
parseSuccess = [self cL_parsePostFlight];
}
return parseSuccess;
}
- (BOOL) cL_optionIsSet: (NSString *) option {
BOOL returnValue = NO;
for (NSString *key in self.parsedCommandLine) {
if (![key compare: option]) {
returnValue = YES;
break;
}
}
return returnValue;
}
- (NSString *) cL_optionGetValue: (NSString *) option {
NSString *returnValue;
for (NSString *key in self.parsedCommandLine) {
if (![key compare: option]) {
returnValue = [self.parsedCommandLine valueForKey: key];
break;
}
}
return returnValue;
}
- (NSString *) cL_parameterGetValue: (int) parameter {
NSString *parameterString = [NSString stringWithFormat: @"%d", parameter];
return [self cL_optionGetValue: parameterString];
}
@synthesize optionsMatrix;
@synthesize commandLine;
@synthesize commandLineStr;
@synthesize parsedCommandLine;
@synthesize lastParsedOption;
@synthesize resultSuccess;
@synthesize resultText;
@end
@implementation CommandLine (NonPublicMethods)
- (NSString *)cL_createCommandLineString: (int) argc andArgv: (char *[]) argv {
NSString *commandLineString = nil;
int i = 0;
while (i < argc) {
if (i == 1) {
commandLineString = [NSString stringWithFormat: @"%s", argv[1]];
} else if (i > 0) {
commandLineString = [NSString stringWithFormat: @"%@ %s", commandLineString, argv[i]];
}
i++;
}
return commandLineString;
}
- (void) cL_parseParameter: (int) optionBegin endExclusive: (int) switchEnd {
NSRange range = {optionBegin, switchEnd - optionBegin};
NSString *parameter = [self.commandLine substringWithRange: range];
BOOL found = NO;
if (lastParsedOption) {
for (NSString *key in optionsMatrix) {
if (![[key description]compare: lastParsedOption] &&
(![(NSString *)[optionsMatrix valueForKey:key]compare: CL_OPTION_REQUIRED_WITH_VALUE]
|| ![(NSString *)[optionsMatrix valueForKey:key]compare: CL_OPTION_OPTIONAL_WITH_VALUE]
)) {
[parsedCommandLine setObject: parameter forKey: lastParsedOption];
self.lastParsedOption = nil;
found = YES;
}
}
}
if (!found) {
NSString *parameterKey = [NSString stringWithFormat: @"%d", ++parameterCount];
[parsedCommandLine setObject: parameter forKey: parameterKey];
}
}
- (BOOL) cL_parseOptionsAtOffset: (int) optionBegin endExclusive: (int) switchEnd {
int clPointer = optionBegin;
char clChar = 0;
BOOL found = NO;
NSString *parsedValue;
if (switchEnd <= clPointer) {
[self cL_resultSet: FALSE message: CL_ERRORMESSAGE_SYNTAX_ERROR];
} else {
while (clPointer < switchEnd) {
found = NO;
clChar = [commandLine characterAtIndex: clPointer];
NSString *switchString = [NSString stringWithFormat: @"%c", clChar];
for (NSString *key in optionsMatrix) {
if (![[key description]compare: switchString]) {
self.lastParsedOption = switchString;
found = YES;
if (![(NSString *)[optionsMatrix valueForKey: key] compare: CL_OPTION_REQUIRED_WITH_VALUE]
|| ![(NSString *)[optionsMatrix valueForKey: key] compare: CL_OPTION_OPTIONAL_WITH_VALUE]
) {
parsedValue = CL_OPTION_VALUE_EXPECTED;
} else {
parsedValue = CL_OPTION_VALUE_FOUND;
}
}
}
if (!found) {
if (!clChar) {
[self cL_resultSet: FALSE message: CL_ERRORMESSAGE_SYNTAX_ERROR];
} else {
[self cL_resultSet: FALSE message: [NSString stringWithFormat: @"%@ %@", CL_ERRORMESSAGE_UNKNOWN_OPTION, switchString]];
}
break;
} else {
[parsedCommandLine setObject: parsedValue forKey: switchString];
}
clPointer++;
}
}
return found;
}
- (BOOL) cL_parsePostFlight {
BOOL parseSuccess = YES;
BOOL found;
NSString *optionValue, *optionType;
for (NSString *keyInMatrix in self.optionsMatrix) {
optionType = [self.optionsMatrix valueForKey: keyInMatrix];
found = NO;
for (NSString *keyInCommandLine in self.parsedCommandLine) {
if (![keyInMatrix compare: keyInCommandLine]) {
found = YES;
optionValue = [self.parsedCommandLine valueForKey: keyInCommandLine];
if (
(![optionType compare: CL_OPTION_REQUIRED_WITH_VALUE]
|| ![optionType compare: CL_OPTION_OPTIONAL_WITH_VALUE])
&& ![optionValue compare: CL_OPTION_VALUE_EXPECTED]
) {
parseSuccess = NO;
[self cL_resultSet: parseSuccess message: [NSString stringWithFormat: @"%@ %@", CL_ERRORMESSAGE_MISSING_VALUE, keyInCommandLine]];
break;
}
}
}
if (!found
&& (![optionType compare: CL_OPTION_REQUIRED_WITH_VALUE] || ![optionType compare: CL_OPTION_REQUIRED])
) {
parseSuccess = NO;
[self cL_resultSet: parseSuccess message: [NSString stringWithFormat: @"%@ %@", CL_ERRORMESSAGE_OPTION_NOT_FOUND, keyInMatrix]];
}
}
return parseSuccess;
}
- (void) cL_resultReset {
self.resultSuccess = YES;
self.resultText = nil;
}
- (void) cL_resultSet: (BOOL) success message: (NSString *) message {
self.resultText = message;
self.resultSuccess = success;
}
@end