From b7d2f8a0ee7510c1107d9358a2dbea33840e89b9 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Thu, 25 Aug 2016 21:15:47 -0400 Subject: [PATCH] redid ShowHelp() for easier future changes, and for the ability to word wrap --- Program.cs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index c9eba5c..860d666 100644 --- a/Program.cs +++ b/Program.cs @@ -132,6 +132,12 @@ public static class Program static void ShowHelp() { var descPadding = 26; + var descWidth = Console.BufferWidth - descPadding; + if (descWidth < 20) + descWidth = 20; + else if (descWidth > 56) + descWidth = 56; + var flags = new Dictionary { { "-h, --help", "Display this help message." }, @@ -152,13 +158,39 @@ public static class Program Console.WriteLine(); foreach (var flag in flags) { - Console.WriteLine($"{flag.Key.PadRight(descPadding)}{flag.Value}"); + // word wrap the descriptions, if needed + var desc = flag.Value; + if (desc.Length > descWidth) + { + var sb = new StringBuilder(); + for (int i = 0; i < desc.Length; i += descWidth) + { + if (i > 0) + sb.Append("".PadLeft((flag.Key.StartsWith("--") ? 5 : 2) + descPadding)); + sb.AppendLine(desc.Substring(i, i + descWidth > desc.Length ? desc.Length - i : descWidth).Trim()); + } + desc = sb.ToString().TrimEnd('\n'); + } + Console.WriteLine($"{(flag.Key.StartsWith("--") ? " " : " " )}{flag.Key.PadRight(descPadding)}{desc}"); } Console.WriteLine(); Console.WriteLine("Actions:"); foreach (var action in actions) { - Console.WriteLine($"{action.Key.PadRight(descPadding)}{action.Value}"); + // word wrap the descriptions, if needed + var desc = action.Value; + if (desc.Length > descWidth) + { + var sb = new StringBuilder(); + for (int i = 0; i < desc.Length; i += descWidth) + { + if (i > 0) + sb.Append("".PadLeft(descPadding + 2)); + sb.AppendLine(desc.Substring(i, i + descWidth > desc.Length ? desc.Length - i : descWidth).Trim()); + } + desc = sb.ToString().TrimEnd('\n'); + } + Console.WriteLine($" {action.Key.PadRight(descPadding)}{desc}"); } }