mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor ArgumentValueConverter into a class
This commit is contained in:
@@ -153,9 +153,9 @@ public class MyCommand : ICommand
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
public class MyConverter : IArgumentValueConverter
|
||||
public class MyConverter : ArgumentValueConverter<string>
|
||||
{
|
||||
public object ConvertFrom(string value) => value;
|
||||
public string ConvertFrom(string value) => value;
|
||||
}
|
||||
|
||||
[Command]
|
||||
@@ -305,9 +305,9 @@ public class MyCommand : ICommand
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
public class MyConverter : IArgumentValueConverter
|
||||
public class MyConverter : ArgumentValueConverter<string>
|
||||
{
|
||||
public object ConvertFrom(string value) => value;
|
||||
public string ConvertFrom(string value) => value;
|
||||
}
|
||||
|
||||
[Command]
|
||||
|
||||
@@ -165,9 +165,9 @@ namespace CliFx.Tests.Commands
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
30
CliFx/ArgumentValueConverter.cs
Normal file
30
CliFx/ArgumentValueConverter.cs
Normal 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)!;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ namespace CliFx.Attributes
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
public Type? Converter { get; set; }
|
||||
|
||||
|
||||
@@ -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:
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
|
||||
Specified converter must implement {typeof(ArgumentValueConverter<>).FullName}.";
|
||||
|
||||
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:
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Specified converter must implement {typeof(IArgumentValueConverter).FullName}.";
|
||||
Specified converter must implement {typeof(ArgumentValueConverter<>).FullName}.";
|
||||
|
||||
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:
|
||||
{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());
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user