From 16676cff8c9d10b917da2e1efe59a3a9f323ba97 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sun, 28 Jul 2019 18:01:40 +0300 Subject: [PATCH] Add ToString overloads for some models for easier debugging --- CliFx/Internal/Extensions.cs | 12 ++++++++++++ CliFx/Models/CommandInput.cs | 19 ++++--------------- CliFx/Models/CommandOptionInput.cs | 26 ++++++++++++++++++++++++++ CliFx/Models/CommandOptionSchema.cs | 21 +++++++++++++++++++++ CliFx/Models/CommandSchema.cs | 20 ++++++++++++++++++++ 5 files changed, 83 insertions(+), 15 deletions(-) diff --git a/CliFx/Internal/Extensions.cs b/CliFx/Internal/Extensions.cs index 2a1ddbc..90cd612 100644 --- a/CliFx/Internal/Extensions.cs +++ b/CliFx/Internal/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Text; namespace CliFx.Internal { @@ -13,6 +14,17 @@ namespace CliFx.Internal public static string JoinToString(this IEnumerable source, string separator) => string.Join(separator, source); + public static StringBuilder Trim(this StringBuilder builder) + { + while (builder.Length > 0 && char.IsWhiteSpace(builder[0])) + builder.Remove(0, 1); + + while (builder.Length > 0 && char.IsWhiteSpace(builder[builder.Length - 1])) + builder.Remove(builder.Length - 1, 1); + + return builder; + } + public static TValue GetValueOrDefault(this IReadOnlyDictionary dic, TKey key) => dic.TryGetValue(key, out var result) ? result : default; diff --git a/CliFx/Models/CommandInput.cs b/CliFx/Models/CommandInput.cs index 05caa58..60a3038 100644 --- a/CliFx/Models/CommandInput.cs +++ b/CliFx/Models/CommandInput.cs @@ -37,26 +37,15 @@ namespace CliFx.Models var buffer = new StringBuilder(); if (!CommandName.IsNullOrWhiteSpace()) - { buffer.Append(CommandName); - } - if (Options.Any()) + foreach (var option in Options) { - if (buffer.Length > 0) - buffer.Append(' '); - - buffer.Append('['); - - foreach (var option in Options) - { - buffer.Append(option.Alias); - } - - buffer.Append(']'); + buffer.Append(' '); + buffer.Append(option); } - return buffer.ToString(); + return buffer.Trim().ToString(); } } diff --git a/CliFx/Models/CommandOptionInput.cs b/CliFx/Models/CommandOptionInput.cs index 94bc6c3..50e8080 100644 --- a/CliFx/Models/CommandOptionInput.cs +++ b/CliFx/Models/CommandOptionInput.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Text; namespace CliFx.Models { @@ -23,5 +24,30 @@ namespace CliFx.Models : this(alias, new string[0]) { } + + public override string ToString() + { + var buffer = new StringBuilder(); + + buffer.Append(Alias.Length > 1 ? "--" : "-"); + buffer.Append(Alias); + + foreach (var value in Values) + { + buffer.Append(' '); + + var isEscaped = value.Contains(" "); + + if (isEscaped) + buffer.Append('"'); + + buffer.Append(value); + + if (isEscaped) + buffer.Append('"'); + } + + return buffer.ToString(); + } } } \ No newline at end of file diff --git a/CliFx/Models/CommandOptionSchema.cs b/CliFx/Models/CommandOptionSchema.cs index 37be7d6..35b3dfc 100644 --- a/CliFx/Models/CommandOptionSchema.cs +++ b/CliFx/Models/CommandOptionSchema.cs @@ -1,4 +1,6 @@ using System.Reflection; +using System.Text; +using CliFx.Internal; namespace CliFx.Models { @@ -26,5 +28,24 @@ namespace CliFx.Models GroupName = groupName; Description = description; } + + public override string ToString() + { + var buffer = new StringBuilder(); + + if (IsRequired) + buffer.Append('*'); + + if (!Name.IsNullOrWhiteSpace()) + buffer.Append(Name); + + if (!Name.IsNullOrWhiteSpace() && ShortName != null) + buffer.Append('|'); + + if (ShortName != null) + buffer.Append(ShortName); + + return buffer.ToString(); + } } } \ No newline at end of file diff --git a/CliFx/Models/CommandSchema.cs b/CliFx/Models/CommandSchema.cs index e40deda..bf4aef0 100644 --- a/CliFx/Models/CommandSchema.cs +++ b/CliFx/Models/CommandSchema.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Text; +using CliFx.Internal; namespace CliFx.Models { @@ -20,5 +22,23 @@ namespace CliFx.Models Description = description; Options = options; } + + public override string ToString() + { + var buffer = new StringBuilder(); + + if (!Name.IsNullOrWhiteSpace()) + buffer.Append(Name); + + foreach (var option in Options) + { + buffer.Append(' '); + buffer.Append('['); + buffer.Append(option); + buffer.Append(']'); + } + + return buffer.Trim().ToString(); + } } } \ No newline at end of file