Skip to content

Commit fcfd448

Browse files
daveMuellernatemcmaster
authored andcommitted
fix: update help text generator to display HelpOption correctly (#323)
1 parent 4fe7d70 commit fcfd448

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ public virtual void ShowHint()
998998
var flag = !string.IsNullOrEmpty(OptionHelp.LongName)
999999
? "--" + OptionHelp.LongName
10001000
: !string.IsNullOrEmpty(OptionHelp.ShortName)
1001-
? "-" + OptionHelp.LongName
1001+
? "-" + OptionHelp.ShortName
10021002
: "-" + OptionHelp.SymbolName;
10031003

10041004
Out.WriteLine($"Specify {flag} for a list of available options and commands.");

src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected virtual void GenerateCommands(
278278
if (application.OptionHelp != null)
279279
{
280280
output.WriteLine();
281-
output.WriteLine($"Run '{application.Name} [command] --{application.OptionHelp.LongName}' for more information about a command.");
281+
output.WriteLine($"Run '{application.Name} [command] {Format(application.OptionHelp)}' for more information about a command.");
282282
}
283283
}
284284
}

test/CommandLineUtils.Tests/CommandLineApplicationTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Runtime.InteropServices;
99
using System.Threading.Tasks;
10+
using Moq;
1011
using Xunit;
1112
using Xunit.Abstractions;
1213

@@ -1052,5 +1053,21 @@ public async Task OperationCanceledReturnsExpectedOsCode()
10521053
var exitCode = await executeTask;
10531054
Assert.Equal(expectedCode, exitCode);
10541055
}
1056+
1057+
[Theory]
1058+
[InlineData("-h")]
1059+
[InlineData("--h")]
1060+
[InlineData("-?")]
1061+
public void ShowHintDisplaysValidInfo(string helpOption)
1062+
{
1063+
var app = new CommandLineApplication();
1064+
var textWriter = new Mock<TextWriter>();
1065+
app.Out = textWriter.Object;
1066+
app.HelpOption(helpOption);
1067+
1068+
app.ShowHint();
1069+
1070+
textWriter.Verify(mock => mock.WriteLine($"Specify {helpOption} for a list of available options and commands."), Times.Once);
1071+
}
10551072
}
10561073
}

test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
57
using System.IO;
68
using System.Text;
79
using McMaster.Extensions.CommandLineUtils.HelpText;
@@ -175,5 +177,34 @@ public class MyApp
175177
[Argument(1, Description = "enum arg desc")]
176178
public SomeEnum SomeEnumArgument { get; set; }
177179
}
180+
181+
[Theory]
182+
[InlineData("-h", "-h", " -h Show help information", " Subcommand ")]
183+
[InlineData("--help", "--help", " --help Show help information", " Subcommand ")]
184+
[InlineData("-?", "-?", " -? Show help information", " Subcommand ")]
185+
[InlineData(null, "-?|-h|--help", " -?|-h|--help Show help information", " Subcommand ")]
186+
public void ShowHelpWithSubcommands(string helpOption, string expectedHintText, string expectedOptionsText,
187+
string expectedCommandsText)
188+
{
189+
var app = new CommandLineApplication {Name = "test"};
190+
if (helpOption != null) app.HelpOption(helpOption);
191+
app.Command("Subcommand", _ => { });
192+
app.Conventions.UseDefaultConventions();
193+
var helpText = GetHelpText(app);
194+
195+
Assert.Equal($@"Usage: test [command] [options]
196+
197+
Options:
198+
{expectedOptionsText}
199+
200+
Commands:
201+
{expectedCommandsText}
202+
203+
Run 'test [command] {expectedHintText}' for more information about a command.
204+
205+
",
206+
helpText,
207+
ignoreLineEndingDifferences: true);
208+
}
178209
}
179210
}

0 commit comments

Comments
 (0)