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..5ce59c46cd 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("")] + [InlineData(" ")] + [InlineData(null)] + public void Synopsis_section_is_omitted_when_description_is_empty(string? description) + { + var command = new Command("the-command", 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..babf3f5a31 100644 --- a/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs +++ b/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs @@ -46,9 +46,9 @@ public static AndConstraint>> BeE return assertions.BeEquivalentTo(expectedValues, c => c.WithStrictOrderingFor(s => s)); } - public static AndConstraint ShowHelp(this StringAssertions output) => - output.Subject.Should().Match("*Description:*Usage:*"); + public static AndConstraint ShowHelp(this StringAssertions output) => + output.Subject.Should().Contain("Usage:"); - public static AndConstraint NotShowHelp(this StringAssertions output) => - output.Subject.Should().NotMatch("*Description:*Usage:*"); -} \ No newline at end of file + public static AndConstraint NotShowHelp(this StringAssertions output) => + 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..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; };