-
Notifications
You must be signed in to change notification settings - Fork 12
/
CommandLineParser.cs
199 lines (190 loc) · 6.59 KB
/
CommandLineParser.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cnpl
{
class CommandLineParser
{
Dictionary<string, string> mKeyValuePairs = new Dictionary<string, string>();
public void Parse(string[] args)
{
var sb = new StringBuilder(1204);
foreach (var arg in args)
{
sb.Append(arg);
sb.Append(" ");
}
int state = 0;
string name = string.Empty;
string value = string.Empty;
char valueStart = '\0';
foreach (var ch in sb.ToString())
{
switch(ch)
{
case '/':
case '-':
{
if (state == 0)
{
state = 1;
}
else if(state == 3)
{
if (ch != valueStart)
value += ch;
else
{
mKeyValuePairs[name] = value;
name = string.Empty;
value = string.Empty;
state = 1;
}
}
else
{
mKeyValuePairs[name] = value;
name = string.Empty;
value = string.Empty;
state = 1;
}
}
break;
case ':':
case '=':
case ' ':
{
if (state == 1)
state = 2;
else if (state == 3)
{
if (ch != valueStart)
value += ch;
else
{
mKeyValuePairs[name] = value;
name = string.Empty;
value = string.Empty;
state = 0;
}
}
}
break;
default:
{
if(state==1)
{
name += ch;
}
else if(state == 2)
{
if (ch == '\'' || ch == '"')
{
state = 3;
valueStart = ch;
}
else
{
state = 3;
valueStart = ' ';
value += ch;
}
}
else if (state == 3)
{
if (ch != valueStart)
value += ch;
else
{
mKeyValuePairs[name] = value;
name = string.Empty;
value = string.Empty;
state = 0;
}
}
}
break;
}
}
if (!string.IsNullOrEmpty(name))
{
mKeyValuePairs[name] = value;
name = string.Empty;
value = string.Empty;
}
}
public bool Has(string name)
{
return mKeyValuePairs.ContainsKey(name);
}
public int GetValue(string name, int defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
int result;
if (int.TryParse(value, out result))
return result;
return defaultValue;
}
public uint GetValue(string name, uint defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
uint result;
if (uint.TryParse(value, out result))
return result;
return defaultValue;
}
public long GetValue(string name, long defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
long result;
if (long.TryParse(value, out result))
return result;
return defaultValue;
}
public ulong GetValue(string name, ulong defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
ulong result;
if (ulong.TryParse(value, out result))
return result;
return defaultValue;
}
public float GetValue(string name, float defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
float result;
if (float.TryParse(value, out result))
return result;
return defaultValue;
}
public double GetValue(string name, double defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
double result;
if (double.TryParse(value, out result))
return result;
return defaultValue;
}
public string GetValue(string name, string defaultValue)
{
string value;
if (!mKeyValuePairs.TryGetValue(name, out value))
return defaultValue;
return value;
}
}
}