Skip to content

Commit 39f4d06

Browse files
committed
ArgumentParser cleanup
1 parent 74f0dd3 commit 39f4d06

File tree

5 files changed

+24
-50
lines changed

5 files changed

+24
-50
lines changed

src/GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using GitVersion;
43
using NUnit.Framework;
54
using Shouldly;
@@ -66,7 +65,7 @@ public void Exec()
6665
[Test]
6766
public void Exec_with_args()
6867
{
69-
var arguments = argumentParser.ParseArguments(new List<string>
68+
var arguments = argumentParser.ParseArguments(new []
7069
{
7170
"-exec",
7271
"rake",
@@ -87,7 +86,7 @@ public void Msbuild()
8786
[Test]
8887
public void Msbuild_with_args()
8988
{
90-
var arguments = argumentParser.ParseArguments(new List<string>
89+
var arguments = argumentParser.ParseArguments(new []
9190
{
9291
"-proj",
9392
"msbuild.proj",

src/GitVersionExe/ArgumentParser.cs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,18 @@ namespace GitVersion
1313
{
1414
public class ArgumentParser : IArgumentParser
1515
{
16-
public Arguments ParseArguments()
17-
{
18-
var argumentsWithoutExeName = GetArgumentsWithoutExeName();
19-
20-
Arguments arguments = null;
21-
try
22-
{
23-
arguments = ParseArguments(argumentsWithoutExeName);
24-
}
25-
catch (Exception exception)
26-
{
27-
Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName));
28-
if (!string.IsNullOrWhiteSpace(exception.Message))
29-
{
30-
Console.WriteLine(exception.Message);
31-
}
32-
33-
return arguments;
34-
}
35-
36-
return arguments;
37-
}
38-
3916
public Arguments ParseArguments(string commandLineArguments)
4017
{
4118
var arguments = commandLineArguments
4219
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
43-
.ToList();
20+
.ToArray();
4421

4522
return ParseArguments(arguments);
4623
}
4724

48-
public Arguments ParseArguments(List<string> commandLineArguments)
25+
public Arguments ParseArguments(string[] commandLineArguments)
4926
{
50-
if (commandLineArguments.Count == 0)
27+
if (commandLineArguments.Length == 0)
5128
{
5229
return new Arguments
5330
{
@@ -75,6 +52,7 @@ public Arguments ParseArguments(List<string> commandLineArguments)
7552
}
7653

7754
var arguments = new Arguments();
55+
7856
var switchesAndValues = CollectSwitchesAndValuesFromArguments(commandLineArguments, out var firstArgumentIsSwitch);
7957

8058
for (var i = 0; i < switchesAndValues.AllKeys.Length; i++)
@@ -189,6 +167,7 @@ public Arguments ParseArguments(List<string> commandLineArguments)
189167
{
190168
arguments.Diag = true;
191169
}
170+
192171
continue;
193172
}
194173

@@ -267,6 +246,7 @@ public Arguments ParseArguments(List<string> commandLineArguments)
267246
{
268247
arguments.ShowConfig = true;
269248
}
249+
270250
continue;
271251
}
272252

@@ -312,6 +292,7 @@ public Arguments ParseArguments(List<string> commandLineArguments)
312292
{
313293
throw new WarningException("Can't specify multiple assembly info files when using /ensureassemblyinfo switch, either use a single assembly info file or do not specify /ensureassemblyinfo and create assembly info files manually");
314294
}
295+
315296
continue;
316297
}
317298

@@ -370,6 +351,7 @@ public Arguments ParseArguments(List<string> commandLineArguments)
370351
{
371352
throw new WarningException($"Could not parse Verbosity value '{value}'");
372353
}
354+
373355
continue;
374356
}
375357

@@ -416,15 +398,15 @@ public Arguments ParseArguments(List<string> commandLineArguments)
416398
return arguments;
417399
}
418400

419-
private void EnsureArgumentValueCount(string[] values, int maxArguments = 1)
401+
private static void EnsureArgumentValueCount(string[] values, int maxArguments = 1)
420402
{
421403
if (values != null && values.Length > maxArguments)
422404
{
423405
throw new WarningException($"Could not parse command line parameter '{values[1]}'.");
424406
}
425407
}
426408

427-
private NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string> namedArguments, out bool firstArgumentIsSwitch)
409+
private static NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string> namedArguments, out bool firstArgumentIsSwitch)
428410
{
429411
firstArgumentIsSwitch = true;
430412
var switchesAndValues = new NameValueCollection();
@@ -446,7 +428,7 @@ private NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string>
446428
else if (currentKey != null)
447429
{
448430
// And if the current switch does not have a value yet and the value is not itself a switch, set its value to this argument.
449-
if (String.IsNullOrEmpty(switchesAndValues[currentKey]))
431+
if (string.IsNullOrEmpty(switchesAndValues[currentKey]))
450432
{
451433
switchesAndValues[currentKey] = arg;
452434
}
@@ -467,12 +449,5 @@ private NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string>
467449

468450
return switchesAndValues;
469451
}
470-
471-
private static List<string> GetArgumentsWithoutExeName()
472-
{
473-
return Environment.GetCommandLineArgs()
474-
.Skip(1)
475-
.ToList();
476-
}
477452
}
478453
}

src/GitVersionExe/IArgumentParser.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.Collections.Generic;
2-
31
namespace GitVersion
42
{
53
public interface IArgumentParser
64
{
75
Arguments ParseArguments(string commandLineArguments);
8-
Arguments ParseArguments(List<string> commandLineArguments);
6+
Arguments ParseArguments(string[] commandLineArguments);
97
}
10-
}
8+
}

src/GitVersionExe/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
using GitVersion.Common;
44
using GitVersion.Configuration;
55
using GitVersion.Logging;
6+
using Console = System.Console;
67
using Environment = GitVersion.Common.Environment;
78

89
namespace GitVersion
910
{
1011
class Program
1112
{
12-
static void Main()
13+
static void Main(string[] args)
1314
{
1415
var fileSystem = new FileSystem();
1516
var environment = new Environment();
1617
var argumentParser = new ArgumentParser();
17-
var arguments = argumentParser.ParseArguments();
1818

1919
int exitCode;
20-
if (arguments != null)
20+
try
2121
{
22+
var arguments = argumentParser.ParseArguments(args);
2223
var log = new Log
2324
{
2425
Verbosity = arguments.Verbosity
@@ -32,8 +33,9 @@ static void Main()
3233

3334
exitCode = app.Run(arguments);
3435
}
35-
else
36+
catch (Exception exception)
3637
{
38+
Console.Error.WriteLine(exception.Message);
3739
exitCode = 1;
3840
}
3941

src/GitVersionTask/GitVersionTasks.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ namespace GitVersionTask
1818
{
1919
public static class GitVersionTasks
2020
{
21-
private static ILog log;
22-
private static IEnvironment environment;
23-
private static IFileSystem fileSystem;
21+
private static readonly ILog log;
22+
private static readonly IEnvironment environment;
23+
private static readonly IFileSystem fileSystem;
2424

2525
static GitVersionTasks()
2626
{

0 commit comments

Comments
 (0)