Добавьте файлы проекта.
This commit is contained in:
93
ArgumentsToolkit.Core/Converters/Converters.cs
Normal file
93
ArgumentsToolkit.Core/Converters/Converters.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace ArgumentsToolkit;
|
||||
|
||||
public class DateTimeOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(DateTime);
|
||||
public object Convert(Type targetType, string value) => DateTime.Parse(value);
|
||||
}
|
||||
|
||||
public class TimeSpanOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(TimeSpan);
|
||||
public object Convert(Type targetType, string value) => TimeSpan.Parse(value);
|
||||
}
|
||||
|
||||
public class JsonOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => true;
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
return System.Text.Json.JsonSerializer.Deserialize(value, targetType)!;
|
||||
}
|
||||
}
|
||||
|
||||
public class BoolOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(bool);
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
// когда указали просто флаг без значения
|
||||
if (string.IsNullOrEmpty(value)) { return true; }
|
||||
|
||||
if (bool.TryParse(value, out var b)) { return b; }
|
||||
|
||||
if (value == "0" || value.Equals("false", StringComparison.OrdinalIgnoreCase)) { return true; }
|
||||
if (value == "1" || value.Equals("true", StringComparison.OrdinalIgnoreCase)) { return true; }
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class EnumOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType.IsEnum;
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
return Enum.Parse(targetType, value, ignoreCase: true);
|
||||
}
|
||||
}
|
||||
|
||||
public class IntOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(int);
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
return int.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public class LongOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(long);
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
return long.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public class DoubleOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(double);
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
return double.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public class DecimalOptionConverter : IOptionConverter
|
||||
{
|
||||
public bool CanConvert(Type targetType) => targetType == typeof(decimal);
|
||||
|
||||
public object Convert(Type targetType, string value)
|
||||
{
|
||||
return decimal.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
17
ArgumentsToolkit.Core/Converters/IOptionConverter.cs
Normal file
17
ArgumentsToolkit.Core/Converters/IOptionConverter.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace ArgumentsToolkit;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс для конвертера значений аргументов.
|
||||
/// </summary>
|
||||
public interface IOptionConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Проверяет, может ли данный конвертер обработать указанный тип.
|
||||
/// </summary>
|
||||
bool CanConvert(Type targetType);
|
||||
|
||||
/// <summary>
|
||||
/// Преобразует строковое значение в указанный тип.
|
||||
/// </summary>
|
||||
object Convert(Type targetType, string value);
|
||||
}
|
||||
43
ArgumentsToolkit.Core/Converters/TypeConverters.cs
Normal file
43
ArgumentsToolkit.Core/Converters/TypeConverters.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace ArgumentsToolkit;
|
||||
|
||||
internal static class TypeConverters
|
||||
{
|
||||
public static bool TryConvert(string input, Type targetType, out object? value)
|
||||
{
|
||||
value = null;
|
||||
try
|
||||
{
|
||||
if (targetType == typeof(string)) { value = input; return true; }
|
||||
if (targetType == typeof(bool))
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) { value = true; return true; }
|
||||
if (bool.TryParse(input, out var b)) { value = b; return true; }
|
||||
if (input == "0" || input.Equals("false", StringComparison.OrdinalIgnoreCase)) { value = false; return true; }
|
||||
if (input == "1" || input.Equals("true", StringComparison.OrdinalIgnoreCase)) { value = true; return true; }
|
||||
return false;
|
||||
}
|
||||
if (targetType.IsEnum) { value = Enum.Parse(targetType, input, ignoreCase: true); return true; }
|
||||
if (targetType == typeof(int)) { value = int.Parse(input, CultureInfo.InvariantCulture); return true; }
|
||||
if (targetType == typeof(long)) { value = long.Parse(input, CultureInfo.InvariantCulture); return true; }
|
||||
if (targetType == typeof(double)) { value = double.Parse(input, CultureInfo.InvariantCulture); return true; }
|
||||
if (targetType == typeof(decimal)) { value = decimal.Parse(input, CultureInfo.InvariantCulture); return true; }
|
||||
if (targetType == typeof(Uri)) { value = new Uri(input, UriKind.RelativeOrAbsolute); return true; }
|
||||
|
||||
// Nullable<T>
|
||||
if (Nullable.GetUnderlyingType(targetType) is Type underlying)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) { value = null; return true; }
|
||||
return TryConvert(input, underlying, out value);
|
||||
}
|
||||
|
||||
// Custom types with string ctor
|
||||
var ctor = targetType.GetConstructor(new[] { typeof(string) });
|
||||
if (ctor != null) { value = ctor.Invoke(new object[] { input }); return true; }
|
||||
|
||||
return false;
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user