added args using CommandLineParser

This commit is contained in:
irongut
2021-04-11 23:07:09 +01:00
parent 96bcb6d125
commit 43337cd72e
3 changed files with 40 additions and 13 deletions
@@ -5,4 +5,8 @@
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>
</Project> </Project>
@@ -0,0 +1,10 @@
using CommandLine;
namespace CodeCoverageSummary
{
public class CommandLineOptions
{
[Value(index: 0, Required = true, HelpText = "Code coverage file to analyse.")]
public string Filename { get; set; }
}
}
+26 -13
View File
@@ -1,4 +1,5 @@
using System; using CommandLine;
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -8,21 +9,33 @@ namespace CodeCoverageSummary
{ {
internal static class Program internal static class Program
{ {
private const string _filename = "D:\\Dev\\Csharp\\CodeCoverageSummary\\coverage.cobertura.xml"; //private const string _filename = "D:\\Dev\\Csharp\\CodeCoverageSummary\\coverage.cobertura.xml";
private static int Main(string[] args) private static int Main(string[] args)
{ {
Console.WriteLine($"Parsing Code Coverage File: {_filename}{Environment.NewLine}"); return Parser.Default.ParseArguments<CommandLineOptions>(args)
string summary = ParseTestResults(_filename); .MapResult<CommandLineOptions, int>(o =>
if (string.IsNullOrWhiteSpace(summary)) {
{ try
return -3; {
} Console.WriteLine($"Parsing Code Coverage File: {o.Filename}{Environment.NewLine}");
else string summary = ParseTestResults(o.Filename);
{ if (string.IsNullOrWhiteSpace(summary))
Console.WriteLine(summary); {
return 0; return -2; // error
} }
else
{
Console.WriteLine(summary);
return 0; // success
}
}
catch
{
return -3; // unhandled error
}
},
errs => -1); // invalid arguments
} }
private static string ParseTestResults(string filename) private static string ParseTestResults(string filename)