Skip to content

Commit e66a107

Browse files
committed
Fix empty description help section
1 parent fa1991f commit e66a107

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ public void Help_default_sections_can_be_wrapped()
466466
command.Parse("test -h").Invoke(new() { Output = output });
467467

468468
output.ToString().Should().Be(
469-
$"Description:{NewLine}{NewLine}" +
470469
$"Usage:{NewLine} test [options]{NewLine}{NewLine}" +
471470
$"Options:{NewLine}" +
472471
$" --option option {NewLine}" +
@@ -534,4 +533,4 @@ private string GetDefaultHelp(Command command, bool trimOneNewline = true)
534533
return output;
535534
}
536535
}
537-
}
536+
}

src/System.CommandLine.Tests/Help/HelpBuilderTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public void Synopsis_section_properly_wraps_description()
7474
_console.ToString().Should().Contain(expected);
7575
}
7676

77+
[Theory]
78+
[InlineData(null)]
79+
[InlineData("")]
80+
[InlineData(" ")]
81+
public void Synopsis_section_is_not_rendered_when_description_is_empty(string? description)
82+
{
83+
var command = new RootCommand(description);
84+
85+
_helpBuilder.Write(command, _console);
86+
87+
_console.ToString().Should().NotContain("Description:");
88+
_console.ToString().Should().Contain("Usage:");
89+
}
90+
7791
[Fact]
7892
public void Command_name_in_synopsis_can_be_specified()
7993
{

src/System.CommandLine/Help/HelpBuilder.Default.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ public static IEnumerable<Func<HelpContext, bool>> GetLayout()
185185
public static Func<HelpContext, bool> SynopsisSection() =>
186186
ctx =>
187187
{
188+
if (string.IsNullOrWhiteSpace(ctx.Command.Description))
189+
{
190+
return false;
191+
}
192+
188193
ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output);
189194
return true;
190195
};

0 commit comments

Comments
 (0)