Skip to content

Commit

Permalink
style: C# cleanup to use more C# 8 features, and fix a few analyzer w…
Browse files Browse the repository at this point in the history
…arnings
  • Loading branch information
natemcmaster committed Jun 20, 2019
1 parent ef6bdcd commit 83b79da
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 31 deletions.
14 changes: 6 additions & 8 deletions src/CommandLineUtils/CommandLineApplication.Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ public static int Execute<TApp>(CommandLineContext context)

try
{
using (var app = new CommandLineApplication<TApp>())
{
app.SetContext(context);
app.Conventions.UseDefaultConventions();
return app.Execute(context.Arguments);
}
using var app = new CommandLineApplication<TApp>();
app.SetContext(context);
app.Conventions.UseDefaultConventions();
return app.Execute(context.Arguments);
}
catch (CommandParsingException ex)
{
Expand Down Expand Up @@ -101,7 +99,7 @@ public static int Execute<TApp>(params string[] args)
public static int Execute<TApp>(IConsole console, params string[] args)
where TApp : class
{
args = args ?? new string[0];
args ??= Util.EmptyArray<string>();
var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args);
return Execute<TApp>(context);
}
Expand Down Expand Up @@ -134,7 +132,7 @@ public static Task<int> ExecuteAsync<TApp>(params string[] args)
public static Task<int> ExecuteAsync<TApp>(IConsole console, params string[] args)
where TApp : class
{
args = args ?? new string[0];
args ??= Util.EmptyArray<string>();
var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args);
return ExecuteAsync<TApp>(context);
}
Expand Down
3 changes: 2 additions & 1 deletion src/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,8 @@ public void ShowHelp(string? commandName = null)
public virtual string GetHelpText()
{
var sb = new StringBuilder();
_helpTextGenerator.Generate(this, new StringWriter(sb));
using var writer = new StringWriter(sb);
_helpTextGenerator.Generate(this, writer);
return sb.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private async Task<int> OnExecute(ConventionContext context)
throw new InvalidOperationException(Strings.AmbiguousOnExecuteMethod);
}

method = method ?? asyncMethod;
method ??= asyncMethod;

if (method == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public virtual void Apply(ConventionContext context)

var typeInfo = context.ModelType.GetTypeInfo();
var prop = typeInfo.GetProperty("RemainingArguments", PropertyBindingFlags);
prop = prop ?? typeInfo.GetProperty("RemainingArgs", PropertyBindingFlags);
prop ??= typeInfo.GetProperty("RemainingArgs", PropertyBindingFlags);
if (prop == null)
{
return;
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLineUtils/IO/ConsoleReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace McMaster.Extensions.CommandLineUtils
/// </summary>
public class ConsoleReporter : IReporter
{
private object _writeLock = new object();
private readonly object _writeLock = new object();

/// <summary>
/// Initializes an instance of <see cref="ConsoleReporter"/>.
Expand Down Expand Up @@ -57,7 +57,7 @@ public ConsoleReporter(IConsole console, bool verbose, bool quiet)
/// <param name="message"></param>
/// <param name="foregroundColor"></param>
/// <param name="backgroundColor"></param>
protected virtual void WriteLine(TextWriter writer, string message, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = default(ConsoleColor?))
protected virtual void WriteLine(TextWriter writer, string message, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor = default)
{
lock (_writeLock)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLineUtils/Internal/AnsiConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private AnsiConsole(TextWriter writer, bool useConsoleColor)
}

private int _boldRecursion;
private bool _useConsoleColor;
private readonly bool _useConsoleColor;

public static AnsiConsole GetOutput(bool useConsoleColor)
{
Expand Down
7 changes: 4 additions & 3 deletions src/CommandLineUtils/Internal/CommandLineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
using System.IO;
using System.Linq;
using McMaster.Extensions.CommandLineUtils.Abstractions;
using McMaster.Extensions.CommandLineUtils.Internal;

namespace McMaster.Extensions.CommandLineUtils
{
internal sealed class CommandLineProcessor
{
private readonly CommandLineApplication _app;
private readonly CommandLineApplication _initialCommand;
private readonly ArgumentEnumerator _enumerator;

Expand All @@ -33,10 +31,13 @@ private CommandLineApplication _currentCommand
public CommandLineProcessor(CommandLineApplication command,
IReadOnlyList<string> arguments)
{
_app = command;
_initialCommand = command;
_enumerator = new ArgumentEnumerator(command, arguments ?? new string[0]);
CheckForShortOptionClustering(command);
}

private static void CheckForShortOptionClustering(CommandLineApplication command)
{
if (!command.ClusterOptionsWasSetExplicitly)
{
foreach (var option in AllOptions(command))
Expand Down
6 changes: 1 addition & 5 deletions src/CommandLineUtils/Internal/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public static object[] BindParameters(MethodInfo method, CommandLineApplication
else
{
object? service = command.AdditionalServices?.GetService(methodParam.ParameterType);
if (service == null)
{
throw new InvalidOperationException(Strings.UnsupportedParameterTypeOnMethod(method.Name, methodParam));
}
arguments[i] = service;
arguments[i] = service ?? throw new InvalidOperationException(Strings.UnsupportedParameterTypeOnMethod(method.Name, methodParam));
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/Hosting.CommandLine/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
.AddSingleton<ICommandLineService, CommandLineService<TApp>>();
});

using (var host = hostBuilder.Build())
{
await host.RunAsync(cancellationToken);

if (exceptionHandler.StoredException != null)
{
ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
}
using var host = hostBuilder.Build();
await host.RunAsync(cancellationToken);

return state.ExitCode;
if (exceptionHandler.StoredException != null)
{
ExceptionDispatchInfo.Capture(exceptionHandler.StoredException).Throw();
}

return state.ExitCode;
}
}
}
1 change: 1 addition & 0 deletions src/Hosting.CommandLine/Internal/CommandLineLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public Task WaitForStartAsync(CancellationToken cancellationToken)
public void Dispose()
{
_disposeComplete.Set();
_disposeComplete.Dispose();
}
}
}

0 comments on commit 83b79da

Please sign in to comment.