Refactor ArgumentValueConverter into a class

This commit is contained in:
Tyrrrz
2020-11-18 18:37:20 +02:00
parent f9f5a4696b
commit 9be811a89a
6 changed files with 40 additions and 23 deletions

View File

@@ -153,9 +153,9 @@ public class MyCommand : ICommand
// language=cs // language=cs
@" @"
public class MyConverter : IArgumentValueConverter public class MyConverter : ArgumentValueConverter<string>
{ {
public object ConvertFrom(string value) => value; public string ConvertFrom(string value) => value;
} }
[Command] [Command]
@@ -305,9 +305,9 @@ public class MyCommand : ICommand
// language=cs // language=cs
@" @"
public class MyConverter : IArgumentValueConverter public class MyConverter : ArgumentValueConverter<string>
{ {
public object ConvertFrom(string value) => value; public string ConvertFrom(string value) => value;
} }
[Command] [Command]

View File

@@ -165,9 +165,9 @@ namespace CliFx.Tests.Commands
public CustomConvertible(int value) => Value = value; public CustomConvertible(int value) => Value = value;
} }
public class CustomConvertibleConverter : IArgumentValueConverter public class CustomConvertibleConverter : ArgumentValueConverter<CustomConvertible>
{ {
public object ConvertFrom(string value) => public override CustomConvertible ConvertFrom(string value) =>
new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture)); new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture));
} }
} }

View File

@@ -0,0 +1,30 @@
namespace CliFx
{
/// <summary>
/// Implements custom conversion logic that maps an argument value to a domain type.
/// </summary>
/// <remarks>
/// This type is public for legacy reasons.
/// Please derive from <see cref="ArgumentValueConverter{T}"/> instead.
/// </remarks>
public interface IArgumentValueConverter
{
/// <summary>
/// Converts an input value to object of required type.
/// </summary>
public object ConvertFrom(string value);
}
/// <summary>
/// A base type for custom argument converters.
/// </summary>
public abstract class ArgumentValueConverter<T> : IArgumentValueConverter
{
/// <summary>
/// Converts an input value to object of required type.
/// </summary>
public abstract T ConvertFrom(string value);
object IArgumentValueConverter.ConvertFrom(string value) => ConvertFrom(value)!;
}
}

View File

@@ -15,7 +15,7 @@ namespace CliFx.Attributes
/// <summary> /// <summary>
/// Type of converter to use when mapping the argument value. /// Type of converter to use when mapping the argument value.
/// Converter must implement <see cref="IArgumentValueConverter"/>. /// Converter must derive from <see cref="ArgumentValueConverter{T}"/>.
/// </summary> /// </summary>
public Type? Converter { get; set; } public Type? Converter { get; set; }

View File

@@ -181,7 +181,7 @@ If it's not feasible to fit into these constraints, consider using options inste
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameter(s) with invalid converters: Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameter(s) with invalid converters:
{invalidParameters.JoinToString(Environment.NewLine)} {invalidParameters.JoinToString(Environment.NewLine)}
Specified converter must implement {typeof(IArgumentValueConverter).FullName}."; Specified converter must implement {typeof(ArgumentValueConverter<>).FullName}.";
return new CliFxException(message.Trim()); return new CliFxException(message.Trim());
} }
@@ -279,7 +279,7 @@ Environment variable names are not case-sensitive.";
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid converters: Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid converters:
{invalidOptions.JoinToString(Environment.NewLine)} {invalidOptions.JoinToString(Environment.NewLine)}
Specified converter must implement {typeof(IArgumentValueConverter).FullName}."; Specified converter must implement {typeof(ArgumentValueConverter<>).FullName}.";
return new CliFxException(message.Trim()); return new CliFxException(message.Trim());
} }
@@ -292,7 +292,7 @@ Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid validators: Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} option(s) with invalid validators:
{invalidOptions.JoinToString(Environment.NewLine)} {invalidOptions.JoinToString(Environment.NewLine)}
Specified validators must inherit from {typeof(IArgumentValueValidator).FullName}."; Specified validators must inherit from {typeof(ArgumentValueValidator<>).FullName}.";
return new CliFxException(message.Trim()); return new CliFxException(message.Trim());
} }

View File

@@ -1,13 +0,0 @@
namespace CliFx
{
/// <summary>
/// Implements custom conversion logic that maps an argument value to a domain type.
/// </summary>
public interface IArgumentValueConverter
{
/// <summary>
/// Converts an input value to object of required type.
/// </summary>
public object ConvertFrom(string value);
}
}