|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | + |
| 6 | +namespace System.CommandLine.Parsing; |
| 7 | + |
| 8 | +/* |
| 9 | + * Pattern based on: |
| 10 | + * https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnostic.cs |
| 11 | + * https://github.com/mhutch/MonoDevelop.MSBuildEditor/blob/main/MonoDevelop.MSBuild/Analysis/MSBuildDiagnosticDescriptor.cs |
| 12 | + * https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/DiagnosticDescriptor.cs |
| 13 | + * https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/Diagnostic/Diagnostic.cs |
| 14 | + */ |
| 15 | +internal static class ParseDiagnostics |
| 16 | +{ |
| 17 | + public const string DirectiveIsNotDefinedId = "CMD0001"; |
| 18 | + public static readonly CliDiagnosticDescriptor DirectiveIsNotDefined = |
| 19 | + new( |
| 20 | + DirectiveIsNotDefinedId, |
| 21 | + //TODO: use localized strings |
| 22 | + "Directive is not defined", |
| 23 | + "The directive '{0}' is not defined.", |
| 24 | + CliDiagnosticSeverity.Error, |
| 25 | + null); |
| 26 | +} |
| 27 | + |
| 28 | +public sealed class CliDiagnosticDescriptor |
| 29 | +{ |
| 30 | + public CliDiagnosticDescriptor(string id, string title, string messageFormat, CliDiagnosticSeverity severity, string? helpUri) |
| 31 | + { |
| 32 | + Id = id; |
| 33 | + Title = title; |
| 34 | + MessageFormat = messageFormat; |
| 35 | + Severity = severity; |
| 36 | + HelpUri = helpUri; |
| 37 | + } |
| 38 | + |
| 39 | + public string Id { get; } |
| 40 | + public string Title { get; } |
| 41 | + public string MessageFormat { get; } |
| 42 | + public CliDiagnosticSeverity Severity { get; } |
| 43 | + public string? HelpUri { get; } |
| 44 | +} |
| 45 | + |
| 46 | +public enum CliDiagnosticSeverity |
| 47 | +{ |
| 48 | + Hidden = 0, |
| 49 | + Info, |
| 50 | + Warning, |
| 51 | + Error |
| 52 | +} |
| 53 | + |
| 54 | +/// <summary> |
| 55 | +/// Describes an error that occurs while parsing command line input. |
| 56 | +/// </summary> |
| 57 | +public sealed class CliDiagnostic |
| 58 | +{ |
| 59 | + // TODO: reevaluate whether we should be exposing a SymbolResult here |
| 60 | + // TODO: Rename to CliError |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Initializes a new instance of the <see cref="CliDiagnostic"/> class. |
| 64 | + /// </summary> |
| 65 | + /// <param name="descriptor">Contains information about the error.</param> |
| 66 | + /// <param name="messageArgs">The arguments to be passed to the <see cref="CliDiagnosticDescriptor.MessageFormat"/> in the <paramref name="descriptor"/>.</param> |
| 67 | + /// <param name="properties">Properties to be associated with the diagnostic.</param> |
| 68 | + /// <param name="symbolResult">The symbol result detailing the symbol that failed to parse and the tokens involved.</param> |
| 69 | + /// <param name="location">The location of the error.</param> |
| 70 | + public CliDiagnostic( |
| 71 | + CliDiagnosticDescriptor descriptor, |
| 72 | + object?[]? messageArgs, |
| 73 | + ImmutableDictionary<string, object>? properties = null, |
| 74 | + SymbolResult? symbolResult = null, |
| 75 | + Location? location = null) |
| 76 | + { |
| 77 | + Descriptor = descriptor; |
| 78 | + MessageArgs = messageArgs; |
| 79 | + Properties = properties; |
| 80 | + SymbolResult = symbolResult; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets a message to explain the error to a user. |
| 85 | + /// </summary> |
| 86 | + public string Message |
| 87 | + { |
| 88 | + get |
| 89 | + { |
| 90 | + if (MessageArgs is not null) |
| 91 | + { |
| 92 | + return string.Format(Descriptor.MessageFormat, MessageArgs); |
| 93 | + } |
| 94 | + return Descriptor.MessageFormat; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public ImmutableDictionary<string, object>? Properties { get; } |
| 99 | + |
| 100 | + public CliDiagnosticDescriptor Descriptor { get; } |
| 101 | + |
| 102 | + public object?[]? MessageArgs { get; } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Gets the symbol result detailing the symbol that failed to parse and the tokens involved. |
| 106 | + /// </summary> |
| 107 | + public SymbolResult? SymbolResult { get; } |
| 108 | + |
| 109 | + /// <inheritdoc /> |
| 110 | + public override string ToString() => Message; |
| 111 | +} |
0 commit comments