-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
330 lines (282 loc) · 12 KB
/
Extensions.cs
File metadata and controls
330 lines (282 loc) · 12 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
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
//
// Original code taken from CommandLineExtensions.cs of the CoApp toolkit.
// Last edited by Tim Rogers - CoApp Project
//
//-----------------------------------------------------------------------
// <copyright company="CoApp Project">
// Original Copyright (c) 2009 Microsoft Corporation. All rights reserved.
// Changes Copyright (c) 2010 Garrett Serack. All rights reserved.
// </copyright>
// <license>
// The software is licensed under the Apache 2.0 License (the "License")
// You may not use the software except in compliance with the License.
// </license>
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// Original Code:
// (c) 2009 Microsoft Corporation -- All rights reserved
// This code is licensed under the MS-PL
// http://www.opensource.org/licenses/ms-pl.html
// Courtesy of the Open Source Techology Center: http://port25.technet.com
// -----------------------------------------------------------------------
namespace GitRepoGenerator{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
/// <summary>
/// Storage Class for complex options from the command line.
/// </summary>
/// <remarks></remarks>
public class ComplexOption {
/// <summary>
///
/// </summary>
public string WholePrefix; // stuff in the []
/// <summary>
///
/// </summary>
public string WholeValue; // stuff after the []
/// <summary>
///
/// </summary>
public List<string> PrefixParameters = new List<string>(); // individual items in the []
/// <summary>
///
/// </summary>
public Dictionary<string, string> Values = new Dictionary<string, string>(); // individual key/values after the []
}
/// <summary>
///
/// </summary>
/// <remarks>
/// NOTE: Explicity Ignore, testing this will produce no discernable value, and will only lead to heartbreak.
/// </remarks>
public static class CommandLineExtensions {
/// <summary>
///
/// </summary>
private static Dictionary<string, IEnumerable<string>> switches;
/// <summary>
///
/// </summary>
private static IEnumerable<string> parameters;
/// <summary>
/// Gets the parameters for switch or null.
/// </summary>
/// <param name="args">The args.</param>
/// <param name="key">The key.</param>
/// <returns></returns>
/// <remarks></remarks>
public static IEnumerable<string> GetParametersForSwitchOrNull(this IEnumerable<string> args, string key) {
if(switches == null)
Switches(args);
if(switches.ContainsKey(key))
return switches[key];
return null;
}
/// <summary>
/// Gets the parameters for switch.
/// </summary>
/// <param name="args">The args.</param>
/// <param name="key">The key.</param>
/// <returns></returns>
/// <remarks></remarks>
public static IEnumerable<string> GetParametersForSwitch(this IEnumerable<string> args,string key) {
if (switches == null)
Switches(args);
if (switches.ContainsKey(key))
return switches[key];
return new List<string>();
}
/// <summary>
/// Switches the value.
/// </summary>
/// <param name="args">The args.</param>
/// <param name="key">The key.</param>
/// <returns></returns>
/// <remarks></remarks>
public static string SwitchValue(this IEnumerable<string> args, string key) {
if(args.Switches().ContainsKey(key))
return args.GetParametersForSwitch(key).FirstOrDefault();
return null;
}
/// <summary>
/// Switcheses the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns></returns>
/// <remarks></remarks>
public static Dictionary<string, IEnumerable<string>> Switches(this IEnumerable<string> args) {
if(switches != null) {
return switches;
}
switches = new Dictionary<string, IEnumerable<string>>();
var argEnumerator = args.GetEnumerator();
//while(firstarg < args.Length && args[firstarg].StartsWith("--")) {
while(argEnumerator.MoveNext() && argEnumerator.Current.StartsWith("--")) {
var arg = argEnumerator.Current.Substring(2).ToLower();
var param = "";
int pos;
if((pos = arg.IndexOf("=")) > -1) {
param = argEnumerator.Current.Substring(pos + 3);
arg = arg.Substring(0, pos);
if(string.IsNullOrEmpty(param) || string.IsNullOrEmpty(arg)) {
Console.WriteLine("Invalid Option :"+argEnumerator.Current.Substring(2).ToLower());
switches.Clear();
switches.Add("help", new List<string>());
return switches;
}
}
if(arg.Equals("load-config")) {
// loads the config file, and then continues parsing this line.
LoadConfiguration(param);
// firstarg++;
continue;
}
if(!switches.ContainsKey(arg)) {
switches.Add(arg, new List<string>());
}
((List<string>)switches[arg]).Add(param);
// firstarg++;
}
return switches;
}
/// <summary>
/// Loads the configuration.
/// </summary>
/// <param name="file">The file.</param>
/// <remarks></remarks>
public static void LoadConfiguration(this string file) {
if(switches == null) {
switches = new Dictionary<string, IEnumerable<string>>();
}
var param = "";
var category = "";
string arg;
int pos;
if(File.Exists(file)) {
var lines = File.ReadAllLines(file);
for(var ln = 0; ln < lines.Length; ln++) {
var line = lines[ln].Trim();
while(line.EndsWith("\\") && ln < lines.Length) {
line = line.Substring(0, line.Length - 1);
if(++ln < lines.Length) {
line += lines[ln].Trim();
}
}
arg = line;
param = "";
if(arg.IndexOf("[") == 0) {
// category
category = arg.Substring(1, arg.IndexOf(']')-1).Trim();
continue;
}
if(string.IsNullOrEmpty(arg) || arg.StartsWith(";") || arg.StartsWith("#")) // comments
{
continue;
}
if(!string.IsNullOrEmpty(category))
arg = category+"-"+arg;
if((pos = arg.IndexOf("=")) > -1) {
param = arg.Substring(pos + 1);
arg = arg.Substring(0, pos).ToLower();
if(string.IsNullOrEmpty(param) || string.IsNullOrEmpty(arg)) {
Console.WriteLine("Invalid Option in config file ["+file+"]: "+line.Trim());
switches.Add("help", new List<string>());
return;
}
}
if(!switches.ContainsKey(arg)) {
switches.Add(arg, new List<string>());
}
((List<string>)switches[arg]).Add(param);
}
}
else {
Console.WriteLine("Unable to find configuration file ["+param+"]");
}
}
// handles complex option switches
// RX for splitting comma seperated values:
// http://dotnetslackers.com/Regex/re-19977_Regex_This_regex_splits_comma_or_semicolon_separated_lists_of_optionally_quoted_strings_It_hand.aspx
// @"\s*[;,]\s*(?!(?<=(?:^|[;,])\s*""(?:[^""]|""""|\\"")*[;,])(?:[^""]|""""|\\"")*""\s*(?:[;,]|$))"
// http://regexlib.com/REDetails.aspx?regexp_id=621
// @",(?!(?<=(?:^|,)\s*\x22(?:[^\x22]|\x22\x22|\\\x22)*,)(?:[^\x22]|\x22\x22|\\\x22)*\x22\s*(?:,|$))"
/// <summary>
/// Gets the complex options.
/// </summary>
/// <param name="rawParameterList">The raw parameter list.</param>
/// <returns></returns>
/// <remarks></remarks>
public static IEnumerable<ComplexOption> GetComplexOptions(this IEnumerable<string> rawParameterList) {
var optionList = new List<ComplexOption>();
foreach(string p in rawParameterList) {
var m = Regex.Match(p, @"\[(?>\"".*?\""|\[(?<DEPTH>)|\](?<-DEPTH>)|[^[]]?)*\](?(DEPTH)(?!))");
if(m.Success) {
var co = new ComplexOption();
var v = m.Groups[0].Value;
var len = v.Length;
co.WholePrefix = v.Substring(1, len - 2);
co.WholeValue = p.Substring(len);
var parameterStrings = Regex.Split(co.WholePrefix, @",(?!(?<=(?:^|,)\s*\x22(?:[^\x22]|\x22\x22|\\\x22)*,)(?:[^\x22]|\x22\x22|\\\x22)*\x22\s*(?:,|$))");
foreach(string q in parameterStrings) {
v = q.Trim();
if(v[0] == '"' && v[v.Length - 1] == '"') {
v = v.Trim('"');
}
co.PrefixParameters.Add(v);
}
optionList.Add(co);
}
}
return optionList;
}
// public static List<string> Data(this string[] args) {
/// <summary>
/// Parameterses the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns></returns>
/// <remarks></remarks>
public static IEnumerable<string> Parameters(this IEnumerable<string> args) {
return parameters ?? (parameters = from argument in args
where !(argument.StartsWith("--"))
select argument);
}
/// <summary>
///
/// </summary>
public const string HelpConfigSyntax = @"
Advanced Command Line Configuration Files
-----------------------------------------
You may pass any double-dashed command line options in a configuration file
that is loaded with --load-config=<file>.
Inside the configuration file, omit the double dash prefix; simply put
each option on a seperate line.
On the command line:
--some-option=foo
would become the following inside the configuration file:
some-option=foo
Additionally, options in the configuration file can be grouped together in
categories. The category is simply syntatic sugar for simplifying the command
line.
Categories are declared with the square brackets: []
The category is appended to options that follow its declaration.
A configuration file expressed as:
source-option=foo
source-option=bar
source-option=bin
source-add=baz
source-ignore=bug
can also be expressed as:
[source]
option=foo
option=bar
option=bin
add=baz
ignore=bug
";
}
}