From e66a10793b0aff0451c50c77d85df358de5b9b35 Mon Sep 17 00:00:00 2001 From: marko1olo Date: Fri, 5 Jun 2026 23:07:25 +0400 Subject: [PATCH 1/2] Fix empty description help section --- .../Help/HelpBuilderTests.Customization.cs | 3 +-- .../Help/HelpBuilderTests.cs | 14 ++++++++++++++ src/System.CommandLine/Help/HelpBuilder.Default.cs | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) 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..f9c6c15d0b 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 Synopsis_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/Help/HelpBuilder.Default.cs b/src/System.CommandLine/Help/HelpBuilder.Default.cs index 00364784dd..986d33ecb7 100644 --- a/src/System.CommandLine/Help/HelpBuilder.Default.cs +++ b/src/System.CommandLine/Help/HelpBuilder.Default.cs @@ -185,6 +185,11 @@ public static IEnumerable> GetLayout() public static Func SynopsisSection() => ctx => { + if (string.IsNullOrWhiteSpace(ctx.Command.Description)) + { + return false; + } + ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output); return true; }; From 7829e695198a7d5665603a3a45989084a8a586f3 Mon Sep 17 00:00:00 2001 From: marko1olo Date: Fri, 5 Jun 2026 23:38:04 +0400 Subject: [PATCH 2/2] test: allow help without descriptions --- src/System.CommandLine.Tests/Help/HelpBuilderTests.cs | 2 +- src/System.CommandLine.Tests/Utility/AssertionExtensions.cs | 6 +++--- src/System.CommandLine/Help/HelpBuilder.Default.cs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs index f9c6c15d0b..de5ea39c3f 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs @@ -78,7 +78,7 @@ public void Synopsis_section_properly_wraps_description() [InlineData(null)] [InlineData("")] [InlineData(" ")] - public void Synopsis_section_is_not_rendered_when_description_is_empty(string? description) + public void Command_description_section_is_not_rendered_when_description_is_empty(string? description) { var command = new RootCommand(description); 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 986d33ecb7..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,9 +180,9 @@ 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))