This commit is contained in:
Tyrrrz
2024-08-06 01:42:09 +03:00
parent 3e7eb08eca
commit e20672328b
12 changed files with 129 additions and 30 deletions

View File

@@ -1,19 +1,5 @@
namespace CliFx.Extensibility;
/// <summary>
/// Defines a custom conversion for binding command-line arguments to command inputs.
/// </summary>
/// <remarks>
/// To implement your own converter, inherit from <see cref="BindingConverter{T}" /> instead.
/// </remarks>
public interface IBindingConverter
{
/// <summary>
/// Parses the value from a raw command-line argument.
/// </summary>
object? Convert(string? rawValue);
}
/// <summary>
/// Base type for custom converters.
/// </summary>
@@ -22,7 +8,7 @@ public abstract class BindingConverter<T> : IBindingConverter
/// <summary>
/// Parses the value from a raw command-line argument.
/// </summary>
public abstract T Convert(string? rawValue);
public abstract T? Convert(string? rawValue);
object? IBindingConverter.Convert(string? rawValue) => Convert(rawValue);
}

View File

@@ -1,20 +1,5 @@
namespace CliFx.Extensibility;
/// <summary>
/// Defines a custom validation rules for values bound from command-line arguments.
/// </summary>
/// <remarks>
/// To implement your own validator, inherit from <see cref="BindingValidator{T}" /> instead.
/// </remarks>
public interface IBindingValidator
{
/// <summary>
/// Validates the value bound to a parameter or an option.
/// Returns null if validation is successful, or an error in case of failure.
/// </summary>
BindingValidationError? Validate(object? value);
}
/// <summary>
/// Base type for custom validators.
/// </summary>

View File

@@ -0,0 +1,10 @@
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="bool" />.
/// </summary>
public class BoolBindingConverter : BindingConverter<bool>
{
/// <inheritdoc />
public override bool Convert(string? rawValue) => string.IsNullOrWhiteSpace(rawValue) || bool.Parse(rawValue);
}

View File

@@ -0,0 +1,13 @@
using System;
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties that implement <see cref="IConvertible" />.
/// </summary>
public class ConvertibleBindingConverter<T>(IFormatProvider formatProvider) : BindingConverter<T> where T: IConvertible
{
/// <inheritdoc />
public override T? Convert(string? rawValue) =>
(T?)System.Convert.ChangeType(rawValue, typeof(T), formatProvider);
}

View File

@@ -0,0 +1,12 @@
using System;
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="DateTimeOffset" />.
/// </summary>
public class DateTimeOffsetBindingConverter(IFormatProvider formatProvider) : BindingConverter<DateTimeOffset>
{
/// <inheritdoc />
public override DateTimeOffset Convert(string? rawValue) => DateTimeOffset.Parse(rawValue!, formatProvider);
}

View File

@@ -0,0 +1,12 @@
using System;
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties using a custom delegate.
/// </summary>
public class DelegateBindingConverter<T>(Func<string?, T> convert) : BindingConverter<T>
{
/// <inheritdoc />
public override T? Convert(string? rawValue) => convert(rawValue);
}

View File

@@ -0,0 +1,12 @@
using System;
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="Enum" />.
/// </summary>
public class EnumBindingConverter<T> : BindingConverter<T> where T : struct, Enum
{
/// <inheritdoc />
public override T Convert(string? rawValue) => (T)Enum.Parse(typeof(T), rawValue!, true);
}

View File

@@ -0,0 +1,15 @@
namespace CliFx.Extensibility;
/// <summary>
/// Defines a custom conversion for binding command-line arguments to command inputs.
/// </summary>
/// <remarks>
/// To implement your own converter, inherit from <see cref="BindingConverter{T}" /> instead.
/// </remarks>
public interface IBindingConverter
{
/// <summary>
/// Parses the value from a raw command-line argument.
/// </summary>
object? Convert(string? rawValue);
}

View File

@@ -0,0 +1,16 @@
namespace CliFx.Extensibility;
/// <summary>
/// Defines a custom validation rules for values bound from command-line arguments.
/// </summary>
/// <remarks>
/// To implement your own validator, inherit from <see cref="BindingValidator{T}" /> instead.
/// </remarks>
public interface IBindingValidator
{
/// <summary>
/// Validates the value bound to a parameter or an option.
/// Returns null if validation is successful, or an error in case of failure.
/// </summary>
BindingValidationError? Validate(object? value);
}

View File

@@ -0,0 +1,10 @@
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties without any conversion.
/// </summary>
public class NoopBindingConverter : IBindingConverter
{
/// <inheritdoc />
public object? Convert(string? rawValue) => rawValue;
}

View File

@@ -0,0 +1,15 @@
using System;
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="Nullable{T}" />.
/// </summary>
public class NullableBindingConverter<T>(BindingConverter<T> innerConverter) : BindingConverter<T?> where T : struct
{
/// <inheritdoc />
public override T? Convert(string? rawValue) =>
!string.IsNullOrWhiteSpace(rawValue)
? innerConverter.Convert(rawValue)
: null;
}

View File

@@ -0,0 +1,13 @@
using System;
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="TimeSpan" />.
/// </summary>
public class TimeSpanBindingConverter(IFormatProvider formatProvider) : BindingConverter<TimeSpan>
{
/// <inheritdoc />
public override TimeSpan Convert(string? rawValue) =>
TimeSpan.Parse(rawValue!, formatProvider);
}