Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/System.CommandLine.Tests/UseParseErrorReportingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,46 @@ public void User_can_customize_parse_error_result_code()

result.Should().Be(42);
}

[Fact]
public void User_can_customize_help_printed_on_parse_error()
{
CustomHelpBuilder customHelpBuilder = new();

CliRootCommand root = new ();
root.Options.Clear();
root.Options.Add(new HelpOption()
{
Action = new HelpAction()
{
Builder = customHelpBuilder
}
});

CliConfiguration config = new(root)
{
EnableParseErrorReporting = true
};

customHelpBuilder.WasUsed.Should().BeFalse();

ParseResult parseResult = root.Parse("-bla", config);

int result = parseResult.Invoke();
result.Should().Be(1);
customHelpBuilder.WasUsed.Should().BeTrue();
}

private sealed class CustomHelpBuilder : HelpBuilder
{
internal bool WasUsed = false;

public override void Write(HelpContext context)
{
WasUsed = true;

base.Write(context);
}
}
}
}
5 changes: 4 additions & 1 deletion src/System.CommandLine/Invocation/ParseErrorAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Help;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -23,7 +24,9 @@ public override int Invoke(ParseResult parseResult)

ConsoleHelpers.ResetTerminalForegroundColor();

new HelpOption().Action!.Invoke(parseResult);
HelpOption helpOption = parseResult.RootCommandResult.Command.Options.FirstOrDefault(option => option is HelpOption) as HelpOption ?? new HelpOption();

helpOption.Action!.Invoke(parseResult);

return 1;
}
Expand Down