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;
}
}
}
}
15 changes: 15 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,21 @@ public void Synopsis_section_properly_wraps_description()
_console.ToString().Should().Contain(expected);
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Synopsis_section_is_omitted_when_description_is_missing(string? description)
{
var command = new RootCommand(description);

_helpBuilder.Write(command, _console);

_console.ToString().Should()
.NotContain(LocalizationResources.HelpDescriptionTitle())
.And.Contain(LocalizationResources.HelpUsageTitle());
}

[Fact]
public void Command_name_in_synopsis_can_be_specified()
{
Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine.Tests/HelpOptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task Help_option_interrupts_execution_of_the_specified_command()
[InlineData("/?")]
public async Task Help_option_accepts_default_values(string value)
{
var command = new Command("command")
var command = new Command("command", "command description")
{
new HelpOption()
};
Expand All @@ -72,7 +72,7 @@ public async Task Help_option_accepts_default_values(string value)
[Fact]
public async Task Help_option_does_not_display_when_option_defined_with_same_alias()
{
var command = new Command("command");
var command = new Command("command", "command description");
command.Options.Add(new Option<bool>("-h"));

var output = new StringWriter();
Expand Down Expand Up @@ -143,7 +143,7 @@ public void There_are_no_parse_errors_when_help_is_invoked_on_a_command_with_req
[InlineData("--confused")]
public async Task HelpOption_with_custom_aliases_uses_aliases(string helpAlias)
{
RootCommand command = new()
RootCommand command = new("root description")
{
new HelpOption("/lost", "--confused")
};
Expand Down Expand Up @@ -385,4 +385,4 @@ public override int Invoke(ParseResult parseResult)
return 0;
}
}
}
}
8 changes: 4 additions & 4 deletions src/System.CommandLine.Tests/ParseErrorReportingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ParseErrorReportingTests
[Fact] // https://github.com/dotnet/command-line-api/issues/817
public void Help_is_shown_when_required_subcommand_is_missing()
{
var root = new RootCommand
var root = new RootCommand("root description")
{
new Command("inner"),
new HelpOption()
Expand All @@ -37,7 +37,7 @@ public void Help_is_shown_when_required_subcommand_is_missing()
[Fact]
public void Help_display_can_be_disabled()
{
RootCommand rootCommand = new()
RootCommand rootCommand = new("root description")
{
new Option<bool>("--verbose")
};
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task When_there_are_parse_errors_then_customized_help_action_on_anc
[Fact]
public void When_no_help_option_is_present_then_help_is_not_shown_for_parse_errors()
{
RootCommand rootCommand = new();
RootCommand rootCommand = new("root description");
rootCommand.Options.Clear();
var output = new StringWriter();

Expand Down Expand Up @@ -172,4 +172,4 @@ public void Pre_actions_cannot_clear_parse_errors()

result.Errors.Should().NotBeEmpty();
}
}
}
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 @@ -46,9 +46,9 @@ public static AndConstraint<StringCollectionAssertions<IEnumerable<string>>> BeE
return assertions.BeEquivalentTo(expectedValues, c => c.WithStrictOrderingFor(s => s));
}

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

public static AndConstraint<StringAssertions> NotShowHelp(this StringAssertions output) =>
public static AndConstraint<StringAssertions> NotShowHelp(this StringAssertions output) =>
output.Subject.Should().NotMatch("*Description:*Usage:*");
}
}
8 changes: 7 additions & 1 deletion src/System.CommandLine/Help/HelpBuilder.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ public static IEnumerable<Func<HelpContext, bool>> GetLayout()
public static Func<HelpContext, bool> SynopsisSection() =>
ctx =>
{
ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output);
string? description = ctx.Command.Description;
if (string.IsNullOrWhiteSpace(description))
{
return false;
}

ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), description, ctx.Output);
return true;
};

Expand Down