From 4acffe925ce24ba7f5374c07769f67e4329edac9 Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Sun, 6 Dec 2020 18:24:30 +0200 Subject: [PATCH] Revert help text changes in the command section Not happy with how it looks right now. Confusing to the user. --- CliFx.Tests/HelpTextSpecs.cs | 12 ++++----- CliFx/Domain/HelpTextWriter.cs | 48 +++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/CliFx.Tests/HelpTextSpecs.cs b/CliFx.Tests/HelpTextSpecs.cs index e254a7d..722eabe 100644 --- a/CliFx.Tests/HelpTextSpecs.cs +++ b/CliFx.Tests/HelpTextSpecs.cs @@ -65,7 +65,7 @@ namespace CliFx.Tests } [Fact] - public async Task Help_text_shows_commands_list_with_description_and_usage_info() + public async Task Help_text_shows_usage_format_which_lists_available_sub_commands() { // Arrange var (console, stdOut, _) = VirtualConsole.CreateBuffered(); @@ -78,16 +78,14 @@ namespace CliFx.Tests .Build(); // Act - var exitCode = await application.RunAsync(new[] { "--help" }); + var exitCode = await application.RunAsync(new[] {"--help"}); // Assert exitCode.Should().Be(0); stdOut.GetString().Should().ContainAll( - "Commands", - "Named command description", - "named [options]", - "Named sub command description", - "named sub [options]" + "Usage", + "... named", + "... named sub" ); _output.WriteLine(stdOut.GetString()); diff --git a/CliFx/Domain/HelpTextWriter.cs b/CliFx/Domain/HelpTextWriter.cs index 6177c23..b7528c6 100644 --- a/CliFx/Domain/HelpTextWriter.cs +++ b/CliFx/Domain/HelpTextWriter.cs @@ -169,6 +169,19 @@ namespace CliFx.Domain // Current command usage WriteCommandUsageLineItem(command, childCommands.Any()); + + // Sub commands usage + if (childCommands.Any()) + { + WriteVerticalMargin(); + + foreach (var childCommand in childCommands) + { + WriteHorizontalMargin(); + Write("... "); + WriteCommandUsageLineItem(childCommand, false); + } + } } private void WriteCommandParameters(CommandSchema command) @@ -283,11 +296,11 @@ namespace CliFx.Domain } } - private void WriteCommandDescendants( + private void WriteCommandChildren( CommandSchema command, - IReadOnlyList descendantCommands) + IReadOnlyList childCommands) { - if (!descendantCommands.Any()) + if (!childCommands.Any()) return; if (!IsEmpty) @@ -295,24 +308,28 @@ namespace CliFx.Domain WriteHeader("Commands"); - foreach (var descendantCommand in descendantCommands) + foreach (var childCommand in childCommands) { - // Description - if (!string.IsNullOrWhiteSpace(descendantCommand.Description)) - { - WriteHorizontalMargin(); - Write(descendantCommand.Description); - WriteVerticalMargin(); - } + var relativeCommandName = !string.IsNullOrWhiteSpace(command.Name) + ? childCommand.Name!.Substring(command.Name.Length).Trim() + : childCommand.Name!; - // Usage - WriteHorizontalMargin(4); - WriteCommandUsageLineItem(descendantCommand, false); + // Name + WriteHorizontalMargin(); + Write(ConsoleColor.Cyan, relativeCommandName); + + // Description + if (!string.IsNullOrWhiteSpace(childCommand.Description)) + { + WriteColumnMargin(); + Write(childCommand.Description); + } WriteLine(); } // Child command help tip + WriteVerticalMargin(); Write("You can run `"); Write(_metadata.ExecutableName); @@ -339,6 +356,7 @@ namespace CliFx.Domain IReadOnlyDictionary defaultValues) { var commandName = command.Name; + var childCommands = root.GetChildCommands(commandName); var descendantCommands = root.GetDescendantCommands(commandName); _console.ResetColor(); @@ -350,7 +368,7 @@ namespace CliFx.Domain WriteCommandUsage(command, descendantCommands); WriteCommandParameters(command); WriteCommandOptions(command, defaultValues); - WriteCommandDescendants(command, descendantCommands); + WriteCommandChildren(command, childCommands); } }