From c322b7029cd18dfee515797dfa23c85e69755352 Mon Sep 17 00:00:00 2001 From: Nikiforov Alexey Date: Thu, 22 Oct 2020 16:02:58 +0300 Subject: [PATCH] Add child command usage in help text (#77) --- CliFx/Domain/HelpTextWriter.cs | 45 +++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/CliFx/Domain/HelpTextWriter.cs b/CliFx/Domain/HelpTextWriter.cs index 7724425..f2b452b 100644 --- a/CliFx/Domain/HelpTextWriter.cs +++ b/CliFx/Domain/HelpTextWriter.cs @@ -105,31 +105,52 @@ namespace CliFx.Domain WriteLine(); } - private void WriteCommandUsage(CommandSchema command, IReadOnlyList childCommands) + private void WriteCommandUsage( + CommandSchema command, + IReadOnlyList childCommands) { if (!IsEmpty) WriteVerticalMargin(); WriteHeader("Usage"); - // Exe name - WriteHorizontalMargin(); - Write(_metadata.ExecutableName); + WriteCommandUsageLineItem(command, childCommands.Count > 0); + if (!IsEmpty) + WriteVerticalMargin(); + + foreach (var childCommand in childCommands) + { + WriteCommandUsageLineItem(childCommand, compactCommand: false, size: 4); + } + } + + private void WriteCommandUsageLineItem( + CommandSchema command, + bool compactCommand = true, + int size = 2) + { + WriteHorizontalMargin(size); + if (compactCommand) + { + // Exe name + Write(_metadata.ExecutableName); + Write(' '); + + } // Command name if (!string.IsNullOrWhiteSpace(command.Name)) { - Write(' '); + // this is fragile, because we rely that subcommand name consists + // of all required tokens Write(ConsoleColor.Cyan, command.Name); } - // Child command placeholder - if (childCommands.Any()) + if (compactCommand) { Write(' '); Write(ConsoleColor.Cyan, "[command]"); } - // Parameters foreach (var parameter in command.Parameters) { @@ -334,7 +355,9 @@ namespace CliFx.Domain CommandSchema command, IReadOnlyDictionary defaultValues) { - var childCommands = root.GetChildCommands(command.Name); + var commandName = command.Name; + var childCommands = root.GetChildCommands(commandName); + var descendantCommands = root.GetDescendantCommands(commandName); _console.ResetColor(); @@ -342,7 +365,7 @@ namespace CliFx.Domain WriteApplicationInfo(); WriteCommandDescription(command); - WriteCommandUsage(command, childCommands); + WriteCommandUsage(command, descendantCommands); WriteCommandParameters(command); WriteCommandOptions(command, defaultValues); WriteCommandChildren(command, childCommands); @@ -385,4 +408,4 @@ namespace CliFx.Domain } } } -} \ No newline at end of file +}