From fcfd448767d728e74218dd9739ed9b7e081369b5 Mon Sep 17 00:00:00 2001 From: daveMueller Date: Thu, 2 Jan 2020 01:16:52 +0100 Subject: [PATCH] fix: update help text generator to display HelpOption correctly (#323) --- .../CommandLineApplication.cs | 2 +- .../HelpText/DefaultHelpTextGenerator.cs | 2 +- .../CommandLineApplicationTests.cs | 17 ++++++++++ .../DefaultHelpTextGeneratorTests.cs | 31 +++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/CommandLineUtils/CommandLineApplication.cs b/src/CommandLineUtils/CommandLineApplication.cs index 2d8c5e5b..70cab498 100644 --- a/src/CommandLineUtils/CommandLineApplication.cs +++ b/src/CommandLineUtils/CommandLineApplication.cs @@ -998,7 +998,7 @@ public virtual void ShowHint() var flag = !string.IsNullOrEmpty(OptionHelp.LongName) ? "--" + OptionHelp.LongName : !string.IsNullOrEmpty(OptionHelp.ShortName) - ? "-" + OptionHelp.LongName + ? "-" + OptionHelp.ShortName : "-" + OptionHelp.SymbolName; Out.WriteLine($"Specify {flag} for a list of available options and commands."); diff --git a/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs b/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs index f26e39ce..9c5d963b 100644 --- a/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs +++ b/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs @@ -278,7 +278,7 @@ protected virtual void GenerateCommands( if (application.OptionHelp != null) { output.WriteLine(); - output.WriteLine($"Run '{application.Name} [command] --{application.OptionHelp.LongName}' for more information about a command."); + output.WriteLine($"Run '{application.Name} [command] {Format(application.OptionHelp)}' for more information about a command."); } } } diff --git a/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs b/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs index ed9e681c..fab68a1a 100644 --- a/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs +++ b/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; +using Moq; using Xunit; using Xunit.Abstractions; @@ -1052,5 +1053,21 @@ public async Task OperationCanceledReturnsExpectedOsCode() var exitCode = await executeTask; Assert.Equal(expectedCode, exitCode); } + + [Theory] + [InlineData("-h")] + [InlineData("--h")] + [InlineData("-?")] + public void ShowHintDisplaysValidInfo(string helpOption) + { + var app = new CommandLineApplication(); + var textWriter = new Mock(); + app.Out = textWriter.Object; + app.HelpOption(helpOption); + + app.ShowHint(); + + textWriter.Verify(mock => mock.WriteLine($"Specify {helpOption} for a list of available options and commands."), Times.Once); + } } } diff --git a/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs b/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs index cc17b87a..618efd62 100644 --- a/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs +++ b/test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections; +using System.Collections.Generic; using System.IO; using System.Text; using McMaster.Extensions.CommandLineUtils.HelpText; @@ -175,5 +177,34 @@ public class MyApp [Argument(1, Description = "enum arg desc")] public SomeEnum SomeEnumArgument { get; set; } } + + [Theory] + [InlineData("-h", "-h", " -h Show help information", " Subcommand ")] + [InlineData("--help", "--help", " --help Show help information", " Subcommand ")] + [InlineData("-?", "-?", " -? Show help information", " Subcommand ")] + [InlineData(null, "-?|-h|--help", " -?|-h|--help Show help information", " Subcommand ")] + public void ShowHelpWithSubcommands(string helpOption, string expectedHintText, string expectedOptionsText, + string expectedCommandsText) + { + var app = new CommandLineApplication {Name = "test"}; + if (helpOption != null) app.HelpOption(helpOption); + app.Command("Subcommand", _ => { }); + app.Conventions.UseDefaultConventions(); + var helpText = GetHelpText(app); + + Assert.Equal($@"Usage: test [command] [options] + +Options: +{expectedOptionsText} + +Commands: +{expectedCommandsText} + +Run 'test [command] {expectedHintText}' for more information about a command. + +", + helpText, + ignoreLineEndingDifferences: true); + } } }