Skip to content

Commit

Permalink
Merge pull request #1874 from dotnet/revert-1872-sclUpdate
Browse files Browse the repository at this point in the history
Revert "System.CommandLine update"
  • Loading branch information
JoeRobich authored Jun 15, 2023
2 parents 3491d2c + a0c1dec commit 1caca35
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 167 deletions.
12 changes: 6 additions & 6 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
<Uri>https://github.com/dotnet/symreader</Uri>
<Sha>27e584661980ee6d82c419a2a471ae505b7d122e</Sha>
</Dependency>
<Dependency Name="Microsoft.SourceBuild.Intermediate.command-line-api" Version="0.1.430701">
<Dependency Name="Microsoft.SourceBuild.Intermediate.command-line-api" Version="0.1.356401">
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>02fe27cd6a9b001c8feb7938e6ef4b3799745759</Sha>
<Sha>8374d5fca634a93458c84414b1604c12f765d1ab</Sha>
<SourceBuild RepoName="command-line-api" ManagedOnly="true" />
</Dependency>
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.23307.1">
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.22564.1">
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>02fe27cd6a9b001c8feb7938e6ef4b3799745759</Sha>
<Sha>8374d5fca634a93458c84414b1604c12f765d1ab</Sha>
</Dependency>
<Dependency Name="System.CommandLine.Rendering" Version="0.4.0-alpha.23307.1">
<Dependency Name="System.CommandLine.Rendering" Version="0.4.0-alpha.22564.1">
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>02fe27cd6a9b001c8feb7938e6ef4b3799745759</Sha>
<Sha>8374d5fca634a93458c84414b1604c12f765d1ab</Sha>
</Dependency>
</ProductDependencies>
<ToolsetDependencies>
Expand Down
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</PropertyGroup>
<PropertyGroup>
<!-- command-line-api -->
<SystemCommandLineVersion>2.0.0-beta4.23307.1</SystemCommandLineVersion>
<SystemCommandLineRenderingVersion>0.4.0-alpha.23307.1</SystemCommandLineRenderingVersion>
<SystemCommandLineVersion>2.0.0-beta4.22564.1</SystemCommandLineVersion>
<SystemCommandLineRenderingVersion>0.4.0-alpha.22564.1</SystemCommandLineRenderingVersion>
<!-- corefx -->
<MicrosoftVisualBasicVersion>10.3.0</MicrosoftVisualBasicVersion>
<!-- msbuild -->
Expand Down
102 changes: 102 additions & 0 deletions src/CommandLineExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Microsoft.CodeAnalysis.Tools
{
internal static class CommandLineExtensions
{
internal static OptionResult? GetOptionResult(this ParseResult result, string alias)
{
return GetOptionResult(result.CommandResult, alias);
}

internal static ArgumentResult? GetArgumentResult(this ParseResult result, string alias)
{
return GetArgumentResult(result.CommandResult, alias);
}

internal static OptionResult? GetOptionResult(this CommandResult result, string alias)
{
return result.Children.GetByAlias(alias) as OptionResult;
}

internal static ArgumentResult? GetArgumentResult(this CommandResult result, string alias)
{
return result.Children.GetByAlias(alias) as ArgumentResult;
}

internal static SymbolResult? GetByAlias(this IReadOnlyList<SymbolResult> results, string alias)
{
return results.SingleOrDefault(result => result.Symbol.Name.Equals(alias) || result.Symbol is IdentifierSymbol id && id.HasAlias(alias));
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this ParseResult result, string alias)
{
return GetValueForArgument<T>(result.CommandResult, alias);
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this ParseResult result, Argument<T> argument)
{
return GetValueForArgument<T>(result.CommandResult, argument);
}

[return: MaybeNull]
internal static T GetValueForOption<T>(this ParseResult result, string alias)
{
return GetValueForOption<T>(result.CommandResult, alias);
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this CommandResult result, Argument<T> argumentDefinition)
{
var arguments = result.Children.Where(x => x.Symbol.Name == argumentDefinition.Name).ToArray();
if (arguments.Length == 1 &&
arguments.SingleOrDefault() is ArgumentResult argument &&
argument.GetValueOrDefault<T>() is T t)
{
return t;
}

return default;
}

[return: MaybeNull]
internal static T GetValueForArgument<T>(this CommandResult result, string alias)
{
if (result.GetArgumentResult(alias) is ArgumentResult argument &&
argument.GetValueOrDefault<T>() is { } t)
{
return t;
}

return default;
}

[return: MaybeNull]
internal static T GetValueForOption<T>(this CommandResult result, string alias)
{
if (result.GetOptionResult(alias) is OptionResult option &&
option.GetValueOrDefault<T>() is { } t)
{
return t;
}

return default;
}

internal static bool WasOptionUsed(this ParseResult result, params string[] aliases)
{
return result.Tokens
.Where(token => token.Type == TokenType.Option)
.Any(token => aliases.Contains(token.Value));
}
}
}
27 changes: 14 additions & 13 deletions src/Commands/FormatAnalyzersCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using System.Collections.Immutable;
using System.CommandLine;
using System.CommandLine.IO;
using System.Threading;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using static Microsoft.CodeAnalysis.Tools.FormatCommandCommon;
Expand All @@ -14,51 +14,52 @@ internal static class FormatAnalyzersCommand
{
private static readonly FormatAnalyzersHandler s_analyzerHandler = new();

internal static CliCommand GetCommand()
internal static Command GetCommand()
{
var command = new CliCommand("analyzers", Resources.Run_3rd_party_analyzers__and_apply_fixes)
var command = new Command("analyzers", Resources.Run_3rd_party_analyzers__and_apply_fixes)
{
DiagnosticsOption,
ExcludeDiagnosticsOption,
SeverityOption,
};
command.AddCommonOptions();
command.Action = s_analyzerHandler;
command.Handler = s_analyzerHandler;
return command;
}

private class FormatAnalyzersHandler : CliAction
private class FormatAnalyzersHandler : ICommandHandler
{
public override int Invoke(ParseResult parseResult) => InvokeAsync(parseResult, CancellationToken.None).GetAwaiter().GetResult();
public int Invoke(InvocationContext context) => InvokeAsync(context).GetAwaiter().GetResult();

public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
public async Task<int> InvokeAsync(InvocationContext context)
{
var parseResult = context.ParseResult;
var formatOptions = parseResult.ParseVerbosityOption(FormatOptions.Instance);
var logger = new SystemConsole().SetupLogging(minimalLogLevel: formatOptions.LogLevel, minimalErrorLevel: LogLevel.Warning);
var logger = context.Console.SetupLogging(minimalLogLevel: formatOptions.LogLevel, minimalErrorLevel: LogLevel.Warning);
formatOptions = parseResult.ParseCommonOptions(formatOptions, logger);
formatOptions = parseResult.ParseWorkspaceOptions(formatOptions);

if (parseResult.GetResult(SeverityOption) is not null &&
if (parseResult.HasOption(SeverityOption) &&
parseResult.GetValue(SeverityOption) is string { Length: > 0 } analyzerSeverity)
{
formatOptions = formatOptions with { AnalyzerSeverity = GetSeverity(analyzerSeverity) };
}

if (parseResult.GetResult(DiagnosticsOption) is not null &&
if (parseResult.HasOption(DiagnosticsOption) &&
parseResult.GetValue(DiagnosticsOption) is string[] { Length: > 0 } diagnostics)
{
formatOptions = formatOptions with { Diagnostics = diagnostics.ToImmutableHashSet() };
}

if (parseResult.GetResult(ExcludeDiagnosticsOption) is not null &&
if (parseResult.HasOption(ExcludeDiagnosticsOption) &&
parseResult.GetValue(ExcludeDiagnosticsOption) is string[] { Length: > 0 } excludeDiagnostics)
{
formatOptions = formatOptions with { ExcludeDiagnostics = excludeDiagnostics.ToImmutableHashSet() };
}

formatOptions = formatOptions with { FixCategory = FixCategory.Analyzers };

return await FormatAsync(formatOptions, logger, cancellationToken).ConfigureAwait(false);
return await FormatAsync(formatOptions, logger, context.GetCancellationToken()).ConfigureAwait(false);
}
}
}
Expand Down
Loading

0 comments on commit 1caca35

Please sign in to comment.