-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLineArguments.cs
376 lines (295 loc) · 8.25 KB
/
CommandLineArguments.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace BefunRep
{
public class CommandLineArguments
{
private readonly Dictionary<string, string> paramDict;
public CommandLineArguments(string[] args, bool caseSensitive = true)
{
if (caseSensitive)
paramDict = new Dictionary<string, string>(StringComparer.CurrentCulture);
else
paramDict = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
var rexSplitter = new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
var rexRemover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string parameter = null;
foreach (string txt in args)
{
var parts = rexSplitter.Split(txt, 3);
switch (parts.Length)
{
case 1:
if (parameter != null)
{
if (!paramDict.ContainsKey(parameter))
{
parts[0] = rexRemover.Replace(parts[0], "$1");
paramDict.Add(parameter, parts[0]);
}
parameter = null;
}
break;
case 2:
if (parameter != null)
{
if (!paramDict.ContainsKey(parameter))
paramDict.Add(parameter, "true");
}
parameter = parts[1];
break;
case 3:
if (parameter != null)
{
if (!paramDict.ContainsKey(parameter))
paramDict.Add(parameter, "true");
}
parameter = parts[1];
if (!paramDict.ContainsKey(parameter))
{
parts[2] = rexRemover.Replace(parts[2], "$1");
paramDict.Add(parameter, parts[2]);
}
parameter = null;
break;
}
}
if (parameter != null)
{
if (!paramDict.ContainsKey(parameter))
paramDict.Add(parameter, "true");
}
}
public bool Contains(string key)
{
return paramDict.ContainsKey(key);
}
public bool IsSet(string key)
{
return paramDict.ContainsKey(key) && paramDict[key] != null;
}
public string this[string param] => (paramDict[param]);
public bool IsEmpty()
{
return paramDict.Count == 0;
}
#region String
public string GetStringDefault(string p, string def)
{
return Contains(p) ? this[p] : def;
}
public List<string> GetStringList(string p, string delimiter, StringSplitOptions options = StringSplitOptions.None)
{
if (Contains(p))
return this[p].Split(new[] { delimiter }, options).ToList();
else
return null;
}
#endregion
#region Long
public bool IsLong(string p)
{
long a;
return IsSet(p) && long.TryParse(paramDict[p], out a);
}
public long GetLong(string p)
{
return long.Parse(this[p]);
}
public long GetLongDefault(string p, long def)
{
return IsLong(p) ? GetLong(p) : def;
}
public long? GetLongDefaultNull(string p)
{
return IsLong(p) ? GetLong(p) : (long?)null;
}
public long GetLongDefaultRange(string p, long def, long min, long max)
{
return Math.Min(max - 1, Math.Max(min, (IsLong(p) ? GetLong(p) : def)));
}
public List<long> GetLongList(string p, string delimiter, bool sanitize = false)
{
List<String> ls = GetStringList(p, delimiter, sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
long aout;
if (ls.Any(pp => !long.TryParse(pp, out aout)))
return null;
return ls.Select(long.Parse).ToList();
}
#endregion
#region Integer
public bool IsInt(string p)
{
int a;
return IsSet(p) && int.TryParse(paramDict[p], out a);
}
public int GetInt(string p)
{
return int.Parse(this[p]);
}
public int GetIntDefault(string p, int def)
{
return IsInt(p) ? GetInt(p) : def;
}
public int? GetIntDefaultNull(string p)
{
return IsInt(p) ? GetInt(p) : (int?)null;
}
public int GetIntDefaultRange(string p, int def, int min, int max)
{
return Math.Min(max - 1, Math.Max(min, (IsInt(p) ? GetInt(p) : def)));
}
public List<int> GetIntList(string p, string delimiter, bool sanitize = false)
{
List<String> ls = GetStringList(p, delimiter, sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
int aout;
if (ls.Any(pp => !int.TryParse(pp, out aout)))
return null;
return ls.Select(int.Parse).ToList();
}
#endregion
#region UInteger
public bool IsUInt(string p)
{
uint a;
return IsSet(p) && uint.TryParse(paramDict[p], out a);
}
public uint GetUInt(string p)
{
return uint.Parse(this[p]);
}
public uint GetUIntDefault(string p, uint def)
{
return IsUInt(p) ? GetUInt(p) : def;
}
public uint? GetUIntDefaultNull(string p)
{
return IsUInt(p) ? GetUInt(p) : (uint?)null;
}
public uint GetUIntDefaultRange(string p, uint def, uint min, uint max)
{
return Math.Min(max - 1, Math.Max(min, (IsUInt(p) ? GetUInt(p) : def)));
}
public List<uint> GetUIntList(string p, string delimiter, bool sanitize = false)
{
List<String> ls = GetStringList(p, delimiter, sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
uint aout;
if (ls.Any(pp => !uint.TryParse(pp, out aout)))
return null;
return ls.Select(uint.Parse).ToList();
}
#endregion
#region Enum
private bool TryParseEnum<T>(string input, out T value) where T : struct, IConvertible
{
T result;
if (Enum.TryParse(input, true, out result))
{
value = result;
return true;
}
int resultOrd;
if (int.TryParse(input, out resultOrd))
{
value = (T)Enum.ToObject(typeof(T), resultOrd);
return true;
}
value = default(T);
return false;
}
private T ParseEnum<T>(string p) where T : struct, IConvertible
{
T value;
if (TryParseEnum(p, out value))
return value;
throw new FormatException(string.Format("The value {0} is not a valid enum member", p));
}
public bool IsEnum<T>(string p) where T : struct, IConvertible
{
if (!IsSet(p)) return false;
T tmp;
return TryParseEnum(p, out tmp);
}
public T GetEnum<T>(string p) where T : struct, IConvertible
{
T value;
if (TryParseEnum(p, out value))
return value;
throw new FormatException(string.Format("The parameter {0} is not a valid enum value", p));
}
public T GetEnumDefault<T>(string p, T def) where T : struct, IConvertible
{
T value;
if (TryParseEnum(p, out value))
return value;
else
return def;
}
public List<T> GetEnumList<T>(string p, string delimiter, bool sanitize = false) where T : struct, IConvertible
{
var ls = GetStringList(p, delimiter, sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
T aout;
if (ls.Any(pp => !TryParseEnum(pp, out aout)))
return null;
return ls.Select(ParseEnum<T>).ToList();
}
#endregion
#region Bool
private bool TryParseBool(string input, bool allowOrdinal, out bool value)
{
if (input.ToLower() == "true")
{
value = true;
return true;
}
if (input.ToLower() == "false")
{
value = true;
return true;
}
int resultOrd;
if (allowOrdinal && int.TryParse(input, out resultOrd))
{
value = (resultOrd != 0);
return true;
}
value = default(bool);
return false;
}
private bool ParseBool(string p, bool allowOrdinal = false)
{
bool value;
if (TryParseBool(p, allowOrdinal, out value))
return value;
throw new FormatException(string.Format("The value {0} is not a valid boolean value", p));
}
public bool IsBool(string p, bool allowOrdinal = false)
{
bool a;
return IsSet(p) && TryParseBool(paramDict[p], allowOrdinal, out a);
}
public bool GetBool(string p, bool allowOrdinal = false)
{
return ParseBool(this[p], allowOrdinal);
}
public bool GetBoolDefault(string p, bool def, bool allowOrdinal = false)
{
return IsBool(p, allowOrdinal) ? GetBool(p, allowOrdinal) : def;
}
public bool? GetBoolDefaultNull(string p, bool allowOrdinal = false)
{
return IsBool(p, allowOrdinal) ? GetBool(p, allowOrdinal) : (bool?)null;
}
public List<bool> GetBoolList(string p, string delimiter, bool allowOrdinal = false, bool sanitize = false)
{
var ls = GetStringList(p, delimiter, sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
bool aout;
if (ls.Any(pp => !TryParseBool(pp, allowOrdinal, out aout)))
return null;
return ls.Select(e => ParseBool(e, allowOrdinal)).ToList();
}
#endregion
}
}