diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs index c3d73d818f..3194811959 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs @@ -466,7 +466,6 @@ public void Help_default_sections_can_be_wrapped() command.Parse("test -h").Invoke(new() { Output = output }); output.ToString().Should().Be( - $"Description:{NewLine}{NewLine}" + $"Usage:{NewLine} test [options]{NewLine}{NewLine}" + $"Options:{NewLine}" + $" --option option {NewLine}" + @@ -534,4 +533,4 @@ private string GetDefaultHelp(Command command, bool trimOneNewline = true) return output; } } -} \ No newline at end of file +} diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs index 019c4b7e3b..de5ea39c3f 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs @@ -74,6 +74,20 @@ public void Synopsis_section_properly_wraps_description() _console.ToString().Should().Contain(expected); } + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void Command_description_section_is_not_rendered_when_description_is_empty(string? description) + { + var command = new RootCommand(description); + + _helpBuilder.Write(command, _console); + + _console.ToString().Should().NotContain("Description:"); + _console.ToString().Should().Contain("Usage:"); + } + [Fact] public void Command_name_in_synopsis_can_be_specified() { diff --git a/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs b/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs index 74dcc77879..f7ac51a2ab 100644 --- a/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs +++ b/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs @@ -47,8 +47,8 @@ public static AndConstraint>> BeE } public static AndConstraint ShowHelp(this StringAssertions output) => - output.Subject.Should().Match("*Description:*Usage:*"); + output.Subject.Should().Contain("Usage:"); public static AndConstraint NotShowHelp(this StringAssertions output) => - output.Subject.Should().NotMatch("*Description:*Usage:*"); -} \ No newline at end of file + output.Subject.Should().NotContain("Usage:"); +} diff --git a/src/System.CommandLine/Help/HelpBuilder.Default.cs b/src/System.CommandLine/Help/HelpBuilder.Default.cs index 00364784dd..d4bda5f77e 100644 --- a/src/System.CommandLine/Help/HelpBuilder.Default.cs +++ b/src/System.CommandLine/Help/HelpBuilder.Default.cs @@ -171,7 +171,7 @@ private static string GetIdentifierSymbolUsageLabel(Symbol symbol, ICollection public static IEnumerable> GetLayout() { - yield return SynopsisSection(); + yield return CommandDescriptionSection(); yield return CommandUsageSection(); yield return CommandArgumentsSection(); yield return OptionsSection(); @@ -180,11 +180,16 @@ public static IEnumerable> GetLayout() } /// - /// Writes a help section describing a command's synopsis. + /// Writes the command description help section. /// - public static Func SynopsisSection() => + public static Func CommandDescriptionSection() => ctx => { + if (string.IsNullOrWhiteSpace(ctx.Command.Description)) + { + return false; + } + ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output); return true; };