Add ToString overloads for some models for easier debugging

This commit is contained in:
Alexey Golub
2019-07-28 18:01:40 +03:00
parent d9c27dc82a
commit 16676cff8c
5 changed files with 83 additions and 15 deletions

View File

@@ -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<T>(this IEnumerable<T> 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<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
dic.TryGetValue(key, out var result) ? result : default;

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}