mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
asd
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
10
CliFx/Extensibility/BoolBindingConverter.cs
Normal file
10
CliFx/Extensibility/BoolBindingConverter.cs
Normal 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);
|
||||
}
|
||||
13
CliFx/Extensibility/ConvertibleBindingConverter.cs
Normal file
13
CliFx/Extensibility/ConvertibleBindingConverter.cs
Normal 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);
|
||||
}
|
||||
12
CliFx/Extensibility/DateTimeOffsetBindingConverter.cs
Normal file
12
CliFx/Extensibility/DateTimeOffsetBindingConverter.cs
Normal 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);
|
||||
}
|
||||
12
CliFx/Extensibility/DelegateBindingConverter.cs
Normal file
12
CliFx/Extensibility/DelegateBindingConverter.cs
Normal 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);
|
||||
}
|
||||
12
CliFx/Extensibility/EnumBindingConverter.cs
Normal file
12
CliFx/Extensibility/EnumBindingConverter.cs
Normal 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);
|
||||
}
|
||||
15
CliFx/Extensibility/IBindingConverter.cs
Normal file
15
CliFx/Extensibility/IBindingConverter.cs
Normal 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);
|
||||
}
|
||||
16
CliFx/Extensibility/IBindingValidator.cs
Normal file
16
CliFx/Extensibility/IBindingValidator.cs
Normal 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);
|
||||
}
|
||||
10
CliFx/Extensibility/NoopBindingConverter.cs
Normal file
10
CliFx/Extensibility/NoopBindingConverter.cs
Normal 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;
|
||||
}
|
||||
15
CliFx/Extensibility/NullableBindingConverter.cs
Normal file
15
CliFx/Extensibility/NullableBindingConverter.cs
Normal 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;
|
||||
}
|
||||
13
CliFx/Extensibility/TimeSpanBindingConverter.cs
Normal file
13
CliFx/Extensibility/TimeSpanBindingConverter.cs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user