Skip to content

Commit 53cbe5e

Browse files
authored
Update to System.CommandLine 2.0 and refactor usage (#1776) (#1777)
* Update to System.CommandLine 2.0 and refactor usage (#1776) Updated the System.CommandLine package to the stable version 2.0.0. Replaced deprecated `CommandLineParser.Parse` and `CommandLineConfiguration.InvokeAsync` with `RootCommand.Parse` and `ParseResult.InvokeAsync` respectively. * Update Changelog.md - add improvements - add new fixe 1776, 1764, 1741
1 parent d4dfa2c commit 53cbe5e

File tree

3 files changed

+40
-45
lines changed

3 files changed

+40
-45
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<PackageVersion Include="ReportGenerator.Core" Version="5.3.11" />
3838
<!--For test issue 809 https://github.com/coverlet-coverage/coverlet/issues/809-->
3939
<PackageVersion Include="LinqKit.Microsoft.EntityFrameworkCore" Version="8.1.8" />
40-
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta6.25358.103" />
40+
<PackageVersion Include="System.CommandLine" Version="2.0.0" />
4141
<!--To test issue 1104 https://github.com/coverlet-coverage/coverlet/issues/1104-->
4242
<!-- latest Tmds.ExecFunction package uses EnvDTE V17.8.37221 -->
4343
<PackageVersion Include="Tmds.ExecFunction" Version="0.8.0" />

Documentation/Changelog.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99

1010
### Fixed
11-
11+
- Fix System.CommandLine 2.0 release is available [#1776](https://github.com/coverlet-coverage/coverlet/issues/1776)
12+
- Fix Excluding From Coverage bad defaults from given example [#1764](https://github.com/coverlet-coverage/coverlet/issues/1764)
1213
- Fix branchpoint exclusion for sdk 8.0.407 [#1741](https://github.com/coverlet-coverage/coverlet/issues/1741)
13-
- Use `<TargetFramework>netstandard2.0</TargetFramework>` for _coverlet.collector_ and _coverlet.msbuild.tasks_ Packages´
1414

1515
### Improvements
1616

17+
- Use multi targets projects for coverlet.collector, coverlet.msbuild.tasks packages [#1742](https://github.com/coverlet-coverage/coverlet/pull/1742)
18+
- Use .NET 8.0 target framework for coverlet.core and remove Newtonsoft.Json [#1733](https://github.com/coverlet-coverage/coverlet/pull/1733)
19+
- Use latest System.CommandLine version [#1660](https://github.com/coverlet-coverage/coverlet/pull/1660)
1720
- Upgraded minimum required .NET SDK and runtime to .NET 8.0 LTS (Long Term Support) (**Breaking Change**)
1821
- Use [xunit.v3](https://xunit.net/docs/getting-started/v3/whats-new) for tests and example code
1922

src/coverlet.console/Program.cs

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.CommandLine;
7-
using System.CommandLine.Help;
8-
using System.CommandLine.Parsing;
97
using System.ComponentModel;
108
using System.Diagnostics;
119
using System.Globalization;
@@ -31,11 +29,11 @@ public static class Program
3129
static async Task<int> Main(string[] args)
3230
{
3331
Argument<string> moduleOrAppDirectory = new("path") { Description = "Path to the test assembly or application directory." };
34-
Option<string> target = new("--target", aliases: new[] { "--target", "-t" }) { Description = "Path to the test runner application.", Arity = ArgumentArity.ZeroOrOne, Required = true };
35-
Option<string> targs = new("--targetargs", aliases: new[] { "--targetargs", "-a" }) { Description = "Arguments to be passed to the test runner.", Arity = ArgumentArity.ZeroOrOne };
36-
Option<string> output = new("--output", aliases: new[] { "--output", "-o" }) { Description = "Output of the generated coverage report", Arity = ArgumentArity.ZeroOrOne };
37-
Option<LogLevel> verbosity = new("--verbosity", aliases: new[] { "--verbosity", "-v" }) { DefaultValueFactory = (_) => LogLevel.Normal, Description = "Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.", Arity = ArgumentArity.ZeroOrOne };
38-
Option<string[]> formats = new("--format", aliases: new[] { "--format", "-f" }) { DefaultValueFactory = (_) => new[] { "json" }, Description = "Format of the generated coverage report.", Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
32+
Option<string> target = new("--target", "-t") { Description = "Path to the test runner application.", Arity = ArgumentArity.ZeroOrOne, Required = true };
33+
Option<string> targs = new("--targetargs", "-a") { Description = "Arguments to be passed to the test runner.", Arity = ArgumentArity.ZeroOrOne };
34+
Option<string> output = new("--output", "-o") { Description = "Output of the generated coverage report", Arity = ArgumentArity.ZeroOrOne };
35+
Option<LogLevel> verbosity = new("--verbosity", "-v") { DefaultValueFactory = (_) => LogLevel.Normal, Description = "Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.", Arity = ArgumentArity.ZeroOrOne };
36+
Option<string[]> formats = new("--format", "-f") { DefaultValueFactory = (_) => new[] { "json" }, Description = "Format of the generated coverage report.", Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
3937
formats.AcceptOnlyFromAmong("json", "lcov", "opencover", "cobertura", "teamcity");
4038
Option<string> threshold = new("--threshold") { Description = "Exits with error if the coverage % is below value.", Arity = ArgumentArity.ZeroOrOne };
4139
Option<List<string>> thresholdTypes = new("--threshold-type") { DefaultValueFactory = (_) => ["line", "branch", "method"], Description = "Coverage type to apply the threshold to." };
@@ -56,37 +54,31 @@ static async Task<int> Main(string[] args)
5654
excludeAssembliesWithoutSources.AcceptOnlyFromAmong("MissingAll", "MissingAny", "None");
5755
Option<string> sourceMappingFile = new("--source-mapping-file") { Description = "Specifies the path to a SourceRootsMappings file.", Arity = ArgumentArity.ZeroOrOne };
5856

59-
RootCommand rootCommand = new("Cross platform .NET Core code coverage tool")
60-
{
61-
moduleOrAppDirectory,
62-
target,
63-
targs,
64-
output,
65-
verbosity,
66-
formats,
67-
threshold,
68-
thresholdTypes,
69-
thresholdStat,
70-
excludeFilters,
71-
includeFilters,
72-
excludedSourceFiles,
73-
includeDirectories,
74-
excludeAttributes,
75-
includeTestAssembly,
76-
singleHit,
77-
skipAutoProp,
78-
mergeWith,
79-
useSourceLink,
80-
doesNotReturnAttributes,
81-
excludeAssembliesWithoutSources,
82-
sourceMappingFile
83-
};
84-
rootCommand.Add(new HelpOption());
85-
rootCommand.Add(new VersionOption());
86-
87-
ParseResult parseResult = CommandLineParser.Parse(rootCommand, args);
88-
89-
rootCommand.SetAction(async (context) =>
57+
RootCommand rootCommand = new("Cross platform .NET Core code coverage tool");
58+
rootCommand.Arguments.Add(moduleOrAppDirectory);
59+
rootCommand.Options.Add(target);
60+
rootCommand.Options.Add(targs);
61+
rootCommand.Options.Add(output);
62+
rootCommand.Options.Add(verbosity);
63+
rootCommand.Options.Add(formats);
64+
rootCommand.Options.Add(threshold);
65+
rootCommand.Options.Add(thresholdTypes);
66+
rootCommand.Options.Add(thresholdStat);
67+
rootCommand.Options.Add(excludeFilters);
68+
rootCommand.Options.Add(includeFilters);
69+
rootCommand.Options.Add(excludedSourceFiles);
70+
rootCommand.Options.Add(includeDirectories);
71+
rootCommand.Options.Add(excludeAttributes);
72+
rootCommand.Options.Add(includeTestAssembly);
73+
rootCommand.Options.Add(singleHit);
74+
rootCommand.Options.Add(skipAutoProp);
75+
rootCommand.Options.Add(mergeWith);
76+
rootCommand.Options.Add(useSourceLink);
77+
rootCommand.Options.Add(doesNotReturnAttributes);
78+
rootCommand.Options.Add(excludeAssembliesWithoutSources);
79+
rootCommand.Options.Add(sourceMappingFile);
80+
81+
rootCommand.SetAction(async (parseResult) =>
9082
{
9183
string moduleOrAppDirectoryValue = parseResult.GetValue(moduleOrAppDirectory);
9284
string targetValue = parseResult.GetValue(target);
@@ -136,13 +128,13 @@ static async Task<int> Main(string[] args)
136128
doesNotReturnAttributesValue,
137129
excludeAssembliesWithoutSourcesValue,
138130
sourceMappingFileValue);
139-
//context.ExitCode = taskStatus;
140131

132+
s_exitCode = taskStatus;
133+
return taskStatus;
141134
});
142135

143-
CommandLineConfiguration config = new(rootCommand);
144-
145-
await config.InvokeAsync(args).ConfigureAwait(false);
136+
ParseResult parseResult = rootCommand.Parse(args);
137+
await parseResult.InvokeAsync();
146138
return s_exitCode;
147139
}
148140
private static Task<int> HandleCommand(string moduleOrAppDirectory,

0 commit comments

Comments
 (0)