-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineArguments.cs
244 lines (193 loc) · 5.03 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
namespace BefunExec
{
public class CommandLineArguments
{
private StringDictionary Parameters;
public CommandLineArguments(string[] args)
{
Parameters = new StringDictionary();
Regex splitter = new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex remover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string parameter = null;
foreach (string Txt in args)
{
var parts = splitter.Split(Txt, 3);
switch (parts.Length)
{
case 1:
if (parameter != null)
{
if (!Parameters.ContainsKey(parameter))
{
parts[0] = remover.Replace(parts[0], "$1");
Parameters.Add(parameter, parts[0]);
}
parameter = null;
}
break;
case 2:
if (parameter != null)
{
if (!Parameters.ContainsKey(parameter))
Parameters.Add(parameter, "true");
}
parameter = parts[1];
break;
case 3:
if (parameter != null)
{
if (!Parameters.ContainsKey(parameter))
Parameters.Add(parameter, "true");
}
parameter = parts[1];
if (!Parameters.ContainsKey(parameter))
{
parts[2] = remover.Replace(parts[2], "$1");
Parameters.Add(parameter, parts[2]);
}
parameter = null;
break;
}
}
if (parameter != null)
{
if (!Parameters.ContainsKey(parameter))
Parameters.Add(parameter, "true");
}
}
public bool Contains(string key)
{
return Parameters.ContainsKey(key);
}
public bool IsSet(string key)
{
return Parameters.ContainsKey(key) && Parameters[key] != null;
}
public string this[string param]
{
get
{
return (Parameters[param]);
}
}
public bool isEmpty()
{
return Parameters.Count == 0;
}
public int Count()
{
return Parameters.Count;
}
#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 string[] { delimiter }, options).ToList();
else
return null;
}
#endregion
#region Long
public bool IsLong(string p)
{
long a;
return IsSet(p) && long.TryParse(Parameters[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(Parameters[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(Parameters[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
}
}