This commit is contained in:
Tyrrrz
2024-08-11 22:22:03 +03:00
parent 82b0c6fd98
commit 651146c97b
13 changed files with 36 additions and 41 deletions

View File

@@ -40,7 +40,7 @@ public partial class CliFxException
Exception? innerException = null
) => new(message, DefaultExitCode, false, innerException);
// User errors are typically caused by invalid input and they're meant for the end-user,
// User errors are typically caused by invalid input and are meant for the end-user,
// so we want to show help.
internal static CliFxException UserError(string message, Exception? innerException = null) =>
new(message, DefaultExitCode, true, innerException);

View File

@@ -3,15 +3,13 @@
namespace CliFx.Extensibility;
/// <summary>
/// Base type for custom converters.
/// Defines custom conversion logic for activating command inputs from the corresponding raw command-line arguments.
/// </summary>
public abstract class BindingConverter<T> : IBindingConverter
{
/// <summary>
/// Parses the value from a raw command-line argument.
/// </summary>
public abstract T? Convert(string? rawValue, IFormatProvider? formatProvider);
/// <inheritdoc cref="IBindingConverter.Convert" />
public abstract T? Convert(string? rawArgument, IFormatProvider? formatProvider);
object? IBindingConverter.Convert(string? rawValue, IFormatProvider? formatProvider) =>
Convert(rawValue, formatProvider);
object? IBindingConverter.Convert(string? rawArgument, IFormatProvider? formatProvider) =>
Convert(rawArgument, formatProvider);
}

View File

@@ -1,7 +1,7 @@
namespace CliFx.Extensibility;
/// <summary>
/// Base type for custom validators.
/// Defines custom validation logic for activated command inputs.
/// </summary>
public abstract class BindingValidator<T> : IBindingValidator
{
@@ -15,10 +15,7 @@ public abstract class BindingValidator<T> : IBindingValidator
/// </summary>
protected BindingValidationError Error(string message) => new(message);
/// <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>
/// <inheritdoc cref="IBindingValidator.Validate" />
/// <remarks>
/// You can use the utility methods <see cref="Ok" /> and <see cref="Error" /> to
/// create an appropriate result.

View File

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

View File

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

View File

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

View File

@@ -3,7 +3,7 @@
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties using a custom delegate.
/// Converter for binding command inputs to properties using a custom delegate.
/// </summary>
public class DelegateBindingConverter<T>(Func<string?, IFormatProvider?, T> convert)
: BindingConverter<T>
@@ -12,9 +12,9 @@ public class DelegateBindingConverter<T>(Func<string?, IFormatProvider?, T> conv
/// Initializes an instance of <see cref="DelegateBindingConverter{T}" />
/// </summary>
public DelegateBindingConverter(Func<string?, T> convert)
: this((rawValue, _) => convert(rawValue)) { }
: this((rawArgument, _) => convert(rawArgument)) { }
/// <inheritdoc />
public override T Convert(string? rawValue, IFormatProvider? formatProvider) =>
convert(rawValue, formatProvider);
public override T Convert(string? rawArgument, IFormatProvider? formatProvider) =>
convert(rawArgument, formatProvider);
}

View File

@@ -3,12 +3,12 @@
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="Enum" />.
/// Converter for binding command 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, IFormatProvider? formatProvider) =>
(T)Enum.Parse(typeof(T), rawValue!, true);
public override T Convert(string? rawArgument, IFormatProvider? formatProvider) =>
(T)Enum.Parse(typeof(T), rawArgument!, true);
}

View File

@@ -3,7 +3,7 @@
namespace CliFx.Extensibility;
/// <summary>
/// Defines a custom conversion for binding command-line arguments to command inputs.
/// Defines custom conversion logic for activating command inputs from the corresponding raw command-line arguments.
/// </summary>
/// <remarks>
/// To implement your own converter, inherit from <see cref="BindingConverter{T}" /> instead.

View File

@@ -1,7 +1,7 @@
namespace CliFx.Extensibility;
/// <summary>
/// Defines a custom validation rules for values bound from command-line arguments.
/// Defines custom validation logic for activated command inputs.
/// </summary>
/// <remarks>
/// To implement your own validator, inherit from <see cref="BindingValidator{T}" /> instead.
@@ -9,8 +9,8 @@
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.
/// Validates the input value.
/// Returns <c>null</c> if the validation is successful, or an error in case of failure.
/// </summary>
BindingValidationError? Validate(object? value);
}

View File

@@ -3,7 +3,7 @@
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties without any conversion.
/// Converter for binding command inputs to properties without any conversion.
/// </summary>
public class NoopBindingConverter : IBindingConverter
{

View File

@@ -3,14 +3,14 @@
namespace CliFx.Extensibility;
/// <summary>
/// Converter for binding inputs to properties of type <see cref="Nullable{T}" />.
/// Converter for binding command 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, IFormatProvider? formatProvider) =>
!string.IsNullOrWhiteSpace(rawValue)
? innerConverter.Convert(rawValue, formatProvider)
public override T? Convert(string? rawArgument, IFormatProvider? formatProvider) =>
!string.IsNullOrWhiteSpace(rawArgument)
? innerConverter.Convert(rawArgument, formatProvider)
: null;
}

View File

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