Skip to content

Commit 83b79da

Browse files
committed
style: C# cleanup to use more C# 8 features, and fix a few analyzer warnings
1 parent ef6bdcd commit 83b79da

File tree

10 files changed

+26
-31
lines changed

10 files changed

+26
-31
lines changed

src/CommandLineUtils/CommandLineApplication.Execute.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ public static int Execute<TApp>(CommandLineContext context)
5151

5252
try
5353
{
54-
using (var app = new CommandLineApplication<TApp>())
55-
{
56-
app.SetContext(context);
57-
app.Conventions.UseDefaultConventions();
58-
return app.Execute(context.Arguments);
59-
}
54+
using var app = new CommandLineApplication<TApp>();
55+
app.SetContext(context);
56+
app.Conventions.UseDefaultConventions();
57+
return app.Execute(context.Arguments);
6058
}
6159
catch (CommandParsingException ex)
6260
{
@@ -101,7 +99,7 @@ public static int Execute<TApp>(params string[] args)
10199
public static int Execute<TApp>(IConsole console, params string[] args)
102100
where TApp : class
103101
{
104-
args = args ?? new string[0];
102+
args ??= Util.EmptyArray<string>();
105103
var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args);
106104
return Execute<TApp>(context);
107105
}
@@ -134,7 +132,7 @@ public static Task<int> ExecuteAsync<TApp>(params string[] args)
134132
public static Task<int> ExecuteAsync<TApp>(IConsole console, params string[] args)
135133
where TApp : class
136134
{
137-
args = args ?? new string[0];
135+
args ??= Util.EmptyArray<string>();
138136
var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args);
139137
return ExecuteAsync<TApp>(context);
140138
}

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,8 @@ public void ShowHelp(string? commandName = null)
917917
public virtual string GetHelpText()
918918
{
919919
var sb = new StringBuilder();
920-
_helpTextGenerator.Generate(this, new StringWriter(sb));
920+
using var writer = new StringWriter(sb);
921+
_helpTextGenerator.Generate(this, writer);
921922
return sb.ToString();
922923
}
923924

src/CommandLineUtils/Conventions/ExecuteMethodConvention.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private async Task<int> OnExecute(ConventionContext context)
4949
throw new InvalidOperationException(Strings.AmbiguousOnExecuteMethod);
5050
}
5151

52-
method = method ?? asyncMethod;
52+
method ??= asyncMethod;
5353

5454
if (method == null)
5555
{

src/CommandLineUtils/Conventions/RemainingArgsPropertyConvention.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public virtual void Apply(ConventionContext context)
2828

2929
var typeInfo = context.ModelType.GetTypeInfo();
3030
var prop = typeInfo.GetProperty("RemainingArguments", PropertyBindingFlags);
31-
prop = prop ?? typeInfo.GetProperty("RemainingArgs", PropertyBindingFlags);
31+
prop ??= typeInfo.GetProperty("RemainingArgs", PropertyBindingFlags);
3232
if (prop == null)
3333
{
3434
return;

src/CommandLineUtils/IO/ConsoleReporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace McMaster.Extensions.CommandLineUtils
1212
/// </summary>
1313
public class ConsoleReporter : IReporter
1414
{
15-
private object _writeLock = new object();
15+
private readonly object _writeLock = new object();
1616

1717
/// <summary>
1818
/// Initializes an instance of <see cref="ConsoleReporter"/>.
@@ -57,7 +57,7 @@ public ConsoleReporter(IConsole console, bool verbose, bool quiet)
5757
/// <param name="message"></param>
5858
/// <param name="foregroundColor"></param>
5959
/// <param name="backgroundColor"></param>
60-
protected virtual void WriteLine(TextWriter writer, string message, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = default(ConsoleColor?))
60+
protected virtual void WriteLine(TextWriter writer, string message, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = default)
6161
{
6262
lock (_writeLock)
6363
{

src/CommandLineUtils/Internal/AnsiConsole.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private AnsiConsole(TextWriter writer, bool useConsoleColor)
2121
}
2222

2323
private int _boldRecursion;
24-
private bool _useConsoleColor;
24+
private readonly bool _useConsoleColor;
2525

2626
public static AnsiConsole GetOutput(bool useConsoleColor)
2727
{

src/CommandLineUtils/Internal/CommandLineProcessor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
using System.IO;
99
using System.Linq;
1010
using McMaster.Extensions.CommandLineUtils.Abstractions;
11-
using McMaster.Extensions.CommandLineUtils.Internal;
1211

1312
namespace McMaster.Extensions.CommandLineUtils
1413
{
1514
internal sealed class CommandLineProcessor
1615
{
17-
private readonly CommandLineApplication _app;
1816
private readonly CommandLineApplication _initialCommand;
1917
private readonly ArgumentEnumerator _enumerator;
2018

@@ -33,10 +31,13 @@ private CommandLineApplication _currentCommand
3331
public CommandLineProcessor(CommandLineApplication command,
3432
IReadOnlyList<string> arguments)
3533
{
36-
_app = command;
3734
_initialCommand = command;
3835
_enumerator = new ArgumentEnumerator(command, arguments ?? new string[0]);
36+
CheckForShortOptionClustering(command);
37+
}
3938

39+
private static void CheckForShortOptionClustering(CommandLineApplication command)
40+
{
4041
if (!command.ClusterOptionsWasSetExplicitly)
4142
{
4243
foreach (var option in AllOptions(command))

src/CommandLineUtils/Internal/ReflectionHelper.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ public static object[] BindParameters(MethodInfo method, CommandLineApplication
8383
else
8484
{
8585
object? service = command.AdditionalServices?.GetService(methodParam.ParameterType);
86-
if (service == null)
87-
{
88-
throw new InvalidOperationException(Strings.UnsupportedParameterTypeOnMethod(method.Name, methodParam));
89-
}
90-
arguments[i] = service;
86+
arguments[i] = service ?? throw new InvalidOperationException(Strings.UnsupportedParameterTypeOnMethod(method.Name, methodParam));
9187
}
9288
}
9389

src/Hosting.CommandLine/HostBuilderExtensions.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,15 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
5454
.AddSingleton<ICommandLineService, CommandLineService<TApp>>();
5555
});
5656

57-
using (var host = hostBuilder.Build())
58-
{
59-
await host.RunAsync(cancellationToken);
60-
61-
if (exceptionHandler.StoredException != null)
62-
{
63-
ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
64-
}
57+
using var host = hostBuilder.Build();
58+
await host.RunAsync(cancellationToken);
6559

66-
return state.ExitCode;
60+
if (exceptionHandler.StoredException != null)
61+
{
62+
ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
6763
}
64+
65+
return state.ExitCode;
6866
}
6967
}
7068
}

src/Hosting.CommandLine/Internal/CommandLineLifetime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public Task WaitForStartAsync(CancellationToken cancellationToken)
101101
public void Dispose()
102102
{
103103
_disposeComplete.Set();
104+
_disposeComplete.Dispose();
104105
}
105106
}
106107
}

0 commit comments

Comments
 (0)