Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}" +
Expand Down Expand Up @@ -534,4 +533,4 @@ private string GetDefaultHelp(Command command, bool trimOneNewline = true)
return output;
}
}
}
}
14 changes: 14 additions & 0 deletions src/System.CommandLine.Tests/Help/HelpBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
6 changes: 3 additions & 3 deletions src/System.CommandLine.Tests/Utility/AssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public static AndConstraint<StringCollectionAssertions<IEnumerable<string>>> BeE
}

public static AndConstraint<StringAssertions> ShowHelp(this StringAssertions output) =>
output.Subject.Should().Match("*Description:*Usage:*");
output.Subject.Should().Contain("Usage:");

public static AndConstraint<StringAssertions> NotShowHelp(this StringAssertions output) =>
output.Subject.Should().NotMatch("*Description:*Usage:*");
}
output.Subject.Should().NotContain("Usage:");
}
11 changes: 8 additions & 3 deletions src/System.CommandLine/Help/HelpBuilder.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static string GetIdentifierSymbolUsageLabel(Symbol symbol, ICollection<s
/// </summary>
public static IEnumerable<Func<HelpContext, bool>> GetLayout()
{
yield return SynopsisSection();
yield return CommandDescriptionSection();
yield return CommandUsageSection();
yield return CommandArgumentsSection();
yield return OptionsSection();
Expand All @@ -180,11 +180,16 @@ public static IEnumerable<Func<HelpContext, bool>> GetLayout()
}

/// <summary>
/// Writes a help section describing a command's synopsis.
/// Writes the command description help section.
/// </summary>
public static Func<HelpContext, bool> SynopsisSection() =>
public static Func<HelpContext, bool> CommandDescriptionSection() =>
ctx =>
{
if (string.IsNullOrWhiteSpace(ctx.Command.Description))
Comment thread
jonsequitur marked this conversation as resolved.
{
return false;
}

ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output);
return true;
};
Expand Down