Skip to content

Commit 56a6239

Browse files
authored
Merge pull request #18746 from sfoslund/CmdHelp
Show help on command parsing error
2 parents ef6249b + c5554b9 commit 56a6239

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

src/Cli/dotnet/CommandLineValidation/CommandParsingException.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.CommandLine.Parsing;
56

67
namespace Microsoft.DotNet.Cli
78
{
89
internal class CommandParsingException : Exception
910
{
1011
public CommandParsingException(
1112
string message,
12-
string helpText = null) : base(message)
13+
ParseResult parseResult = null) : base(message)
1314
{
14-
HelpText = helpText ?? "";
15+
ParseResult = parseResult;
1516
Data.Add("CLI_User_Displayed_Exception", true);
1617
}
1718

18-
public string HelpText { get; } = "";
19+
public ParseResult ParseResult;
1920
}
2021
}

src/Cli/dotnet/ParseResultExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
2626
{
2727
throw new CommandParsingException(
2828
message: string.Join(Environment.NewLine,
29-
parseResult.Errors.Select(e => e.Message)));
29+
parseResult.Errors.Select(e => e.Message)),
30+
parseResult: parseResult);
3031
}
3132
}
3233
else if (parseResult.HasOption("--help"))

src/Cli/dotnet/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public static int Main(string[] args)
7070
: e.Message.Red().Bold());
7171

7272
var commandParsingException = e as CommandParsingException;
73-
if (commandParsingException != null)
73+
if (commandParsingException != null && commandParsingException.ParseResult != null)
7474
{
75-
Reporter.Output.WriteLine(commandParsingException.HelpText);
75+
commandParsingException.ParseResult.ShowHelp();
7676
}
7777

7878
return 1;

src/Tests/dotnet-add-reference.Tests/GivenDotnetAddReference.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void WhenNoCommandIsPassedItPrintsError(string commandName)
127127
.Execute($"add", commandName);
128128
cmd.Should().Fail();
129129
cmd.StdErr.Should().Be(CommonLocalizableStrings.RequiredCommandNotPassed);
130+
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(AddCommandHelpText(Directory.GetCurrentDirectory()));
130131
}
131132

132133
[Fact]

0 commit comments

Comments
 (0)