diff --git a/CliFx/Extensibility/BindingConverter.cs b/CliFx/Extensibility/BindingConverter.cs index ae332aa..49e5ec5 100644 --- a/CliFx/Extensibility/BindingConverter.cs +++ b/CliFx/Extensibility/BindingConverter.cs @@ -1,19 +1,5 @@ namespace CliFx.Extensibility; -/// -/// Defines a custom conversion for binding command-line arguments to command inputs. -/// -/// -/// To implement your own converter, inherit from instead. -/// -public interface IBindingConverter -{ - /// - /// Parses the value from a raw command-line argument. - /// - object? Convert(string? rawValue); -} - /// /// Base type for custom converters. /// @@ -22,7 +8,7 @@ public abstract class BindingConverter : IBindingConverter /// /// Parses the value from a raw command-line argument. /// - public abstract T Convert(string? rawValue); + public abstract T? Convert(string? rawValue); object? IBindingConverter.Convert(string? rawValue) => Convert(rawValue); } diff --git a/CliFx/Extensibility/BindingValidator.cs b/CliFx/Extensibility/BindingValidator.cs index a87562d..5b3e486 100644 --- a/CliFx/Extensibility/BindingValidator.cs +++ b/CliFx/Extensibility/BindingValidator.cs @@ -1,20 +1,5 @@ namespace CliFx.Extensibility; -/// -/// Defines a custom validation rules for values bound from command-line arguments. -/// -/// -/// To implement your own validator, inherit from instead. -/// -public interface IBindingValidator -{ - /// - /// Validates the value bound to a parameter or an option. - /// Returns null if validation is successful, or an error in case of failure. - /// - BindingValidationError? Validate(object? value); -} - /// /// Base type for custom validators. /// diff --git a/CliFx/Extensibility/BoolBindingConverter.cs b/CliFx/Extensibility/BoolBindingConverter.cs new file mode 100644 index 0000000..25347a5 --- /dev/null +++ b/CliFx/Extensibility/BoolBindingConverter.cs @@ -0,0 +1,10 @@ +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties of type . +/// +public class BoolBindingConverter : BindingConverter +{ + /// + public override bool Convert(string? rawValue) => string.IsNullOrWhiteSpace(rawValue) || bool.Parse(rawValue); +} \ No newline at end of file diff --git a/CliFx/Extensibility/ConvertibleBindingConverter.cs b/CliFx/Extensibility/ConvertibleBindingConverter.cs new file mode 100644 index 0000000..c989ab0 --- /dev/null +++ b/CliFx/Extensibility/ConvertibleBindingConverter.cs @@ -0,0 +1,13 @@ +using System; + +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties that implement . +/// +public class ConvertibleBindingConverter(IFormatProvider formatProvider) : BindingConverter where T: IConvertible +{ + /// + public override T? Convert(string? rawValue) => + (T?)System.Convert.ChangeType(rawValue, typeof(T), formatProvider); +} \ No newline at end of file diff --git a/CliFx/Extensibility/DateTimeOffsetBindingConverter.cs b/CliFx/Extensibility/DateTimeOffsetBindingConverter.cs new file mode 100644 index 0000000..1a00845 --- /dev/null +++ b/CliFx/Extensibility/DateTimeOffsetBindingConverter.cs @@ -0,0 +1,12 @@ +using System; + +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties of type . +/// +public class DateTimeOffsetBindingConverter(IFormatProvider formatProvider) : BindingConverter +{ + /// + public override DateTimeOffset Convert(string? rawValue) => DateTimeOffset.Parse(rawValue!, formatProvider); +} \ No newline at end of file diff --git a/CliFx/Extensibility/DelegateBindingConverter.cs b/CliFx/Extensibility/DelegateBindingConverter.cs new file mode 100644 index 0000000..a96ecf7 --- /dev/null +++ b/CliFx/Extensibility/DelegateBindingConverter.cs @@ -0,0 +1,12 @@ +using System; + +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties using a custom delegate. +/// +public class DelegateBindingConverter(Func convert) : BindingConverter +{ + /// + public override T? Convert(string? rawValue) => convert(rawValue); +} \ No newline at end of file diff --git a/CliFx/Extensibility/EnumBindingConverter.cs b/CliFx/Extensibility/EnumBindingConverter.cs new file mode 100644 index 0000000..92ec1dd --- /dev/null +++ b/CliFx/Extensibility/EnumBindingConverter.cs @@ -0,0 +1,12 @@ +using System; + +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties of type . +/// +public class EnumBindingConverter : BindingConverter where T : struct, Enum +{ + /// + public override T Convert(string? rawValue) => (T)Enum.Parse(typeof(T), rawValue!, true); +} \ No newline at end of file diff --git a/CliFx/Extensibility/IBindingConverter.cs b/CliFx/Extensibility/IBindingConverter.cs new file mode 100644 index 0000000..b50b9a4 --- /dev/null +++ b/CliFx/Extensibility/IBindingConverter.cs @@ -0,0 +1,15 @@ +namespace CliFx.Extensibility; + +/// +/// Defines a custom conversion for binding command-line arguments to command inputs. +/// +/// +/// To implement your own converter, inherit from instead. +/// +public interface IBindingConverter +{ + /// + /// Parses the value from a raw command-line argument. + /// + object? Convert(string? rawValue); +} \ No newline at end of file diff --git a/CliFx/Extensibility/IBindingValidator.cs b/CliFx/Extensibility/IBindingValidator.cs new file mode 100644 index 0000000..7e13070 --- /dev/null +++ b/CliFx/Extensibility/IBindingValidator.cs @@ -0,0 +1,16 @@ +namespace CliFx.Extensibility; + +/// +/// Defines a custom validation rules for values bound from command-line arguments. +/// +/// +/// To implement your own validator, inherit from instead. +/// +public interface IBindingValidator +{ + /// + /// Validates the value bound to a parameter or an option. + /// Returns null if validation is successful, or an error in case of failure. + /// + BindingValidationError? Validate(object? value); +} \ No newline at end of file diff --git a/CliFx/Extensibility/NoopBindingConverter.cs b/CliFx/Extensibility/NoopBindingConverter.cs new file mode 100644 index 0000000..1b8c106 --- /dev/null +++ b/CliFx/Extensibility/NoopBindingConverter.cs @@ -0,0 +1,10 @@ +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties without any conversion. +/// +public class NoopBindingConverter : IBindingConverter +{ + /// + public object? Convert(string? rawValue) => rawValue; +} \ No newline at end of file diff --git a/CliFx/Extensibility/NullableBindingConverter.cs b/CliFx/Extensibility/NullableBindingConverter.cs new file mode 100644 index 0000000..22b8f4b --- /dev/null +++ b/CliFx/Extensibility/NullableBindingConverter.cs @@ -0,0 +1,15 @@ +using System; + +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties of type . +/// +public class NullableBindingConverter(BindingConverter innerConverter) : BindingConverter where T : struct +{ + /// + public override T? Convert(string? rawValue) => + !string.IsNullOrWhiteSpace(rawValue) + ? innerConverter.Convert(rawValue) + : null; +} \ No newline at end of file diff --git a/CliFx/Extensibility/TimeSpanBindingConverter.cs b/CliFx/Extensibility/TimeSpanBindingConverter.cs new file mode 100644 index 0000000..c785a05 --- /dev/null +++ b/CliFx/Extensibility/TimeSpanBindingConverter.cs @@ -0,0 +1,13 @@ +using System; + +namespace CliFx.Extensibility; + +/// +/// Converter for binding inputs to properties of type . +/// +public class TimeSpanBindingConverter(IFormatProvider formatProvider) : BindingConverter +{ + /// + public override TimeSpan Convert(string? rawValue) => + TimeSpan.Parse(rawValue!, formatProvider); +} \ No newline at end of file