-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathArgumentParser.cs
More file actions
308 lines (265 loc) · 10.5 KB
/
ArgumentParser.cs
File metadata and controls
308 lines (265 loc) · 10.5 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
namespace GitVersion
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
public class ArgumentParser
{
public static Arguments ParseArguments(string commandLineArguments)
{
return ParseArguments(commandLineArguments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList());
}
public static Arguments ParseArguments(List<string> commandLineArguments)
{
if (commandLineArguments.Count == 0)
{
return new Arguments
{
TargetPath = Environment.CurrentDirectory
};
}
var firstArgument = commandLineArguments.First();
if (IsHelp(firstArgument))
{
return new Arguments
{
IsHelp = true
};
}
if (IsInit(firstArgument))
{
return new Arguments
{
TargetPath = Environment.CurrentDirectory,
Init = true
};
}
if (commandLineArguments.Count == 1 && !(commandLineArguments[0].StartsWith("-") || commandLineArguments[0].StartsWith("/")))
{
return new Arguments
{
TargetPath = firstArgument
};
}
List<string> namedArguments;
var arguments = new Arguments();
if (firstArgument.StartsWith("-") || firstArgument.StartsWith("/"))
{
arguments.TargetPath = Environment.CurrentDirectory;
namedArguments = commandLineArguments;
}
else
{
arguments.TargetPath = firstArgument;
namedArguments = commandLineArguments.Skip(1).ToList();
}
var args = CollectSwitchesAndValuesFromArguments(namedArguments);
foreach (var name in args.AllKeys)
{
var values = args.GetValues(name);
string value = null;
if (values != null)
{
//Currently, no arguments use more than one value, so having multiple values is an input error.
//In the future, this exception can be removed to support multiple values for a switch.
if (values.Length > 1) throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
value = values.FirstOrDefault();
}
if (IsSwitch("l", name))
{
arguments.LogFilePath = value;
continue;
}
if (IsSwitch("targetpath", name))
{
arguments.TargetPath = value;
continue;
}
if (IsSwitch("dynamicRepoLocation", name))
{
arguments.DynamicRepositoryLocation = value;
continue;
}
if (IsSwitch("url", name))
{
arguments.TargetUrl = value;
continue;
}
if (IsSwitch("b", name))
{
arguments.TargetBranch = value;
continue;
}
if (IsSwitch("u", name))
{
arguments.Authentication.Username = value;
continue;
}
if (IsSwitch("p", name))
{
arguments.Authentication.Password = value;
continue;
}
if (IsSwitch("c", name))
{
arguments.CommitId = value;
continue;
}
if (IsSwitch("exec", name))
{
arguments.Exec = value;
continue;
}
if (IsSwitch("execargs", name))
{
arguments.ExecArgs = value;
continue;
}
if (IsSwitch("proj", name))
{
arguments.Proj = value;
continue;
}
if (IsSwitch("projargs", name))
{
arguments.ProjArgs = value;
continue;
}
if (IsSwitch("updateAssemblyInfo", name))
{
if (new[] { "1", "true" }.Contains(value, StringComparer.OrdinalIgnoreCase))
{
arguments.UpdateAssemblyInfo = true;
}
else if (new[] { "0", "false" }.Contains(value, StringComparer.OrdinalIgnoreCase))
{
arguments.UpdateAssemblyInfo = false;
}
else if (!IsSwitchArgument(value))
{
arguments.UpdateAssemblyInfo = true;
arguments.UpdateAssemblyInfoFileName = value;
}
else
{
arguments.UpdateAssemblyInfo = true;
}
continue;
}
if (IsSwitch("assemblyversionformat", name))
{
throw new WarningException("assemblyversionformat switch removed, use AssemblyVersioningScheme configuration value instead");
}
if (IsSwitch("v", name) || IsSwitch("showvariable", name))
{
string versionVariable = null;
if (!string.IsNullOrWhiteSpace(value))
{
versionVariable = VersionVariables.AvailableVariables.SingleOrDefault(av => av.Equals(value.Replace("'", ""), StringComparison.CurrentCultureIgnoreCase));
}
if (versionVariable == null)
{
var messageFormat = "{0} requires a valid version variable. Available variables are:\n{1}";
var message = string.Format(messageFormat, name, String.Join(", ", VersionVariables.AvailableVariables.Select(x=>string.Concat("'", x, "'"))));
throw new WarningException(message);
}
arguments.ShowVariable = versionVariable;
continue;
}
if (IsSwitch("showConfig", name))
{
if (new[] { "1", "true" }.Contains(value, StringComparer.OrdinalIgnoreCase))
{
arguments.ShowConfig = true;
}
else if (new[] { "0", "false" }.Contains(value, StringComparer.OrdinalIgnoreCase))
{
arguments.UpdateAssemblyInfo = false;
}
else
{
arguments.ShowConfig = true;
}
continue;
}
if (IsSwitch("output", name))
{
OutputType outputType;
if (!Enum.TryParse(value, true, out outputType))
{
throw new WarningException(string.Format("Value '{0}' cannot be parsed as output type, please use 'json' or 'buildserver'", value));
}
arguments.Output = outputType;
continue;
}
if (IsSwitch("nofetch", name))
{
arguments.NoFetch = true;
continue;
}
throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", name));
}
return arguments;
}
static NameValueCollection CollectSwitchesAndValuesFromArguments(List<string> namedArguments)
{
var args = new NameValueCollection();
string currentKey = null;
for (var index = 0; index < namedArguments.Count; index = index + 1)
{
var arg = namedArguments[index];
//If this is a switch, create new name/value entry for it, with a null value.
if (IsSwitchArgument(arg))
{
currentKey = arg;
args.Add(currentKey, null);
}
//If this is a value (not a switch)
else
{
//And if the current switch does not have a value yet, set it's value to this argument.
if (String.IsNullOrEmpty(args[currentKey]))
{
args[currentKey] = arg;
}
//Otherwise add the value under the same switch.
else
{
args.Add(currentKey, arg);
}
}
}
return args;
}
static bool IsSwitchArgument(string value)
{
return value != null && (value.StartsWith("-") || value.StartsWith("/"))
&& !Regex.Match(value, @"/\w+:").Success; //Exclude msbuild & project parameters in form /blah:, which should be parsed as values, not switch names.
}
static bool IsSwitch(string switchName, string value)
{
if (value.StartsWith("-"))
{
value = value.Remove(0, 1);
}
if (value.StartsWith("/"))
{
value = value.Remove(0, 1);
}
return (string.Equals(switchName, value, StringComparison.OrdinalIgnoreCase));
}
static bool IsInit(string singleArgument)
{
return singleArgument.Equals("init", StringComparison.OrdinalIgnoreCase);
}
static bool IsHelp(string singleArgument)
{
return (singleArgument == "?") ||
IsSwitch("h", singleArgument) ||
IsSwitch("help", singleArgument) ||
IsSwitch("?", singleArgument);
}
}
}