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..51ff8a7d99 100644 --- a/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs +++ b/src/System.CommandLine.Tests/Help/HelpBuilderTests.cs @@ -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() { diff --git a/src/System.CommandLine.Tests/HelpOptionTests.cs b/src/System.CommandLine.Tests/HelpOptionTests.cs index f91242b6eb..1b1a282c5f 100644 --- a/src/System.CommandLine.Tests/HelpOptionTests.cs +++ b/src/System.CommandLine.Tests/HelpOptionTests.cs @@ -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() }; @@ -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("-h")); var output = new StringWriter(); @@ -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") }; @@ -385,4 +385,4 @@ public override int Invoke(ParseResult parseResult) return 0; } } -} \ No newline at end of file +} diff --git a/src/System.CommandLine.Tests/ParseErrorReportingTests.cs b/src/System.CommandLine.Tests/ParseErrorReportingTests.cs index 6b59494ea8..12c886f5bf 100644 --- a/src/System.CommandLine.Tests/ParseErrorReportingTests.cs +++ b/src/System.CommandLine.Tests/ParseErrorReportingTests.cs @@ -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() @@ -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("--verbose") }; @@ -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(); @@ -172,4 +172,4 @@ public void Pre_actions_cannot_clear_parse_errors() result.Errors.Should().NotBeEmpty(); } -} \ No newline at end of file +} diff --git a/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs b/src/System.CommandLine.Tests/Utility/AssertionExtensions.cs index 74dcc77879..61c70c4b61 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) => + public static AndConstraint ShowHelp(this StringAssertions output) => output.Subject.Should().Match("*Description:*Usage:*"); - public static AndConstraint NotShowHelp(this StringAssertions output) => + public static AndConstraint NotShowHelp(this StringAssertions output) => output.Subject.Should().NotMatch("*Description:*Usage:*"); -} \ No newline at end of file +} diff --git a/src/System.CommandLine/Help/HelpBuilder.Default.cs b/src/System.CommandLine/Help/HelpBuilder.Default.cs index 00364784dd..221e13515f 100644 --- a/src/System.CommandLine/Help/HelpBuilder.Default.cs +++ b/src/System.CommandLine/Help/HelpBuilder.Default.cs @@ -185,7 +185,13 @@ public static IEnumerable> GetLayout() public static Func 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; };