From c5554b910415a868406e11da9b5f3dd11d08f2ef Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Tue, 6 Jul 2021 11:30:13 -0700 Subject: [PATCH] Show help on command parsing error --- .../CommandLineValidation/CommandParsingException.cs | 7 ++++--- src/Cli/dotnet/ParseResultExtensions.cs | 3 ++- src/Cli/dotnet/Program.cs | 4 ++-- .../dotnet-add-reference.Tests/GivenDotnetAddReference.cs | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Cli/dotnet/CommandLineValidation/CommandParsingException.cs b/src/Cli/dotnet/CommandLineValidation/CommandParsingException.cs index 7a7361055821..352468b4e576 100644 --- a/src/Cli/dotnet/CommandLineValidation/CommandParsingException.cs +++ b/src/Cli/dotnet/CommandLineValidation/CommandParsingException.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.CommandLine.Parsing; namespace Microsoft.DotNet.Cli { @@ -9,12 +10,12 @@ internal class CommandParsingException : Exception { public CommandParsingException( string message, - string helpText = null) : base(message) + ParseResult parseResult = null) : base(message) { - HelpText = helpText ?? ""; + ParseResult = parseResult; Data.Add("CLI_User_Displayed_Exception", true); } - public string HelpText { get; } = ""; + public ParseResult ParseResult; } } diff --git a/src/Cli/dotnet/ParseResultExtensions.cs b/src/Cli/dotnet/ParseResultExtensions.cs index e3851bdac1ae..7292d9995461 100644 --- a/src/Cli/dotnet/ParseResultExtensions.cs +++ b/src/Cli/dotnet/ParseResultExtensions.cs @@ -26,7 +26,8 @@ public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult) { throw new CommandParsingException( message: string.Join(Environment.NewLine, - parseResult.Errors.Select(e => e.Message))); + parseResult.Errors.Select(e => e.Message)), + parseResult: parseResult); } } else if (parseResult.HasOption("--help")) diff --git a/src/Cli/dotnet/Program.cs b/src/Cli/dotnet/Program.cs index eb8613a36728..b470461eb6eb 100644 --- a/src/Cli/dotnet/Program.cs +++ b/src/Cli/dotnet/Program.cs @@ -70,9 +70,9 @@ public static int Main(string[] args) : e.Message.Red().Bold()); var commandParsingException = e as CommandParsingException; - if (commandParsingException != null) + if (commandParsingException != null && commandParsingException.ParseResult != null) { - Reporter.Output.WriteLine(commandParsingException.HelpText); + commandParsingException.ParseResult.ShowHelp(); } return 1; diff --git a/src/Tests/dotnet-add-reference.Tests/GivenDotnetAddReference.cs b/src/Tests/dotnet-add-reference.Tests/GivenDotnetAddReference.cs index 7cd4aaf58135..26141b5e0b3f 100644 --- a/src/Tests/dotnet-add-reference.Tests/GivenDotnetAddReference.cs +++ b/src/Tests/dotnet-add-reference.Tests/GivenDotnetAddReference.cs @@ -127,6 +127,7 @@ public void WhenNoCommandIsPassedItPrintsError(string commandName) .Execute($"add", commandName); cmd.Should().Fail(); cmd.StdErr.Should().Be(CommonLocalizableStrings.RequiredCommandNotPassed); + cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(AddCommandHelpText(Directory.GetCurrentDirectory())); } [Fact]