Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis committed Oct 29, 2024
1 parent 5f0b87d commit acbd60f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

/*
Expand Down Expand Up @@ -56,9 +56,9 @@ public void Help_display_can_be_disabled()
var result = rootCommand.Parse("oops", config);
if (result.Action is CliDiagnosticAction CliDiagnostic)
if (result.Action is CliDiagnosticAction cliDiagnostic)
{
CliDiagnostic.ShowHelp = false;
cliDiagnostic.ShowHelp = false;
}
result.Invoke();
Expand Down
64 changes: 12 additions & 52 deletions src/System.CommandLine/Parsing/CliDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,14 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;

namespace System.CommandLine.Parsing;

/*
* Pattern based on:
* https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnostic.cs
* https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnosticDescriptor.cs
* https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs
* https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/Diagnostic.cs
* https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141791086
*/
internal static class ParseDiagnostics
{
public const string DirectiveIsNotDefinedId = "CMD0001";
public static readonly CliDiagnosticDescriptor DirectiveIsNotDefined =
new(
DirectiveIsNotDefinedId,
//TODO: use localized strings
"Directive is not defined",
"The directive '{0}' is not defined.",
CliDiagnosticSeverity.Error,
null);
}

public sealed class CliDiagnosticDescriptor
{
public CliDiagnosticDescriptor(string id, string title, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string messageFormat, CliDiagnosticSeverity severity, string? helpUri)
{
Id = id;
Title = title;
MessageFormat = messageFormat;
Severity = severity;
HelpUri = helpUri;
}

public string Id { get; }
public string Title { get; }
[StringSyntax(StringSyntaxAttribute.CompositeFormat)]
public string MessageFormat { get; }
public CliDiagnosticSeverity Severity { get; }
public string? HelpUri { get; }
}

public enum CliDiagnosticSeverity
{
Hidden = 0,
Info,
Warning,
Error
}

/// <summary>
/// Describes an error that occurs while parsing command line input.
/// </summary>
public sealed class CliDiagnostic
{
// TODO: reevaluate whether we should be exposing a SymbolResult here
// TODO: Rename to CliError

/// <summary>
/// Initializes a new instance of the <see cref="CliDiagnostic"/> class.
/// </summary>
Expand All @@ -70,6 +18,14 @@ public sealed class CliDiagnostic
/// <param name="properties">Properties to be associated with the diagnostic.</param>
/// <param name="cliSymbolResult">Contains information about a single value entered.</param>
/// <param name="location">The location of the error.</param>
/*
* Pattern based on:
* https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnostic.cs
* https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnosticDescriptor.cs
* https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs
* https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/Diagnostic.cs
* https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141791086
*/
public CliDiagnostic(
CliDiagnosticDescriptor descriptor,
object?[]? messageArgs,
Expand All @@ -80,6 +36,8 @@ public CliDiagnostic(
Descriptor = descriptor;
MessageArgs = messageArgs;
Properties = properties;
CliSymbolResult = cliSymbolResult;
Location = location;
}

/// <summary>
Expand All @@ -105,6 +63,8 @@ public string Message

public CliSymbolResult? CliSymbolResult { get; }

public Location? Location { get; }

/// <inheritdoc />
public override string ToString() => Message;
}
47 changes: 47 additions & 0 deletions src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;

namespace System.CommandLine.Parsing;

/// <summary>
/// Provides a description of a <see cref="CliDiagnostic"/>.
/// </summary>
public sealed class CliDiagnosticDescriptor
{
public CliDiagnosticDescriptor(string id, string title, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string messageFormat, CliDiagnosticSeverity severity, string? helpUri)
{
Id = id;
Title = title;
MessageFormat = messageFormat;
Severity = severity;
HelpUri = helpUri;
}

/// <summary>
/// A unique identifier for the diagnostic.
/// </summary>
public string Id { get; }

/// <summary>
/// A short title describing the diagnostic.
/// </summary>
public string Title { get; }

/// <summary>
/// A composite format string, which can be passed to <see cref="string.Format(string, object[])"/> to create a message.
/// </summary>
[StringSyntax(StringSyntaxAttribute.CompositeFormat)]
public string MessageFormat { get; }

/// <summary>
/// The severity of the diagnostic.
/// </summary>
public CliDiagnosticSeverity Severity { get; }

/// <summary>
/// An optional hyperlink that provides more information about the diagnostic.
/// </summary>
public string? HelpUri { get; }
}
31 changes: 31 additions & 0 deletions src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.CommandLine.Parsing;

/// <summary>
/// Describes how severe a <see cref="CliDiagnostic"/> is."/>
/// </summary>
// Pattern based on: https://github.com/dotnet/roslyn/blob/1cca63b5d8ea170f8d8e88e1574aa3ebe354c23b/src/Compilers/Core/Portable/Diagnostic/DiagnosticSeverity.cs.
public enum CliDiagnosticSeverity
{
/// <summary>
/// Something that is not surfaced through normal means.
/// </summary>
Hidden = 0,

/// <summary>
/// Information that does not indicate a problem (i.e. not prescriptive).
/// </summary>
Info,

/// <summary>
/// Something suspicious but allowed.
/// </summary>
Warning,

/// <summary>
/// Something that is definitely wrong and needs fixing.
/// </summary>
Error
}
17 changes: 17 additions & 0 deletions src/System.CommandLine/Parsing/ParseDiagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.CommandLine.Parsing;

internal static class ParseDiagnostics
{
public const string DirectiveIsNotDefinedId = "CMD0001";
public static readonly CliDiagnosticDescriptor DirectiveIsNotDefined =
new(
DirectiveIsNotDefinedId,
//TODO: use localized strings
"Directive is not defined",
"The directive '{0}' is not defined.",
CliDiagnosticSeverity.Error,
null);
}
5 changes: 4 additions & 1 deletion src/System.CommandLine/System.CommandLine.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>true</IsPackable>
Expand Down Expand Up @@ -29,7 +29,10 @@
<Compile Include="CliValueSymbol.cs" />
<Compile Include="ICliValueSymbol.cs" />
<Compile Include="Parsing\CliCommandResult.cs" />
<Compile Include="Parsing\CliDiagnosticDescriptor.cs" />
<Compile Include="Parsing\CliDiagnosticSeverity.cs" />
<Compile Include="Parsing\CliSymbolResult.cs" />
<Compile Include="Parsing\ParseDiagnostics.cs" />
<Compile Include="Parsing\SymbolLookupByName.cs" />
<Compile Include="Parsing\ValueResultOutcome.cs" />
<Compile Include="Binding\ArgumentConversionResultType.cs" />
Expand Down

0 comments on commit acbd60f

Please sign in to comment.