Skip to content

Commit 5543d77

Browse files
committed
Add basic logic to ErrorReportingSubsystem
1 parent 4457da4 commit 5543d77

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/System.CommandLine.Subsystems/ConsoleHack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ConsoleHack
1010
private readonly StringBuilder buffer = new();
1111
private bool redirecting = false;
1212

13-
public void WriteLine(string text)
13+
public void WriteLine(string text = "")
1414
{
1515
if (redirecting)
1616
{
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.CommandLine.Parsing;
45
using System.CommandLine.Subsystems;
56
using System.CommandLine.Subsystems.Annotations;
67

78
namespace System.CommandLine;
89

10+
/// <summary>
11+
///
12+
/// </summary>
13+
/// <remarks>
14+
/// </remarks>
915
public class ErrorReportingSubsystem : CliSubsystem
1016
{
1117
public ErrorReportingSubsystem(IAnnotationProvider? annotationProvider = null)
1218
: base(ErrorReportingAnnotations.Prefix, SubsystemKind.ErrorReporting, annotationProvider)
1319
{ }
1420

15-
// TODO: Stash option rather than using string
1621
protected internal override bool GetIsActivated(ParseResult? parseResult)
1722
=> parseResult is not null && parseResult.Errors.Any();
1823

1924
protected internal override CliExit Execute(PipelineContext pipelineContext)
2025
{
21-
pipelineContext.ConsoleHack.WriteLine("You have errors!");
26+
var _ = pipelineContext.ParseResult
27+
?? throw new ArgumentNullException($"{nameof(pipelineContext)}.ParseResult");
28+
29+
Report(pipelineContext.ConsoleHack, pipelineContext.ParseResult.Errors);
30+
2231
return CliExit.SuccessfullyHandled(pipelineContext.ParseResult);
2332
}
33+
34+
// TODO: internal, protected virtual?
35+
public void Report(ConsoleHack consoleHack, IReadOnlyList<ParseError> errors)
36+
{
37+
ConsoleHelpers.ResetTerminalForegroundColor();
38+
ConsoleHelpers.SetTerminalForegroundRed();
39+
40+
foreach (var error in errors)
41+
{
42+
consoleHack.WriteLine(error.Message);
43+
}
44+
consoleHack.WriteLine();
45+
46+
ConsoleHelpers.ResetTerminalForegroundColor();
47+
}
2448
}

src/System.CommandLine/ConsoleHelpers.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace System.CommandLine
77
{
8-
internal static class ConsoleHelpers
8+
// TODO: Added to project and made public for use in ErrorReportingSubsystem
9+
public static class ConsoleHelpers
910
{
1011
private static readonly bool ColorsAreSupported = GetColorsAreSupported();
1112

@@ -20,15 +21,15 @@ private static bool GetColorsAreSupported()
2021
#endif
2122
&& !Console.IsOutputRedirected;
2223

23-
internal static void SetTerminalForegroundRed()
24+
public static void SetTerminalForegroundRed()
2425
{
2526
if (ColorsAreSupported)
2627
{
2728
Console.ForegroundColor = ConsoleColor.Red;
2829
}
2930
}
3031

31-
internal static void ResetTerminalForegroundColor()
32+
public static void ResetTerminalForegroundColor()
3233
{
3334
if (ColorsAreSupported)
3435
{

src/System.CommandLine/System.CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="CliOption{T}.cs" />
4747
<Compile Include="CliRootCommand.cs" />
4848
<Compile Include="CliSymbol.cs" />
49+
<Compile Include="ConsoleHelpers.cs" />
4950
<Compile Include="LocalizationResources.cs" />
5051
<Compile Include="ParseResult.cs" />
5152
<Compile Include="Parsing\ArgumentResult.cs" />

0 commit comments

Comments
 (0)