diff --git a/src/System.CommandLine.Subsystems.Tests/ErrorReportingFunctionalTests.cs b/src/System.CommandLine.Subsystems.Tests/ErrorReportingFunctionalTests.cs index b9deabd18..f3e43ea5a 100644 --- a/src/System.CommandLine.Subsystems.Tests/ErrorReportingFunctionalTests.cs +++ b/src/System.CommandLine.Subsystems.Tests/ErrorReportingFunctionalTests.cs @@ -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. /* @@ -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(); diff --git a/src/System.CommandLine/Parsing/CliDiagnostic.cs b/src/System.CommandLine/Parsing/CliDiagnostic.cs index f8605ee78..d291eae12 100644 --- a/src/System.CommandLine/Parsing/CliDiagnostic.cs +++ b/src/System.CommandLine/Parsing/CliDiagnostic.cs @@ -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 -} - /// /// Describes an error that occurs while parsing command line input. /// public sealed class CliDiagnostic { - // TODO: reevaluate whether we should be exposing a SymbolResult here - // TODO: Rename to CliError - /// /// Initializes a new instance of the class. /// @@ -70,6 +18,14 @@ public sealed class CliDiagnostic /// Properties to be associated with the diagnostic. /// Contains information about a single value entered. /// The location of the error. + /* + * 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, @@ -80,6 +36,8 @@ public CliDiagnostic( Descriptor = descriptor; MessageArgs = messageArgs; Properties = properties; + CliSymbolResult = cliSymbolResult; + Location = location; } /// @@ -105,6 +63,8 @@ public string Message public CliSymbolResult? CliSymbolResult { get; } + public Location? Location { get; } + /// public override string ToString() => Message; } diff --git a/src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs b/src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs new file mode 100644 index 000000000..2e3243ac8 --- /dev/null +++ b/src/System.CommandLine/Parsing/CliDiagnosticDescriptor.cs @@ -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; + +/// +/// Provides a description of a . +/// +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; + } + + /// + /// A unique identifier for the diagnostic. + /// + public string Id { get; } + + /// + /// A short title describing the diagnostic. + /// + public string Title { get; } + + /// + /// A composite format string, which can be passed to to create a message. + /// + [StringSyntax(StringSyntaxAttribute.CompositeFormat)] + public string MessageFormat { get; } + + /// + /// The severity of the diagnostic. + /// + public CliDiagnosticSeverity Severity { get; } + + /// + /// An optional hyperlink that provides more information about the diagnostic. + /// + public string? HelpUri { get; } +} diff --git a/src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs b/src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs new file mode 100644 index 000000000..956892fb9 --- /dev/null +++ b/src/System.CommandLine/Parsing/CliDiagnosticSeverity.cs @@ -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; + +/// +/// Describes how severe a is."/> +/// +// Pattern based on: https://github.com/dotnet/roslyn/blob/1cca63b5d8ea170f8d8e88e1574aa3ebe354c23b/src/Compilers/Core/Portable/Diagnostic/DiagnosticSeverity.cs. +public enum CliDiagnosticSeverity +{ + /// + /// Something that is not surfaced through normal means. + /// + Hidden = 0, + + /// + /// Information that does not indicate a problem (i.e. not prescriptive). + /// + Info, + + /// + /// Something suspicious but allowed. + /// + Warning, + + /// + /// Something that is definitely wrong and needs fixing. + /// + Error +} diff --git a/src/System.CommandLine/Parsing/ParseDiagnostics.cs b/src/System.CommandLine/Parsing/ParseDiagnostics.cs new file mode 100644 index 000000000..b814c441e --- /dev/null +++ b/src/System.CommandLine/Parsing/ParseDiagnostics.cs @@ -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); +} diff --git a/src/System.CommandLine/System.CommandLine.csproj b/src/System.CommandLine/System.CommandLine.csproj index 5c73ca87b..ea33049a7 100644 --- a/src/System.CommandLine/System.CommandLine.csproj +++ b/src/System.CommandLine/System.CommandLine.csproj @@ -1,4 +1,4 @@ - + true @@ -29,7 +29,10 @@ + + +