diff --git a/src/dotnet/DotNetTopLevelCommandBase.cs b/src/dotnet/DotNetTopLevelCommandBase.cs index 5e2850ee5..650a5fcbb 100644 --- a/src/dotnet/DotNetTopLevelCommandBase.cs +++ b/src/dotnet/DotNetTopLevelCommandBase.cs @@ -28,6 +28,8 @@ namespace Microsoft.DotNet.Cli ParseResult = parser.ParseFrom($"dotnet {CommandName}", args); + ParseResult.ShowHelpIfRequested(); + var subcommandName = ParseResult.Command().Name; try diff --git a/src/dotnet/ParseResultExtensions.cs b/src/dotnet/ParseResultExtensions.cs index 82932de0b..b662d9430 100644 --- a/src/dotnet/ParseResultExtensions.cs +++ b/src/dotnet/ParseResultExtensions.cs @@ -13,6 +13,19 @@ namespace Microsoft.DotNet.Cli Console.WriteLine(parseResult.Command().HelpView()); public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult) + { + parseResult.ShowHelpIfRequested(); + + if (parseResult.Errors.Any()) + { + throw new CommandParsingException( + message: string.Join(Environment.NewLine, + parseResult.Errors.Select(e => e.Message)), + helpText: parseResult?.Command()?.HelpView()); + } + } + + public static void ShowHelpIfRequested(this ParseResult parseResult) { var appliedCommand = parseResult.AppliedCommand(); @@ -23,14 +36,6 @@ namespace Microsoft.DotNet.Cli // NOTE: this is a temporary stage in refactoring toward the ClicCommandLineParser being used at the CLI entry point. throw new HelpException(parseResult.Command().HelpView()); } - - if (parseResult.Errors.Any()) - { - throw new CommandParsingException( - message: string.Join(Environment.NewLine, - parseResult.Errors.Select(e => e.Message)), - helpText: parseResult?.Command()?.HelpView()); - } } } } \ No newline at end of file diff --git a/test/dotnet.Tests/GivenThatTheUserRequestsHelp.cs b/test/dotnet.Tests/GivenThatTheUserRequestsHelp.cs new file mode 100644 index 000000000..153a375d0 --- /dev/null +++ b/test/dotnet.Tests/GivenThatTheUserRequestsHelp.cs @@ -0,0 +1,44 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using FluentAssertions; +using Microsoft.DotNet.Tools.Test.Utilities; +using Xunit; + +namespace dotnet.Tests +{ + public class GivenThatTheUserRequestsHelp + { + [Theory] + [InlineData("-h")] + [InlineData("add -h")] + [InlineData("add package -h")] + [InlineData("add reference -h")] + [InlineData("build -h")] + [InlineData("cache -h")] + [InlineData("clean -h")] + [InlineData("list -h")] + [InlineData("migrate -h")] + [InlineData("msbuild -h")] + [InlineData("new -h")] + [InlineData("nuget -h")] + [InlineData("pack -h")] + [InlineData("publish -h")] + [InlineData("remove -h")] + [InlineData("restore -h")] + [InlineData("run -h")] + [InlineData("sln -h")] + [InlineData("sln add -h")] + [InlineData("sln list -h")] + [InlineData("sln remove -h")] + [InlineData("test -h")] + public void TheResponseIsNotAnError(string commandLine) + { + var result = new DotnetCommand() + .ExecuteWithCapturedOutput(commandLine); + + result.ExitCode.Should().Be(0); + } + } +} \ No newline at end of file