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
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
// 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
{
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;
}
}
3 changes: 2 additions & 1 deletion src/Cli/dotnet/ParseResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down